

Last updated : 26-05-2016
PHPMailer and Pepipost Integration:
Once you have decided to use SMTP in your PHP code, we recommend that you use PHPMailer. PHPMailer is one of the most popular open source PHP libraries to send emails.
PHPMailer is one of the alternatives over PHP mail() function. Advantages of using PHPMailer over PHP mail() function are:
- PHPMailer provides an object-oriented interface, whereas PHP mail() is not object oriented.
- A developer is not required to make $headers string which is a headache.
- Sending attachments is much easier.
- PHPMailer can send alternative plain text versions of emails for those email viewers which are not HTML compatible.
- PHPMailer can use a non-local mail server (SMTP) if the developer has authentication. A local server is required in the PHP 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
We’re 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 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, you likely can't avoid sending emails programmatically. While you may opt for a third party service like Pepipost, sometimes that just isn’t an option. Rolling out your own email sending library is 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.
Ready to get started?
Get started with up to 30,000 emails/month free!