Using PHP mailer library to send emails.

Posted By :Harsh Soni |30th June 2018

PHP has its inbuilt mail function to send email, but today PHPmailer is one of the most popular libraries to send emails with. PHP mailer became more popular after its release in 2001.

 

Why PHPmailer ??

 

Sometimes PHP's default mail function is not flexible enough to achieve what a developer needs. This is where PHP mailer library becomes more useful. Its an object-oriented interface where mail() function is not object-oriented. If a developer wants to send an email using PHP default mail function mail(), he has to write code to escape characters, encoding and a proper formatting to send an attachment. On the other hand PHP mailer makes it easy and smooth for their users. PHP mailer can use a non-local mail server or (SMTP) using authentication.

 

Advantages of using PHP mailer Library.

  1. Easy to debug and display error messages with more than 40 language support.
  2. It can send a plain-text version of HTML emails.
  3. Regular updates and strong community support.
  4. Popular and used in mostly all the popular CMS and frameworks.
  5. Integrated SMTP protocol support and authentication over SSL and TLS.

 

You can download PHPmailer library from it GIT repository. Here is an example of sending email using it.

 

<?php
require("class.PHPMailer.php");

$mail = new PHPMailer(); 
$mail->IsSMTP();
$mail->Host = "mail.oodles.com";
$mail->SMTPAuth = true;
$mail->Username = "admin";  // SMTP username 
$mail->Password = "admin1234"; // SMTP password 
$mail->From = "[email protected]"; 
$mail->FromName = "Test Mail"; 
$mail->AddAddress("[email protected]", "Josh Doe"); 
$mail->AddAddress("[email protected]");
$mail->AddReplyTo("[email protected]", "Informatic"); 
$mail->AddAttachment("/tmp/image.jpg", "testImage.jpg");// optional name 
$mail->IsHTML(true);// set email format to HTML 
$mail->Subject = "Add your subject here"; 
$mail->Body    = "This is the HTML message body "; 
$mail->AltBody = "This is the body in plain text for non-HTML mail clients"; 
if(!$mail->Send()) 
{ 
    echo "Message could not be sent."; 
     echo "Mailer Error: " . $mail->ErrorInfo; exit; 
} echo "Message has been sent";

?>

 


About Author

Harsh Soni

Harsh is an experienced Developer with multiple tech stack such as PHP, NodeJs, Javascript, Angular. He loves learning new technologies and experimenting on them.

Request For Proposal

Sending message..

Ready to innovate ? Let's get in touch

Chat With Us