简要介绍如何使用PHPMailer通过GMail的SMTP协议发送邮件。
下载PHPMailer
点击 http://phpmailer.sourceforge.net/ 进入PHPMailer在Source Forge的发布页, 或者直接点击 下载。
解压缩并上传
将下载下来的PHPMailer压缩包解开,然后将解开的目录和文件上传到可以使用PHP的web服务器。
发送Gmail的代码样例
关键部分:
class="php" name="code">$mail->Mailer = "smtp";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username@gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
完整的样例代码:
function send_by_gmail($to, $subject, $content){
// send mail using PHPMailer
require_once("PHPMailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "user@gmail.com"; // GMAIL username
$mail->Password = "password"; // GMAIL password
$mail->SetFrom($to, 'username');
$mail->Subject = $subject;
$mail->Body = $content;
$mail->AddAddress($to, "username");
if(!$mail->Send()) {
return array('status'=>true, 'msg'=>"Mailer Error: " . $mail->ErrorInfo);
} else {
return array('status'=>false, 'msg'=> "Message sent!");
}
}
本文镜像: PHP发送Gmail邮件