使用PHP,PHPMailer和GMail发送电子邮件

[size=large][b]简要介绍如何使用PHPMailer通过GMail的SMTP协议发送邮件。[/b][/size]

[color=blue][size=large]下载PHPMailer[/size][/color]
点击 [url=http://adf.ly/2960050/iteyephpmailerdownload]http://phpmailer.sourceforge.net/[/url] 进入PHPMailer在Source Forge的发布页, 或者直接点击 [url=http://sourceforge.net/project/showfiles.php?group_id=26031]下载[/url]。

[color=blue][size=large]解压缩并上传[/size][/color]
将下载下来的PHPMailer压缩包解开,然后将解开的目录和文件上传到可以使用PHP的web服务器。

[color=blue][size=large]发送Gmail的代码样例[/size][/color]

[color=red]关键部分:[/color]
$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

[color=red]完整的样例代码:[/color]
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!");
}
}


本文镜像: [url=http://www.jiani.info/2012/08/%e4%bd%bf%e7%94%a8phpphpmailer%e5%92%8cgmail%e5%8f%91%e9%80%81%e7%94%b5%e5%ad%90%e9%82%ae%e4%bb%b6/]PHP发送Gmail邮件[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值