详细教程请参考官方文档:http://phpmailer.worxware.com/?pg=tutorial
1. 下载PHPMailer并放到你的工程文件夹下,GitHub链接:https://github.com/PHPMailer/PHPMailer
2. 新建一个php文件,写下如下代码,我这里使用gmail的smtp server:
<?php
require("PHPMailerAutoload.php");
$mail = new PHPMailer();
define('GUSER', 'polyu.mail123@gmail.com'); // GMail username
define('GPWD', 'polyu123456'); // GMail password
function smtpmailer($to, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
}
$subject = "[SuitSystem] Retrieve Password Mail";
$body = "Hi ".$username.",
Please open the following website to reset your password.
http://".$_SERVER['SERVER_ADDR'].":8000/resetUserPassword.php
This is a system generated message. Please do not reply.";
if (smtpmailer($email, 'PasswordReset@gmail.com', 'Tracy', $subject, $body)) {
echo "<script type='text/javascript'>alert('Email has been sent. Please check your mailbox.');history.back(-1); </script>";
exit;
}
?>
3.
history.back(-1);在alert对话框弹出后点击确定会跳转回上一个页面,如果没有这句话则页面会停留在当前的php脚本下。