首页 > 代码库 > 我的PHPMailer_v5.1 使用

我的PHPMailer_v5.1 使用

<?php/*** Simple example script using PHPMailer with exceptions enabled* @package phpmailer* @version $Id$*/require_once‘/class.phpmailer.php‘;require_once‘/class.smtp.php‘; try {    $mail = new PHPMailer(true); //New instance, with exceptions enabled    $body             = file_get_contents(‘contents.html‘);    $body             = preg_replace(‘/\\\\/‘,‘‘, $body); //Strip backslashes    $mail->IsSMTP();                           // tell the class to use SMTP    $mail->SMTPAuth   = true;                  // enable SMTP authentication    $mail->Port       = 25;                    // set the SMTP server port    $mail->Host       = "smtp.163.com"; // SMTP server    $mail->Username   = "1585358****@163.com";     // SMTP server username    $mail->Password   = "*******";            // SMTP server password    //$mail->IsSendmail();  // tell the class to use Sendmail    $mail->AddReplyTo("1585358****@163.com","First Last");    $mail->From       = "1585358****@163.com";    $mail->FromName   = "First Last";    $to = "309407***@qq.com";    $mail->AddAddress($to);    $mail->Subject  = "First PHPMailer Message";    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test    $mail->WordWrap   = 80; // set word wrap    $mail->MsgHTML($body);    $mail->IsHTML(true); // send as HTML    $mail->Send();    echo ‘Message has been sent.‘;} catch (phpmailerException $e) {    echo $e->errorMessage();}?>

 

用PHP发送邮件, 可以用PHPMailer。PHPMailer是一个用于发送电子邮件的PHP函数包那么PHPMailer怎么用呢?第一步当然是下载PHPMailer包,有两个版本 、php4版本和php5/6版本的 下载自己合适的下载完以后 ,看到里面有很多文件 , 其实大部分都用不到然后进入 test看到testemail.php 文件 这里面是一个例子代码类似<?php/*** Simple example script using PHPMailer with exceptions enabled* @package phpmailer* @version $Id$*/require ‘../class.phpmailer.php‘;try {$mail = new PHPMailer(true); //New instance, with exceptions enabled$body = file_get_contents(‘contents.html‘); // 发送的内容 $body = preg_replace(‘/\\\\/‘,‘‘, $body); //Strip backslashes$mail->IsSMTP(); // tell the class to use SMTP$mail->SMTPAuth = true; // enable SMTP authentication$mail->Port = 25; // set the SMTP server port$mail->Host = "smtp.163.com"; // SMTP server$mail->Username = "yinxiaoluan110@163.com"; // SMTP server username$mail->Password = "*********"; // 填写你自己邮箱的密码//$mail->IsSendmail(); // tell the class to use Sendmail$mail->AddReplyTo("yinxiaoluan110@163.com","First Last");$mail->From = "yinxiaoluan110@163.com"; //发件人邮箱$mail->FromName = “小白"; //发件人 $to = "745888750@qq.com"; //收件人 $mail->AddAddress($to);$mail->Subject = "First PHPMailer Message";$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test$mail->WordWrap = 80; // set word wrap$mail->MsgHTML($body);$mail->IsHTML(true); // send as HTML$mail->Send();echo ‘Message has been sent.‘;} catch (phpmailerException $e) {echo $e->errorMessage();}?>上面代码 ,有注释说明的比较重要 需要认真填写一下有不懂SMTP服务器的 可以参考一下 wordpress发送解决方法 这篇文章注意:1.如果报错 :Could not execute: /var/qmail/bin/sendmail那么你需要把 配置文件中的 $mail->IsSendmail(); 注释掉2.如果你用的是空间报错:SMTP Error: Could not connect to SMTP host那么你需要 修改class.smtp.php$this->smtp_conn = @fsockopen改为$this->smtp_conn = @pfsockopen因为很多空间把fsockopen禁用了!按照以上步骤 , 您的邮件应该已经发送成功了其实 phpmailer 包我们真正用到的文件只有 class.phpmailer.php和class.smtp.php 

 

 

<?php/*** Simple example script using PHPMailer with exceptions enabled* @package phpmailer* @version $Id$*/require_once‘/class.phpmailer.php‘;require_once‘/class.smtp.php‘; try {    $mail = new PHPMailer(true); //New instance, with exceptions enabled    //$body             = file_get_contents(‘contents.html‘);    //$body             = preg_replace(‘/\\\\/‘,‘‘, $body); //Strip backslashes    $mail->IsSMTP();                           // tell the class to use SMTP    $mail->SMTPAuth   = true;                  // enable SMTP authentication    $mail->Port       = 25;                    // set the SMTP server port    $mail->Host       = "smtp.163.com"; // SMTP server    $mail->Username   = "1585358****@163.com";     // SMTP server username    $mail->Password   = "**********";            // SMTP server password    //$mail->IsSendmail();  // tell the class to use Sendmail    $mail->AddReplyTo("1585358****@163.com","First Last");    $mail->From       = "1585358****@163.com";    $mail->FromName   = "First Last";    $to = "******@qq.com";    $mail->AddAddress($to);    $mail->Subject  = "First PHPMailer Message";    $mail->Body="这是我的测试邮件,the test email";    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test    $mail->WordWrap   = 80; // set word wrap    //$mail->CharSet  = "UTF-8"; //字符集    //$mail->MsgHTML($body);    $mail->IsHTML(true); // send as HTML    $mail->Send();    echo ‘Message has been sent.‘;} catch (phpmailerException $e) {    echo $e->errorMessage();}?>
<?phprequire("class.phpmailer.php"); //下载的文件必须放在该文件所在目录$mail = new PHPMailer(); //建立邮件发送类$address = $_POST[‘address‘];$mail->IsSMTP(); // 使用SMTP方式发送$mail->CharSet=‘UTF-8‘;// 设置邮件的字符编码$mail->Host = "mail.xxxxx.***"; // 您的企业邮局域名$mail->SMTPAuth = true; // 启用SMTP验证功能$mail->Port = "***"; //SMTP端口$mail->Username = ***@xxxx.***; // 邮局用户名(请填写完整的email地址)$mail->Password = "******"; // 邮局密码$mail->From = ***@xxxx.***; //邮件发送者email地址$mail->FromName = "您的名称";$mail->AddAddress("$address", "");//收件人地址,可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名")//$mail->AddReplyTo("", "");//$mail->AddAttachment("/var/tmp/file.tar.gz"); // 添加附件//$mail->IsHTML(true); // set email format to HTML //是否使用HTML格式$mail->Subject = "PHPMailer测试邮件"; //邮件标题$mail->Body = "Hello,这是测试邮件"; //邮件内容$mail->AltBody = "This is the body in plain text for non-HTML mail clients"; //附加信息,可以省略if(!$mail->Send()){echo "邮件发送失败. <p>";echo "错误原因: " . $mail->ErrorInfo;exit;}echo "邮件发送成功";?>                             

我的PHPMailer_v5.1 使用