How to Send Email via SMTP with PHP (Using PHPMailer)
Sending emails is an essential feature in many web applications, whether for notifications, account verification, or marketing. PHP provides several methods for sending emails, but using SMTP with PHPMailer is a safer and more reliable way compared to PHP’s built-in mail()
function.
In this article, we will discuss:
- Benefits of using SMTP over PHP
mail()
- How to install PHPMailer
- Example scripts for sending emails in various formats (text, HTML, with attachments, CC/BCC, etc.)
- Additional tips to prevent emails from being marked as spam
- Final Thoughts
Why Use SMTP?
Although PHP has the mail()
function, there are several reasons why SMTP is recommended:
- Better security – SMTP uses authentication with a username & password, making it harder for hackers to manipulate.
- Avoid spam/junk mail – Trusted SMTP servers have SPF, DKIM, and DMARC configurations that help prevent emails from being flagged as spam.
- Supports attachments & HTML – SMTP supports various email formats, including text, HTML, and attachments.
- Can send emails through services like Gmail, Outlook, and others – SMTP allows integration with popular email providers.
- More stable and trackable – SMTP has clear error logs if an email fails to send, making debugging easier.
Installing PHPMailer
Before sending emails, make sure PHPMailer is installed. If not, run the following command in your terminal:
composer require phpmailer/phpmailer
If you are not using Composer, download PHPMailer from its official GitHub and include autoload.php
in your project.
PHP Script for Sending Email via SMTP
Below is a complete example of how to send emails using SMTP with PHPMailer.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php'; // Ensure PHPMailer is installed
$mail = new PHPMailer(true);
try {
// SMTP Configuration
$mail->isSMTP();
$mail->Host = 'smtp.example.com'; // Replace with your SMTP server
$mail->SMTPAuth = true;
$mail->Username = '[email protected]'; // Replace with your email
$mail->Password = 'your-email-password'; // Replace with your email password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// Sender and recipient settings
$mail->setFrom('[email protected]', 'Your Name');
$mail->addAddress('[email protected]', 'Recipient Name');
$mail->addReplyTo('[email protected]', 'Your Name');
// 1. Plain Text Email
$mail->Subject = 'Plain Text Email';
$mail->Body = "Hello,\n\nThis is an email in plain text format.";
$mail->send();
echo "Plain text email sent!<br>";
// 2. HTML Email
$mail->clearAddresses();
$mail->addAddress('[email protected]', 'Recipient Name');
$mail->Subject = 'HTML Email';
$mail->isHTML(true);
$mail->Body = '<h2>Hello!</h2><p>This is an email in <b>HTML</b> format.</p>';
$mail->AltBody = 'Hello! This is an email in HTML format.';
$mail->send();
echo "HTML email sent!<br>";
// 3. Email with Attachment
$mail->clearAddresses();
$mail->addAddress('[email protected]', 'Recipient Name');
$mail->Subject = 'Email with Attachment';
$mail->Body = 'See the attached file I have sent.';
$mail->addAttachment('path/to/file.pdf', 'Document.pdf'); // Replace with file path
$mail->send();
echo "Email with attachment sent!<br>";
// 4. Email with CC & BCC
$mail->clearAddresses();
$mail->addAddress('[email protected]', 'Recipient Name');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');
$mail->Subject = 'Email with CC & BCC';
$mail->Body = 'This is an email with CC and BCC.';
$mail->send();
echo "Email with CC & BCC sent!<br>";
} catch (Exception $e) {
echo "Email failed to send: {$mail->ErrorInfo}";
}
?>
Tips to Prevent Emails from Being Marked as Spam
- Use your own domain email – Avoid sending emails from
@gmail.com
using non-Gmail SMTP. - Configure SPF, DKIM, and DMARC – These help verify your email as legitimate.
- Avoid suspicious words in the subject – Examples: “FREE!!!”, “100% Safe”, etc.
- Ensure there is alternative text (AltBody) – Useful for email clients that do not support HTML.
- Do not send too many emails in bulk frequently – This can be flagged as spam by email providers.
Final Thoughts
Using SMTP with PHPMailer is the best way to send emails from a PHP application. This method provides better security, reliability, and flexibility compared to PHP’s built-in mail()
. Additionally, emails sent using SMTP are less likely to be marked as spam due to proper authentication from the SMTP server.
With the examples above, you can send emails in various formats, with attachments, and using CC & BCC. Make sure to configure your SMTP server correctly for a smooth email-sending process.
🚀 Now it’s your turn! Try this script in your project, explore more features of PHPMailer, and enhance the email system in your web application! If you have any questions or issues, feel free to ask. Happy coding! 🎯