• 18 Jan, 2025

How to send Email From your Web server with the PHPMailer (SMTP)

How to send Email From your Web server with the PHPMailer (SMTP)

How to send Email From your Web server with the PHPMailer (SMTP)

In this tutorial, we have Send mail using php PHPMailer (SMTP)  there are 2 types of mail send 
    

     1) send mail  Learn Click here 
    2) PHPMailer (SMTP)

Send mail via PHPMailer (SMTP)

Download phpmailer via Composer you can install 


Commands are below

cd public_html 

 

composer2 require phpmailer/phpmailer 


Here put some sample code. for full source code click download now end of post

$mail->SMTPDebug = 2;                   
$mail->isSMTP();                        
$mail->Host       = 'smtp.gfg.com;';    
$mail->SMTPAuth   = true;               
$mail->Username   = 'demo@demo.com';    
$mail->Password   = 'password';         
$mail->SMTPSecure = 'tls';              
$mail->Port       = 587; 

 

 

$mail->setFrom('from@from.com', 'Name');                
$mail->addAddress('receiver1@receiver1.com');           
$mail->addAddress('receiver2@receiver2.com', 'Name');  

 

Recipients of the mail.

$mail->addAttachment('url', 'filename');    // Name is optional

 

For attachments  

isHTML(): If passed true, sets the email format to HTML.
Subject:   subject of the Mail.
Body:  contents of the Mail.

 

Add the content.

$mail->isHTML(true);                                  
$mail->Subject = 'Subject';
$mail->Body    = 'HTML message body in bold!';
$mail->AltBody = 'Body in plain text for non-HTML mail clients';

 

$mail->send();

 

Finally, send the email.

 $mail->send(); 

  

Complete PHP code to send e-mail using PHPMailer. 
 

Sample code


  require 'vendor/autoload.php';
  use PHPMailer\PHPMailer\PHPMailer;
  $mail = new PHPMailer;
  $mail->isSMTP();
  $mail->SMTPDebug = 2;
  $mail->Host = 'smtp.hostinger.com';
  $mail->Port = 587;
  $mail->SMTPAuth = true;
  $mail->Username = 'demo@demo.com';
  $mail->Password = 'Demo@123';
  $mail->setFrom('from@from.com', 'Your Name');
  $mail->addReplyTo('replay@replay.com', 'Your Name');
  $mail->addAddress('recipient@recipient.com', 'Receiver Name');
  $mail->Subject = 'Checking if PHPMailer works';
  $mail->msgHTML(file_get_contents('message.html'), __DIR__);
  $mail->Body = 'This is just a plain text message body';
  //$mail->addAttachment('attachment.txt');
  if (!$mail->send()) {
      echo 'Mailer Error: ' . $mail->ErrorInfo;
  } else {
      echo 'The email message was sent.';
  }
?>


Download Full Code HERE

Download Fome Here