Tag: Mail

Send email by using SMTP in PHP via localhost

Send email by using SMTP in PHP via localhost

/**
* This example shows settings to use when sending via Google’s Gmail servers.
*/
date_default_timezone_set(‘Etc/UTC’);
require ‘PHPMailerAutoload.php’;
//Create a new PHPMailer instance
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = ‘html’;
//Set the hostname of the mail server
$mail->Host = ‘smtp.gmail.com’;
//Set the SMTP port number – 587 for authenticated TLS
$mail->Port = 587;
//Set the encryption system to use – ssl (deprecated) or tls
$mail->SMTPSecure = ‘tls’;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication – use full email address for gmail
$mail->Username = “example@gmail.com”;
//Password to use for SMTP authentication
$mail->Password = “gmail password”;
//Set who the message is to be sent from
$mail->setFrom(‘From-example@gmail.com’, ‘Example’);
//Set an alternative reply-to address
$mail->addReplyTo(‘Reply-example@gmail.com’, ‘Example’);
//Set who the message is to be sent to
$mail->addAddress(‘To-example@gmail.com’, ‘To name’);
//Set the subject line
$mail->Subject = ‘Your Subject’;
//Replace the plain text body with one created manually
$mail->Body = ‘Message Body Text’;
//Attach an image file
$mail->addAttachment(‘images path’);
//send the message, check for errors
if (!$mail->send()) {
echo “Mailer Error: ” . $mail->ErrorInfo;
} else {
echo “Message sent!”;
}

Download

Advertisements