Sending emails through SMTP with PHPMailer and Pepipost

May 30, 2017

Sending personalized transactional emails is no longer a choice but a necessity, given the rise in the web and mobile applications. Why? Because your customers are expecting them. Because they took a certain action on your web/ mobile application and expect to receive an acknowledgment from you. A user may have placed an order, requested a password reset, signed-up for using your service, or asked to be notified of your new blog.

Transactional emails are important for your business. But the big question is –

How do we send these type of emails?

You can use the PHP mail() function to send an email with PHP from the web server itself. This function requires you to specify the three mandatory arguments – 1. the recipient’s email address, 2. the subject line of the message and 3. the actual message. Additionally, there are other two optional parameters.

mail( to, subject, message, headers, parameters );

But there are some disadvantages of using the PHP mail() function. In some cases, recipients did not receive emails sent via PHP mail() although the emails sent had no error message. I recommend you use the external mail server to handle these type of emails.

Delivering transactional emails reliably can be very difficult.

As per the study, 19% of all email goes undelivered. Some of that (7% of the total) gets sent to the Spam, but what’s startling is some chunk of that (12% of the total) simply goes missing — silently blocked by the Internet service providers before it reaches the recipient.

Delivery of your email is depended upon the multiple factors like reputation of sending servers, the content of the campaigns, and its recipients. If your website is hosted on shared hosting, then you are sharing the reputation of that server with all the other websites too which are hosted on the same server. If every website is performing well then it will benefit everyone. But, at the same time if any of them is identified as a spammer, then the whole server gets blacklisted. And because of that, any email sent from that server could go undelivered. If you want to ensure delivery of all your emails then you need a server which has a good reputation.

You could create your own infrastructure for sending the emails, but that is not the best of the options. Mail servers are complex to understand. The smart way would be to rely on robust email service providers like Pepipost to take of your business critical emails. This way, you don’t have to worry about time-consuming maintenance nor spend waking hours on getting emails delivered. And, when you spend no more than $1 for 5000 emails and win back email credits for all emails opened, well – that’s a great bargain!

Whether you go ahead with your own mail server or partner with third-party services, Simple Mail Transfer Protocol (SMTP) is the easiest method to communicate with email servers. Every third-party service provides SMTP as well as their own unique APIs for sending the emails. I would recommend you can start with SMTP until you are fully satisfied with the performance of email delivery. After that, you can integrate with API to get better performance.

Sending emails in PHP with PHPMailer

Once you have decided to use SMTP in your PHP code, I would recommend you to use PHPMailer which is one of the most popular open source PHP libraries to send emails.

PHPMailer is one of the alternatives of PHP mail() function. Advantages of using PHPMailer over PHP mail() function:

● PHPMailer provides an object-oriented interface, whereas mail() is not object oriented.
● A developer does not require to make $headers string which is a headache.
● Sending attachments is much much easier.
● PHPMailer can send alternative plaintext version of email for those email viewers which are not HTML compatible.
● PHPMailer can use a non-local mail server (SMTP) if the developer has authentication whereas a local server is required in mail() function.

In order to use PHPMailer, follow the below steps:

Step 1: Download PHPMailer library’s .zip folder.
Step 2: Create a lib folder in the root folder of your application. Now, extract the downloaded PHPMailer’s library zip folder. Copy & paste PHPMailerAutoload.php, class.phpmailer.php and class.smtp.php file in the lib folder.
Step 3: Require a Pepipost username and password
Step 4: Include the library and instantiate a PHPMailer object

<?php
require_once('pathto/library/class.phpmailer.php');
$mail = new PHPMailer();

Step 5: Set up the object to use SMTP and configure it to point to the proper server
I’m using Pepipost in this example:

<?php
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.pepipost.com";
$mail->Port = 25;
$mail->Username = "Pepipost username";
$mail->Password = "Pepipost password";

Step 6: Set the properties for the particular email you want to send:

<php?
$mail->SetFrom('info@example.com', 'App');
$mail->Subject = "A Transactional Email From Pepipost";
$mail->MsgHTML($body);
$mail->AddAddress($address, $name);

Step 7: Once everything is setup, you call the object’s Send method. If the Send method returns true, then everything worked. If it returns false, then there was a problem.

<?php
if($mail->Send()) {
  echo "Message sent!";
} else {
  echo "Mailer Error: " . $mail->ErrorInfo;
}

PHPMailer Object

$mail = new PHPMailer //Creates the new object.
$mail->From() //Sets the From email address for the message. Default value is root@localhost.
$mail->FromName() //Sets the From name of the message. Default value is Root User.
$mail->addAddress() //Recipient’s address and name.
$mail->addReplyTo() //Address to which recipient will reply.
$mail->AddAttachment() //To add attachment.
$mail->isHTML(true/false) //Send HTML or plain text email.
$mail->Subject //Subject of the mail.
$mail->body //Body of the mail.
$mail->AltBody //Used to display plain text message for those email viewers which are not HTML compatible.
$mail->send() //Sends the mail.
$mail->ErrorInfo //Displays errors if any.
$mail->setLanguage() //To display error messages in some other language. For example to use Russian, we write $mail->setLanguage("ru").

 

Conclusion:

If you are a PHP developer, there is little chance of avoiding having to send emails programmatically. While you may opt for third party services like Pepipost or SendGrid, sometimes that just isn’t an option, and rolling your own email sending library even less so. That’s where PHPMailer and its alternatives (Zend Mail, Swiftmailer, etc) come in.

You can learn more about this library’s APIs in the official documentation. Do you use PHPMailer? Or do you rather rely on fully remote API based solutions? Let us know in the comments!

Write a comment