Sending a Mail in PHP

Source code :


require '/path/to/PHPMailerAutoload.php'; //Include the library

function sendMail($to){

$mail = new PHPMailer;

$mail->isSMTP();                    // Set mailer to use SMTP
$mail->Host = '';                   // Specify the Mail Server Host
$mail->SMTPAuth = true; 

$mail->Username = 'username';       // Mail Account username or email
$mail->Password = '12234';          // Mail Account password
$mail->SMTPSecure = 'tls';          // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                  // TCP port

$mail->setFrom('', 'Example');   //Add sender
$mail->addAddress($to, "Sample Name");              // Add a recipient

$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';

if(!$mail->send()) {
    echo 'Mail could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Mail Sent!';

sendMail($to); //Pass the Recipient address 


Output :

Mail Sent!

Notes :

Download PHPMailer from Change the host, username and password to connect the Mail server.

Set the from Address and add the subject, body. Provide Alternative Body for those who are reading the mail in Plain Text. That's all your Mail module is ready !

Output :


Comments :