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

PHP 利用Gmail发送邮件

2010-06-25 13:22 441 查看
系统环境:windows7+wamp5

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

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

2、下载PHPMailer,

PHPMailer是一个用PHP写的用于邮件发送的类。可以去http://sourceforge.net/projects/phpmailer/上下到最新的版本,我用的事PHPMailer_v5.1。

3、将下好的包解压到服务器上,在包里面有个examples文件夹里面是实例,根据需求我选择了test_smtp_gmail_basic.php,修改其中的部分代码为

$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 = "test@gmail.com

"; // GMAIL username

$mail->Password = "test

"; // GMAIL password

$mail->SetFrom('test2@gmail.com

', 'test

2

');//邮件发送者

$mail->AddReplyTo("test2@gmail.com

","test2
");

$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";

$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address = "test3@domain.com

";//邮件接收者

$mail->AddAddress($address, "test
3

");

OK!这样配置就行了,其本身还支持附件的功能,在示例里面有相关的代码。

4、还有一个小问题就是,我文件的的编码为utf-8,当邮件的主题也就是$mail->Subject ,包含有中文的时候接收方看到的事一堆乱码,解决的办法是 $subject = ".........";$mail->Subject = "=?UTF-8?B?".base64_encode(
$subject)."?=
";

5、最后一个问题就是感觉效率比较低,发邮件的时间比较长。

6、在某些情况下邮件正文也会出现乱码,这个需要修改引用文件class.phpmailer.php

public $CharSet = 'iso-8859-1';

/**

* Sets the Content-type of the message.

* @var string

*/

public $ContentType = 'text/plain';

/**

* Sets the Encoding of the message. Options for this are

* "8bit", "7bit", "binary", "base64", and "quoted-printable".

* @var string

*/

public $Encoding = '8bit';

以上配置是对邮件正文的设置,及邮件发送的编码方式等。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: