As we have already mentioned, the native PHP mail() function has limited functionality when it comes to mass sending. For example, it is not designed for creating engaging email templates that may boost your next campaign or sending a large volume of emails.
But since PHP is still one of the most popular programming languages, it also doesn’t lack resources for sending mass emails. Here are several plugins that we can highly recommend:
Pear Mail
Pear Mail is a class that provides multiple interfaces for sending emails (which is stated in their documentation).
Here is what you can do with Pear Mail:
create complex HTML/text messages with attachments and inlined images (with Mail_Mime class)
send emails via PHP’s built-in mail() function, a sendmail program, or SMTP server
send multiple emails from a queue (with Mail_Queue class).
Pear documentation looks a bit complicated but it’s still informative, and you can find several tutorials. To be able to compare several mail packages, let’s review code for sending a standard booking confirmation email. It will contain HTML and text parts, a single attachment, and will be sent via an authenticated SMTP server.
For email experiments, we will use Mailtrap, a fake SMTP server. It imitates a real SMTP server and traps your test email in the virtual inboxes. This way, your email samples will never go to the inboxes of the real customers.
require_once './vendor/autoload.php';
$from = 'Your Hotel <confirmation@hotel.com>';
$to = 'Me <me@gmail.com>';
$subject = 'Thanks for choosing Our Hotel!';
$headers = ['From' => $from,'To' => $to, 'Subject' => $subject];
// include text and HTML versions
$text = 'Hi there, we are happy to confirm your booking. Please check the document in the attachment.';
$html = 'Hi there, we are happy to <br>confirm your booking.</br> Please check the document in the attachment.';
//add attachment
$file = '/confirmations/yourbooking.pdf';
$mime = new Mail_mime();
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'text/plain');
$body = $mime->get();
$headers = $mime->headers($headers);
$host = 'smtp.mailtrap.io';
$username = '1a2b3c4g5f6g7e'; // generated by Mailtrap
$password = '1a2b3c4g5f6g7e'; // generated by Mailtrap
$port = '2525';
$smtp = Mail::factory('smtp', [
'host' => $host,
'auth' => true,
'username' => $username,
'password' => $password,
'port' => $port
]);
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo('<p>' . $mail->getMessage() . '</p>');
} else {
echo('<p>Message successfully sent!</p>');
}
Swift Mailer
Swift Mailer is another popular package for sending emails in PHP. It is feature-rich, well covered by documentation, and pretty straightforward in use.
Here is what you can do with Swift Mailer:
create complex HTML/multipart templates
add attachments and embed images
send emails via authenticated SMTP, sendmail, Postfix, or your own transport
use additional plugins.
Besides that, Swift Mailer offers enhanced security and handles large attachments and images with low memory usage.
For more details, refer to the “How to Use Swift Mailer to Send Emails from PHP Apps” post. Below we will demonstrate a simple example of the same sending booking confirmation we used above.
<?php
require_once './vendor/autoload.php';
try {
// Create the SMTP transport
$transport = (new Swift_SmtpTransport('smtp.mailtrap.io', 2525))
->setUsername('1a2b3c4d5e6f7g')
->setPassword('1a2b3c4d5e6f7g');
$mailer = new Swift_Mailer($transport);
// Create a message
$message = new Swift_Message();
$message->setSubject('Thanks for choosing Our Hotel!');
$message->setFrom(['confirmation@hotel.com' => 'Your Hotel']);
$message->addTo('me@gmail.com','Me');
// Add attachment
$attachment = Swift_Attachment::fromPath('./confirmations/yourbooking.pdf');
$message->attach($attachment);
// Set the plain-text part
$message->setBody('Hi there, we are happy to confirm your booking. Please check the document in the attachment.');
// Set the HTML part
$message->addPart('Hi there, we are happy to <br>confirm your booking.</br> Please check the document in the attachment.', 'text/html');
// Send the message
$result = $mailer->send($message);
} catch (Exception $e) {
echo $e->getMessage();
}
To learn about sending multiple emails, PHP built-in mail() function and PHP Mailer head to our tutorial with code samples to send emails with PHP on Mailtrap’s Blog.
Top comments (1)
Insightful!
but just so you know swiftmailer is deprecated symfony mailer is now in use