Knowledgebase
HOW TO: Sending email using PHP (PHPMailer)
Posted by on 04 April 2018 10:11 AM

You can send email using PHPMailer from your PHP script with SMTP authentication. To use PHPMailer classes, you just need to download PHPMailer class files from the following URL:

https://github.com/PHPMailer/PHPMailer

Following is the sample code to send email from your PHP script with SMTP authentication:

 

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'src/PHPMailer.php';
require 'src/SMTP.php';

$mail = new PHPMailer(true);

try {
//Server settings
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'mail.domain.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'email@domain.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 465; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

//Recipients
$mail->setFrom('email@domain.com', 'Name');
$mail->addAddress('recipient@domain.com', 'Name'); // Add a recipient
$mail->addReplyTo('email@domain.com', 'Name');

// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

 


Please make sure that you make the changes in the above script as necessary. 


Note: For security reason, our mail servers require your script to pass SMTP authentication in order to send email.

(5 vote(s))
Helpful
Not helpful

Comments (0)
Copyright © 1998 - 2018 Shinjiru International Inc. All Rights Reserved.