• PHP (sendmail / PHPMailer / ezcMailComposer)发送邮件


    一. 使用 PHP 内置的 mail() 函数

    1. Windows 下

    环境:WampServer2.5(Windows 10,Apache 2.4.9,MySQL 5.6.17,PHP 5.5.12)
    

    ① 在 Windows 下使用 PHP 内置的 mail() 函数发送邮件,需要先安装 sendmail(下载地址:http://glob.com.au/sendmail/

    把下载下来的 sendmail.zip 解压到自定义的目录(我这里是 D:wampin)

    ② 配置 php.ini 文件(通过 phpinfo 确定 ph.ini 文件真实路径)

    邮件服务器以腾讯邮箱为例,php.ini 文件主要配置

    SMTP = smtp.qq.com

    smtp_port = 25(邮件服务端口),

    sendmail_path = "D:wampinsendmailsendmail.exe -t"

    ③ 配置 sndmail.ini

    需要配置:

    smtp_server=smtp.qq.com
    smtp_port=25
    
    ,开启 log 方便排错,生成的log文件在sendmail根目录
    error_logfile=error.log 
    debug_logfile=debug.log
    
    auth_username=472323087@qq.com
    auth_password=你的授权码
    ,force_sender 要和auth_username一致
    force_sender=472323087@qq.com
    

    说明:

    配置项中的 auth_password 不是邮箱的密码, 而是腾讯邮箱的第三方客户端授权码,获取授权码的方式是:

    登陆 mail.qq.com(472323087@qq.com),选择“设置” -- “POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务” -- “生成授权码”

     

    点击“生成授权码”,需要发送一条验证信息,验证通过之后得到授权码

    ④ 测试发送邮件:

    <?php
    
    // 使用 PHP 内置的 mail() 函数
    
    $to = '472323087@qq.com';
    $subject = 'Hello World!';
    $body = 'Welcome to China!';
    mail($to, $subject, $body);
    

    收到邮件:

      

    2. Linux 下

    环境:LNMP(CentOS 6.6 ,Nginx 1.8.0,MySQL 5.6.23,PHP 5.6.9)
    

      

    ① 安装 sendmail

    # yum install sendmail
    

    ② 启动 sendmail

    # /etc/rc.d/init.d/sendmail start
    

    ③ 此时可以直接通过 mail 命令来给指定邮箱发送邮件:

    [root@localhost ~]# echo 'this is a mail test'|mail -s text dee1566@126.com
    

    这里先用 126 邮箱举例,腾讯邮箱由于腾讯邮件服务器的限制,不做设置很容造成拒收,后面再说。

      

    打开邮件:

      

    ④ 使用 PHP 的 mail() 函数发送邮件

    需要修改 php.ini 

    smtp_port = 25
    sendmail_path = /usr/sbin/sendmail -t
    

    SMTP 不用设置

    mail.php

    <?php
    
    header('Content-type:text/html;charset=utf-8');
    
    // 使用 PHP 内置的 mail() 函数
    
    $to = 'dee1566@126.com';
    $subject = 'Hello World!';
    $body = 'Welcome to China!';
    if(mail($to, $subject, $body)) {
    	echo '发送成功';
    } else {
    	echo '发送失败';
    }
    

    收到邮件:

      

    打开邮件:

      

    说明:

    mail("接受方email", "邮件主题", "正文内容", headers, "from:发送方email");
    

    要修改发件人,可以添加第四个参数

    <?php
    
    header('Content-type:text/html;charset=utf-8');
    
    // 使用 PHP 内置的 mail() 函数
    
    $to = (isset($_GET['type']) && $_GET['type'] == 'qq') ? '472323087@qq.com' : 'dee1566@126.com';
    $subject = 'Hello World!';
    $body = 'Welcome to China!';
    
    $headers   = array();
    $headers[] = "MIME-Version: 1.0";
    $headers[] = "Content-type: text/plain; charset=iso-8859-1";
    $headers[] = "From: dee <472323087@qq.com>";
    
    $from = '472323087@qq.com';
    
    if(mail($to, $subject, $body, implode("
    ", $headers), $from)) {
    	echo '发送成功';
    } else {
    	echo '发送失败';
    }
    

    此时收到的邮件:

      

    打开邮件:

      

    ⑤ 如果接收方 email 是腾讯邮箱的话,很容易显示发送成功但是实际上根本就没有发送成功,通过查看日志

    [root@localhost sbin]# tail -f /var/spool/mail/root
    

    可能会出现

    <<< 550 Mail content denied. http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=20022&&no=1000726
    554 5.0.0 Service unavailable
    

    550 Mail content denied 出错原因:该邮件内容涉嫌大量群发,并且被多数用户投诉为垃圾邮件

    编辑  /etc/mail.rc

    [root@localhost ~]# vim /etc/mail.rc
    

    添加:

    set from=472323087@qq.com  smtp=smtp.qq.com           //邮件来自  
    set smtp-auth-user=472323087@qq.com smtp-auth-password=你的授权码 smtp-auth=login       //登录qq SMTP服务器的用户名和密码 
    

    此时直接使用 mail 命令发送邮件:

    [root@localhost ~]# echo 'this is a mail test'|mail -s title 472323087@qq.com
    

    但还是不能使用 PHP 的 mail 函数给腾讯邮箱发送邮件,查了很多资料,还是没能解决。

    总结:

    在 Windows 下使用 sendmail 结合 mail() 函数能很容易地给腾讯邮箱发邮件,在 Linux 下腾讯邮箱几乎一律拒收,连垃圾箱都进不了,其他的比如 126 邮箱就没有问题,可能还是和主机设置有关。

    最终代码:

    <?php
    
    header('Content-type:text/html;charset=utf-8');
    
    // 使用 PHP 内置的 mail() 函数
    
    $to = 'dee1566@126.com';
    $subject = 'Hello World!';
    $body = 'Welcome to China!';
    
    $headers   = array();
    $headers[] = "MIME-Version: 1.0";
    $headers[] = "Content-type: text/plain; charset=iso-8859-1";
    $headers[] = "From: dee <472323087@qq.com>"; //决定邮件的发件人显示
    
    $from = '472323087@qq.com';
    
    if(mail($to, $subject, $body, implode("
    ", $headers), $from)) {
    	echo '发送成功';
    } else {
    	echo '发送失败';
    }
    

      

      

    二. 使用 Zeatcomponent 的 ezcMailComposer 类

    文档地址:http://ezcomponents.org/docs/tutorials/Mail 

    下载地址:http://ezcomponents.org/download

    下载后解压压缩包

    Zetacomponent ezcMailComposer 类可以与 SMTP 服务器直接通信:

    <?php
    
    header('Content-type:text/html;charset=utf-8');
    
    // 使用 Zeccomponent 的 ezcMailComposer 类
    // http://ezcomponents.org/docs/tutorials/Mail
    
    require_once 'ezcomponents/Mail/docs/tutorial/tutorial_autoload.php';
    
    $message = new ezcMailComposer();
    $message->from = new ezcMailAddress('47232087', 'dee'); //发送邮箱是qq邮箱,例如472323087@qq.com,邮件很容易被拒,sendmail根目录下error.log中错误记录是:Error: content rejected.http://mail.qq.com/zh_CN/help/content/rejectedmail.html<EOL>
    $message->addTo(new ezcMailAddress('472323087@qq.com', 'emperor'));
    $message->subject = 'php sendmail';
    $body = 'this is a test mail';
    $message->plainText = $body;
    $message->build();
    
    $host = 'smtp.qq.com';
    $username = '472323087@qq.com';
    $password = 'niwogqkejpnzbibh';
    $port = '25';
    
    $smtpOptions = new ezcMailSmtpTransportOptions();
    $smtpOptions->preferredAuthMethod = ezcMailSmtpTransport::AUTH_LOGIN;
    
    $sender = new ezcMailMtaTransport($host, $username, $password, $port, $smtpOptions);
    try {
    	$sender->send($message);
    	echo '发生成功';
    } catch(ezcMailTransportException $e) {
    	echo $e->getMessage();
    }
    

    收到邮件:

      

    打开邮件:

    同样在 Linux 下同样会遇到腾讯邮箱直接拒收的问题。

    三. 使用 PHPMailer 类

    PHPMailer 版本 5.2.13

    下载地址:https://github.com/Synchro/PHPMailer

    Windows 下调试代码:

    <?php
    
    header("content-type:text/html;charset=utf-8"); 
    
    require 'PHPMailer/class.smtp.php';
    require 'PHPMailer/class.phpmailer.php';
    
    try { 
    	$mail = new PHPMailer(true); 
    	$mail->IsSMTP(); 
    	$mail->CharSet='UTF-8'; //设置邮件的字符编码,这很重要,不然中文乱码 
    	$mail->SMTPAuth = true; //开启认证 
    	$mail->Port = 25; 
    	$mail->Host = "smtp.qq.cn"; 
    	$mail->Username = "472323087@qq.com"; 
    	$mail->Password = "你的授权码"; 
    	$mail->IsSendmail(); //windows下开启;linux下如果没有sendmail组件就注释掉,否则出现“Could not execute: /usr/sbin/sendmail”的错误提示 
    	$mail->AddReplyTo("472323087@qq.com","dee");//回复地址 
    	$mail->From = "472323087@qq.com"; 
    	$mail->FromName = "472323087@qq.com"; 
    	$to = "472323087@qq.com"; 
    	$mail->AddAddress($to); 
    	$mail->Subject = "phpmailer测试标题"; 
    	$mail->Body = "<h1>phpmail演示</h1>这是emperor对phpmailer的测试内容"; 
    	$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; //当邮件不支持html时备用显示,可以省略 
    	$mail->WordWrap = 80; // 设置每行字符串的	长度 
    	//$mail->AddAttachment("d:/test.jpg"); //可以添加附件 
    	$mail->IsHTML(true); 
    	$mail->Send(); 
    	echo '邮件已发送'; 
    } catch (phpmailerException $e) { 
    	echo "邮件发送失败:".$e->errorMessage(); 
    } 
    

      

    Linux 下修改 php.ini,注释 

    sendmail_path = /usr/sbin/sendmail -t -i

    把程序中的

    $mail->IsSendmail();
    

    也注释,可以完成包括对腾讯邮箱的邮件任务。

    注意,邮件服务器尽量不要选腾讯邮箱。

     

    参考:

    PHP 在windows下配置sendmail,通过 mail() 函数发送邮件

    Linux系统PHP使用sendmail发送邮件

    linux用mail往qq邮箱发邮件

    Sendmail.mc 配置文件详解

    PHPMailer使用教程(PHPMailer发送邮件实例分析)

  • 相关阅读:
    PHP 中各种命名规则的总结
    Linux 下安装mysql 8.0.11(CentOS 7.4 系统)
    常用端口号(转)
    Centos7 FPM 安装mysql8
    windows 下升级安装mysql8,与旧版本5.6共存
    【Eclipse】修改项目访问名称
    【Eclipse】报错提示删掉@Override
    【霓虹语】バスの火事
    【博客园】使用ifream的两种方法
    【Eclipse】调试java程序的九个技巧
  • 原文地址:https://www.cnblogs.com/dee0912/p/5448350.html
Copyright © 2020-2023  润新知