Sending emails through SMTP with PHPMailer and Pepipost

 In Developer, Email Deliverability, Product

Sending personalized transactional emails is no longer a choice but a necessity, given the rise in the web and mobile applications. You may wonder why? Its because your customers are expecting them. When customers took a certain action on your web/ mobile application, they expect to receive an acknowledgment from you. They 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 –

Transactional emails are important for your business. Here's some information about them?

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.So let’s understand what PHP mail() function is briefly, PHP mail is the built in PHP function that is used to send emails from PHP scripts. 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 that you use an external mail server to handle these type of emails.

Delivering transactional emails reliably can be very difficult.

As per the study, 19% of all emails go undelivered. Some of that (7% of the total) gets sent to the spam, but what’s startling is that a 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 a 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 care 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 can 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

$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:

$mail->SMTPAuth = true;
$mail->Host = "";
$mail->Port = 25;
$mail->Username = "Pepipost username";
$mail->Password = "Pepipost password";

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

$mail->SetFrom('', 'App');
$mail->Subject = "A Transactional Email From Pepipost";
$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.

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").


In conclusion, I would say if you are a PHP developer, there is very little chance to avoid sending 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!

Other Related Article.

Learn more on Best Sendgrid Alternatives

Learn more about Amazon SES Alternatives

Learn more about Best Mailgun Alternatives


Found This Blog Interesting, Please Rate This Blog 1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 3.00 out of 5)
Recent Posts

We are always looking for fresh talent to contribute to our blog. If you have a knack for writing and a passion for email marketing, we would love to have your post on our Guest Blog .

Submit a Guest Post
Pepi thinking

Start typing and press Enter to search

Pin It on Pinterest