PHP开发

简单之美 – PHP Mail Class – PHPMailer

发邮件是一件再普通不过的事情,php mail()函数用起来虽然不麻烦,但类似发送附件等比较复杂的工作,如果每次都写一遍代码,重复性劳动就太不值了。为此,今天为大家介绍一个简单实用、支持良好、使用广泛的的php 邮件类 – PHPMailer

PHPMailer的官方网站是http://phpmailer.worxware.com/,除了PHPMailer还有很多附属产品可以免费下载使用,大大方便了我们编写发送邮件的程序。

PHPMailer使用起来很容易上手,首先下载PHPmailer,下载地址http://sourceforge.net/project/showfiles.php?group_id=26031&package_id=252700

为了体验简单之美我们只需要下载lite版本,里面没有test或者example文件,不会让你对着一堆文件愁眉不展,打开readme我们就能轻松找到这个php  mail class的使用方式,如下所示

<?php
require( “phpmailer.inc.php” );

require( “smtp.inc.php” );

$mail = new phpmailer;

//如果不使用SMPT,就不需要引入smtp.inc.php
//也不需要调用isSMTP设置使用smtp发送。
$mail->IsSMTP(); // set mailer to use SMTP
$mail->From = “from@email.com”;
$mail->FromName = “Mailer”;
$mail->Host = “smtp1.site.com;smtp2.site.com”; 
// specify main and backup server

//无论是否使用smtp,下面的代码是共用的

//设置收件人,名字是可选的
$mail->AddAddress( “josh@site.com”, “Josh Adams” );
$mail->AddAddress( “ellen@site.com” );   // name is optional

//设置邮件回复地址
$mail->AddReplyTo(“info@site.com”, “Information”);
$mail->WordWrap = 50;    // set word wrap

//发送附件,发送单个或多个附件如此简单
$mail->AddAttachment( “c:\\temp\\js-bak.sql” );  // add attachments
$mail->AddAttachment( “c:/temp/11-10-00.zip” );

//设置使用HTML格式发送邮件
$mail->IsHTML(true);    // set email format to HTML
$mail->Subject = “Here is the subject”;
$mail->Body = “This is the message body”;

//设置完成,发送
$mail->Send(); // send message
?>

在选择php的文件类时最好选使用人数较多的,防止出现安全隐患。

2条评论

评论已关闭。