
Using email functionality is common in web applications. While PHP’s built-in mail() function can be used to send emails, a more reliable and flexible method is using the PHPMailer library. Below is a practical guide on how to send emails using PHPMailer with SMTP.
1. Installing PHPMailer
You can install PHPMailer using Composer:
composer require phpmailer/phpmailer
Alternatively, download it from the PHPMailer GitHub page and include the src/ folder in your project.
2. Sending Email via SMTP
Here’s how to send an email using Gmail’s SMTP server:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'youremail@gmail.com';
$mail->Password = 'yourpassword';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->setFrom('youremail@gmail.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->isHTML(true);
$mail->Subject = 'Test Email';
$mail->Body = '<h1>Hello!</h1><p>This is a test email.</p>';
$mail->AltBody = 'Hello! This is a test email.';
$mail->send();
echo 'Email sent successfully!';
} catch (Exception $e) {
echo "Email could not be sent. Error: {$mail->ErrorInfo}";
}
3. SMTP Configuration and Alternatives
Gmail SMTP:
Host: smtp.gmail.com
Port: 587 (TLS) / 465 (SSL)
SMTPAuth: true
SMTPSecure: tls or ssl
Outlook SMTP:
Host: smtp.office365.com
Port: 587
SMTPAuth: true
SMTPSecure: tls
Yahoo SMTP:
Host: smtp.mail.yahoo.com
Port: 465
SMTPAuth: true
SMTPSecure: ssl
4. Security Best Practices
- Do not store passwords directly in your code. Use environment variables or configuration files.
- Consider using third-party services like Mailgun or SendGrid.
- Configure SPF, DKIM, and DMARC to improve email delivery and avoid spam filters.
- Rate limiting: Avoid being flagged as spam by spacing out batch emails.
5. Sending Emails with Templates
$mail->Body = file_get_contents('email_template.html');
You can also replace placeholders dynamically:
$template = file_get_contents('email_template.html');
$template = str_replace('{{username}}', 'John Doe', $template);
$mail->Body = $template;
6. Sending Attachments
$mail->addAttachment('/path/to/file.pdf', 'FileName.pdf');
Repeat the method to add multiple attachments.
7. Advanced Error Debugging
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
This helps in identifying issues more effectively during development.
8. Email Sending Performance
- Keep the SMTP connection alive:
$mail->smtpConnect();
- Reuse the PHPMailer instance for batch sending:
foreach ($users as $user) {
$mail->addAddress($user['email'], $user['name']);
$mail->Body = str_replace('{{username}}', $user['name'], $template);
$mail->send();
$mail->clearAddresses();
}
PHPMailer provides a reliable and flexible way to send emails in PHP. With advanced features like attachments, HTML templates, and SMTP debugging, you can build robust email systems for your applications. For better scalability and delivery rates, consider integrating with email services like Mailgun, SendGrid, or Amazon SES.
Related Articles
