您的位置:首页 > 编程语言 > PHP开发

PHP利用PHPMailer组件的Gmail发信能力发送电子邮件

2011-05-19 07:50 513 查看
系统环境:windows7+wamp5

环境要求:Gmail账号+open_ssl+PHPMailer;

1、默认情况下wamp5是没有打开Open_ssl的,这是需要修改php.ini,将extension=php_openssl.dll前面的分号去掉;

2、下载PHPMailer

PHPMailer是一个用PHP写的用于邮件发送的类。可以下載最新的版本,我用的是PHPMailer_v5.0.2

3、将下好的包解压到服务器上,打开examples文件夾,然後在裏邊單獨建立一個PHP的測試文件(自己命名就好了),代碼為:

//如果要轉載本文請注明出處,免的出現版權紛爭,我不喜歡看到那種轉載了我的作品卻不注明出處的人 Seven{See7di#Gmail.com}

<?php
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
require_once('../class.phpmailer.php');
//include("class.smtp.php"); //可选,会自动从class.phpmailer.php加载
$mail=new PHPMailer();
$body=file_get_contents('contents.html');
$body=Strtr($body,Array("\\"=>""));//$body= eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // 告诉程式要使用SMTP
$mail->SMTPDebug = 2; // 开启 SMTP debug 信息 (测试时使用)// 1 = 错误和消息// 2 = 只有消息
$mail->SMTPAuth = true; // 开启 SMTP 验证
$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 = "see7di@gmail.com"; // GMAIL用户名
$mail->Password = "******"; // GMAIL密码
$mail->CharSet = "utf-8"; //加入该行代码可以防止信件内容乱码

$mail->SetFrom('see7di@gmail.com','张三'); //发信人邮件地址及用户名
//$mail->AddReplyTo("see7di@gmail.com","张三"); //回复地址及用户名

$subject='這是郵件標題';
$mail->Subject = "=?UTF-8?B?".base64_encode($subject)."?= ";//使用base64编码是为了防止信件标题乱码

$mail->MsgHTML($body);

$mail->AddAddress("see7di@msn.com","李四"); //接收者邮件地址及用户名
//附件
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
echo "Mailer Error: ".$mail->ErrorInfo;
}else{
echo "Message sent!";
}
?>

OK!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: