A PHP email sending implementation using PHPMailer library version 5.2.4. This implementation provides a robust way to send emails through SMTP with support for HTML content, attachments, and various security features. Sending mail with smtp and webmail in PHP / PHP Mailer Class.
This software is developed during my free time and I will be glad if somebody will support me.
Everyone's time should be valuable, so please consider donating.
https://buymeacoffee.com/oxcakmak
- Features
- Requirements
- Basic Usage
- Advanced Features
- Error Handling
- Debugging
- Security Considerations
- Known Limitations
- Troubleshooting
- License
- Credits
- SMTP email sending
- HTML and plain text email support
- File attachments support
- CC and BCC recipients
- DKIM signing capability
- Multiple character set support
- Error handling and debugging
- Custom headers support
- Embedded images in HTML
- VERP support
- Multiple protocols (mail, sendmail, smtp)
- PHP 5.0 or higher
- SMTP server access
- SSL/TLS support for secure connections
- Required PHP extensions:
- openssl
- pdo
- mbstring
require_once __DIR__ . '/smtpmailer.php';
$mail = new SmtpMailer();
// Configure SMTP settings
$mail->IsSMTP();
$mail->Host = 'your.smtp.server';
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl'; // or 'tls'
$mail->Port = 465; // or 587 for TLS
$mail->Username = 'your_username';
$mail->Password = 'your_password';
$mail->CharSet = 'utf-8';
// Set email content
$mail->IsHTML(true);
$mail->From = 'sender@example.com';
$mail->FromName = 'Sender Name';
$mail->AddAddress('recipient@example.com');
$mail->Subject = 'Email Subject';
$mail->Body = 'HTML message body';
// not necessary actually
$mail->AltBody = 'Plain text message body';
// Send email
if($mail->Send()) {
echo "Email sent successfully";
} else {
echo "Email sending failed: " . $mail->ErrorInfo;
}
$mail->AddAttachment('path/to/file.pdf', 'filename.pdf');
$mail->AddCC('cc@example.com');
$mail->AddBCC('bcc@example.com');
$mail->AddEmbeddedImage('path/to/image.jpg', 'image_cid');
$mail->Body = 'Embedded image: <img src="cid:image_cid">';
$mail->DKIM_domain = 'example.com';
$mail->DKIM_private = 'path/to/private.key';
$mail->DKIM_selector = 'selector';
$mail->DKIM_passphrase = '';
The library includes comprehensive error handling:
try {
if(!$mail->Send()) {
echo "Error: " . $mail->ErrorInfo;
}
} catch (phpmailerException $e) {
echo "Error: " . $e->errorMessage();
}
Enable debugging for troubleshooting:
$mail->SMTPDebug = 1; // 0 = off, 1 = client messages, 2 = client and server messages
- Always use SMTPSecure with SSL/TLS when possible
- Keep credentials secure and never commit them to version control
- Validate email addresses before sending
- Use appropriate error handling
- Keep PHPMailer updated to the latest version
- Implement rate limiting if sending bulk emails
- Some features require PHP 5.3 or higher
- SMTP connections might be blocked by firewalls
- File attachment size limitations based on PHP settings
- Some email clients might handle HTML emails differently
Common issues and solutions:
-
Connection failed
- Check SMTP credentials
- Verify port numbers
- Ensure SSL/TLS settings match server requirements
-
Authentication failed
- Verify username and password
- Check if SMTP authentication is required
-
Timeout issues
- Adjust SMTP timeout settings
- Check server response time
This implementation uses PHPMailer which is distributed under the LGPL license.
Based on PHPMailer:
- Version: 5.2.4
- Original Authors: Andy Prevost, Marcus Bointon
- Current Maintainer: Jim Jagielski
- Project: https://code.google.com/a/apache-extras.org/p/phpmailer/