Get Adobe Flash player
Merry Christmas!

Beginning PHP

PHP中通过PHPMailer发邮件

我们在做测试时可以直接使用PHP中自带的mail();函数库来向Window的环境下发邮件(因不需要进行用户身份认证),可如果要向Linux/Unix发邮件时,通过mail()则通不过。下面我们介绍PHP中常用的一个邮件发送框架PHPMailer.

PHPMailer 是一个功能强大的邮件类,PHPMailer是一个用于发送电子邮件的PHP函数包。它提供的功能包括:

PHPMailer的主要功能特点:PHPMailer是一个用于发送电子邮件的PHP函数包。它提供的功能包括:

*.在发送邮时指定多个收件人,抄送地址,暗送地址和回复地址
*.支持多种邮件编码包括:8bit,base64,binary和quoted-printable
*.支持SMTP验证
*.支持冗余SMTP服务器
*.支持带附件的邮件和Html格式的邮件
*.自定义邮件头
*.支持在邮件中嵌入图片
*.调试灵活
*.经测试兼容的SMTP服务器包括:Sendmail,qmail,Postfix,Imail,Exchange等
*.可运行在任何平台之上

下面介绍使用步骤:

首先修改PHP环境,打开php.ini文件,添加如下内容后,重启Apache.

SMTP = smtp.example.com //邮件服务器IP地址
smtp_port = 25 //端口
sendmail_from = mail@example.com //你的邮箱地址

第一步:需要下载PHPMailer文件包phpmailer-VXXX.zip,来自开源社区: http://phpmailer.worxware.com/

第二步:确认你的服务器系统已经支持socket 如下图,通过 phpinfo(); 查看是否支持sockets

如果没有这一项:在Linux环境下在PHP编译时必须给定一个用于./configure --enable-sockets 的配置选项。国为socket是属于PHP扩展部分,在Window环境下直接修改php.ini文件中 extension=php_sockets.dll 前面的分号前了。即可。

第三步:解压缩后把class.phpmailer.php,class.pop3.php,class.smtp.php文件解压到你的web服务器对应目录下,同时把class.phpmailer.php,class.pop3.php,class.smtp.php文件的添加到php.ini文件里边的includes_path.重启Apache就可以了,说明:首先包含class.phpmailer.php,然后创建对象,设置参数,调用成员函数。具体请见下面的示例代码:

1 下载phpmailer(http://phpmailer.worxware.com/)下载2.1对应php5.1版本的模块.

2 确认你的服务器系统已经支持socket 通过在页面输入phpinfo();查看是否支持sockets

3 解压到一个文件夹

4 在php文件中包含 require_once("class.phpmailer.php");

5 使用SMTP发送邮件:

下面是中文版例子:

<?php

//phpinfo();
require_once("class.phpmailer.php");

$mail = new PHPMailer(); //得到一个PHPMailer实例

$mail->CharSet = "utf8"; //设置采用utf8中文编码
$mail->IsSMTP(); //设置采用SMTP方式发送邮件
$mail->Host = "smtp.163.com"; //设置邮件服务器的地址,这里以163测试
$mail->Port = 25; //设置邮件服务器的端口,默认为25

$mail->From = "XXX@163.com"; //设置发件人的邮箱地址
$mail->FromName = "XXX"; //设置发件人的姓名
$mail->SMTPAuth = true; //设置SMTP是否需要密码验证,true表示需要

$mail->Username="XXX"; //你的用户名

$mail->Password = 'XXX'; //密码
$mail->Subject = $subject; //设置邮件的标题

$mail->Body = "my content"; //内容,先可用英文测试,中文有时会有乱码问题 ,导至发送不成功

$mail->AddReplyTo("XXX@163.com","XXX"); //设置回复的收件人的地址

$mail->AddAddress("XXX@163.com","XXX"); //设置收件的地址

if(!$mail->Send()) { //发送邮件
echo "发送失败".$mail->ErrorInfo;
} else {
echo "发送成功";
}

?>

下面是英文版例子和PHPmailer的安装说明:

Installation:

Copy class.phpmailer.php into your php.ini include_path. If you are using the SMTP mailer then place class.smtp.php in your path as well. In the language directory you will find several files like phpmailer.lang-en.php. If you look right before the .php extension that there are two letters. These represent the language type of the translation file. For instance "en" is the English file and "br" is the Portuguese file. Chose the file that best fits with your language and place it in the PHP include path. If your language is English then you have nothing more to do. If it is a different language then you must point PHPMailer to the correct translation. To do this, call the PHPMailer SetLanguage method like so:

// To load the Portuguese version
$mail->SetLanguage("br", "/optional/path/to/language/directory/");

That's it. You should now be ready to use PHPMailer!

A Simple Example:

<?php
require("class.phpmailer.php");

$mail = new PHPMailer();

$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtp1.example.com;smtp2.example.com"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "jswan"; // SMTP username
$mail->Password = "secret"; // SMTP password

$mail->From = "from@example.com";
$mail->FromName = "Mailer";
$mail->AddAddress("josh@example.net", "Josh Adams");
$mail->AddAddress("ellen@example.com"); // name is optional
$mail->AddReplyTo("info@example.com", "Information");

$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->AddAttachment("/var/tmp/file.tar.gz"); // add attachments
$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // optional name
$mail->IsHTML(true); // set email format to HTML

$mail->Subject = "Here is the subject";
$mail->Body = "This is the HTML message body <b>in bold!</b>";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";

if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}

echo "Message has been sent";
?>

以上资料读者可自行参考。

GMAIL的配置方法:

下面是相关配置代码:

$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the server
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server

然后:你的PHP必须支持SSL
可以修改php.ini, 把php_openssl打开
还有要把openssl.dll 和 libeay32.dll 和 ssleay32.dll拷贝到system32(windows 7需要复制到windows目录下)下

然后测试就可以了.

另phpmailer官方现在建议使用:

Gmail Server CHANGES
Google has recently changed ports and requirements for accessing Gmail accounts.

The code we previous recommended to use was:

$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the server
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server

The code that we now recommend (and have tested) is:

$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the server
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5

What can i design?

  • Logo Design and Brand Development: Whether you need to create a new brand or revitalize an existing one, I can help. I can design Logo Design, Identity Development, Brand Style Guides, Custom Illustration, Artwork Vectorization and much more!

  • Print Design, Business Collateral Production: I can design Business Systems, Photography, Production Work, business cards, letterhead, brochures, catalogs, mailers and much more!

  • Websites Design and Maintenance: I design killer websites for companies of almost every size and shape. I can design WebSite and WebSite Maintenance, E-Commerce, Content Managment and much more!

  • Creative Marketing and Logo Promotion: I can do Search Engines optimization, Email Marketing, Printed T-shirts, Logo Apparel, Promotion Gifts and much more!

I am a designer who is easygoing, useful, well-organized, independent, skillful, confidence, and calm under pressure.