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 software developer with a specialization in the MEAN stack. He is skilled in a wide range of web technologies, including Angular, Node.js, PHP, AWS, and Docker.Throughout his career, Harsh has demonstrated a strong commitment to delivering high-quality software solutions that meet the unique needs of his clients and organizations. His proficiency in Angular and Node.js has allowed him to build dynamic and interactive user interfaces, leveraging the power of modern front-end frameworks. Harsh's expertise also extends to cloud computing and infrastructure management using AWS, enabling him to design and deploy scalable applications with ease. Additionally, his knowledge of Docker has enabled him to streamline the development and deployment process, enhancing efficiency and reducing time-to-market. He excels at analyzing complex technical challenges and devising efficient strategies to overcome them, ensuring the successful completion of projects within deadlines.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us