先天布局法 發表於 2019-5-28 17:15:00

php使用邮箱发送验证码

<p><span style="font-size: 18px">如果看着文字眼乏就去看看视频吧-&gt;</span></p>
<p><span style="font-size: 18px">如何注册腾讯企业邮箱</span></p>
<p><span style="font-size: 18px">https://www.bilibili.com/video/av14351397/</span></p>
<p><span style="font-size: 18px">如何在项目中使用</span></p>
<p><span style="font-size: 18px">https://www.bilibili.com/video/av14351961/</span></p>
<p><span style="font-size: 18px">下面是我自己在采坑的过程中遇到的问题</span></p>
<p><span style="font-size: 18px">首先我们下载PHPMailer的,下载地址:https://github.com/PHPMailer/PHPMailer</span></p>
<p><span style="font-size: 18px">将文件部入到我们的TP框架中,放到extend/org中。</span></p>
<p><span style="font-size: 18px">第一步我们应该准备发送邮箱的账号,我这里用的是腾讯企业邮箱</span></p>
<p><span style="font-size: 18px">没有的可以直接注册一个:https://exmail.qq.com</span></p>
<p><span><span style="font-size: 18px">将类文件PHPMailer.php第1760行的方法里的if判断中添加<span style="color: rgba(0, 0, 255, 1)">include_once 'SMTP.php'<span style="color: rgba(0, 0, 0, 1)">;</span></span></span></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 128, 128, 1)">is_object</span>(<span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">smtp)) {
</span><span style="color: rgba(0, 0, 255, 1)">include_once</span> 'SMTP.php'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;smtp=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SMTP();
}</span></pre>
</div>
<p>&nbsp;</p>
<p><span style="font-size: 18px">&nbsp;在控制器里面代码</span></p>
<div class="cnblogs_code">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">php
namespace app\api\controller;
</span><span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> PHPMailer\PHPMailer\PHPMailer;
</span><span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> PHPMailer\PHPMailer\SMTP;
</span><span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> think\Controller;
</span><span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> think\Loader;
</span><span style="color: rgba(0, 0, 255, 1)">class</span> Mailsms<span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> Controller
{
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">发送商,,,QQ企业邮箱地址都是smtp.exmail.qq.com</span>
    <span style="color: rgba(0, 0, 255, 1)">const</span> HOST='smtp.exmail.qq.com'<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">发件人邮箱....企业邮箱中一个成员的账号</span>
    <span style="color: rgba(0, 0, 255, 1)">const</span> USERNAME='youname@youyuming.top'<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">发件箱授权码。。。。。。这里是成员账号的登录密码</span>
    <span style="color: rgba(0, 0, 255, 1)">const</span> PASSWORD='passworld'<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">发送模板</span>
    <span style="color: rgba(0, 0, 255, 1)">const</span> TEMPLATE='您的验证码是{code},请在10分钟内填写。'<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">是否debug 测试环境使用</span>
    <span style="color: rgba(0, 0, 255, 1)">const</span> DEBUG=<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">实例</span>
    <span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(128, 0, 128, 1)">$mail</span><span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> _initialize(){
      Loader</span>::import("org.PHPMailer.src.Exception",EXTEND_PATH,".php"<span style="color: rgba(0, 0, 0, 1)">);
    Loader</span>::import("org.PHPMailer.src.PHPMailer",EXTEND_PATH,".php"<span style="color: rgba(0, 0, 0, 1)">);
    Loader</span>::import("org.PHPMailer.src.SMTP",EXTEND_PATH,".php"<span style="color: rgba(0, 0, 0, 1)">);</span>
       <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 128, 128, 1)">mail</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> PHPMailer();
    }
    </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * target 发送给谁 收件人的邮箱
   * subject 主题
   * code 验证码apiMailsms/index
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span> sendMail(<span style="color: rgba(128, 0, 128, 1)">$target</span>,<span style="color: rgba(128, 0, 128, 1)">$subject</span>,<span style="color: rgba(128, 0, 128, 1)">$code</span><span style="color: rgba(0, 0, 0, 1)">)
    {
      </span><span style="color: rgba(128, 0, 128, 1)">$content</span> = <span style="color: rgba(0, 128, 128, 1)">str_replace</span>('{code}',<span style="color: rgba(128, 0, 128, 1)">$code</span>,self::<span style="color: rgba(0, 0, 0, 1)">TEMPLATE);
      </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{
            </span><span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 128, 128, 1)">mail</span>-&gt;SMTPDebug=self::<span style="color: rgba(0, 0, 0, 1)">DEBUG;
            </span><span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 128, 128, 1)">mail</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">isSMTP();
            </span><span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 128, 128, 1)">mail</span>-&gt;Host=self::HOST;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">服务器</span>
            <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 128, 128, 1)">mail</span>-&gt;SMTPAuth=<span style="color: rgba(0, 0, 255, 1)">true</span>;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">是否开启SMTP认证</span>
            <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 128, 128, 1)">mail</span>-&gt;Username=self::USERNAME;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">发件人邮箱</span>
            <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 128, 128, 1)">mail</span>-&gt;Password=self::PASSWORD;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">发件箱授权码
         // $this-&gt;mail-&gt;SMTPSecure='tls';//启用TLS加密,' ssl '也接受
          //$this-&gt;mail-&gt;Port='25';//端口<br>      //注意使用阿里域名的需要使用ssl加密,465端口,</span>
            <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 128, 128, 1)">mail</span>-&gt;SMTPSecure='ssl';<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">启用TLS加密,' ssl '也接受</span>
            <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 128, 128, 1)">mail</span>-&gt;Port='465';<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">端口</span>
            <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 128, 128, 1)">mail</span>-&gt;CharSet='UTF-8';<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置邮件内容编码</span>

            <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 128, 128, 1)">mail</span>-&gt;setFrom(self::USERNAME); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">发件人
            //$this-&gt;mail-&gt;addAttachment('/var/tmp/file.tar.gz');//附件
            //$this-&gt;mail-&gt;addAttachment('/tmp/image.jpg', 'new.jpg');//附件重命名</span>
            <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 128, 128, 1)">mail</span>-&gt;addAddress(<span style="color: rgba(128, 0, 128, 1)">$target</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">发送给谁</span>
            <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 128, 128, 1)">mail</span>-&gt;isHTML(<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 128, 128, 1)">mail</span>-&gt;Subject=<span style="color: rgba(128, 0, 128, 1)">$subject</span><span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 128, 128, 1)">mail</span>-&gt;Body=<span style="color: rgba(128, 0, 128, 1)">$content</span><span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">$this-&gt;mail-&gt;AltBody ='This is the body in plain text for non-HTML mail clients';</span>
            <span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 128, 128, 1)">mail</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">send()){
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> "发送成功!"<span style="color: rgba(0, 0, 0, 1)">;
            }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{
                </span><span style="color: rgba(0, 0, 255, 1)">echo</span> "发送失败!"<span style="color: rgba(0, 0, 0, 1)">;
            }
      }</span><span style="color: rgba(0, 0, 255, 1)">catch</span>(<span style="color: rgba(0, 0, 255, 1)">Exception</span> <span style="color: rgba(128, 0, 128, 1)">$e</span><span style="color: rgba(0, 0, 0, 1)">) {
            </span><span style="color: rgba(0, 0, 255, 1)">echo</span> "异常抛出"<span style="color: rgba(0, 0, 0, 1)">;
      }
    }
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> index(){
    </span><span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;sendMail('收件人的邮箱','邮箱标题',自定义数据<span style="color: rgba(0, 0, 0, 1)">);
}
}</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><span><span style="font-size: 18px">可以看出连接超时导致邮件发送失败,初步怀疑测试环境宿主机无法访问外网,进入bash后 curl http://baidu.com可正常响应,排除此种情况,然后同样方法到部署的具体容器环境(docker)控制台&nbsp;curl http://baidu.com 同样可以正常响应,判断应用环境没有问题,则判断是否腾讯邮件服务器问题,到宿主机访问:Telnet smtp.exmail.qq.com 25,访问超时。后咨询PE答复阿里云ECS为管控垃圾邮件,屏蔽了25端口号的服务,所以导致该访问超时,尝试使用ssl方式的465端口,修改代码如下之后成功发送邮件:</span><br><br></span></p>
<p><span style="font-size: 18px"><span style="color: rgba(255, 0, 0, 1); background-color: rgba(255, 255, 255, 1)">邮件直接进入垃圾箱原因:</span><br>1. 以触发式的过滤算法鉴别垃圾邮件<br>
这种垃圾邮件过滤器通常安装在电子邮件客户端软件或者邮件服务器上,其过滤垃圾邮件原理是过滤软件检查邮件发送人、标题、正文内容、邮件中出现的链接和域名,甚至电话号码。当发现带明显垃圾邮件的典型特征,则给予这封邮件一定的垃圾邮件特征分数。当分数达到一定数值,邮件将被标记为垃圾邮件,直接过滤到邮件垃圾箱。<br> 比如,邮件标题中出现¥、$符号,则可以给予2分垃圾邮件分数;邮件内容中出现“免费”、“发票”、“促销”等典型垃圾邮件词汇,给予1分;邮件中如包含已经被确认为经常发送垃圾邮件的域名,再加1分;甚至邮件内容中出现被确认与垃圾邮件相关的电话号码,也给个分数。当这些垃圾分数相加达到某一个数值时,比如达到10分,这封邮件将被标识为垃圾邮件,将会被ISP商丢进垃圾邮箱<br>2. 以黑名单为基础<br>

有些创建和维护邮件黑名单的组织,专门接受用户的垃圾邮件投诉,如果确认是垃圾邮件,黑名单管理者将把发送垃圾邮件的服务器和用户IP地址放入黑名单。比较知名的垃圾邮件黑名单通常都与其他ISP商共享黑名单数据库。一旦某个IP地址被列入黑名单,世界上很多ISP都将拒收来自这个IP地址的所有邮件。<br>

有时候用户投诉,其实收到的邮件并不是垃圾邮件,而是用户忘记了曾经注册过相关电子杂志。如果你的IP地址被错误的投诉而列入黑名单,Jeasin.com建议您,唯一的办法就是联系黑名单维护组织,说明情况,提出证据,要求把你的IP地址从黑名单中删除,此过程比较复杂困难。<br>3. 邮件防火墙<br>

很多邮件服务器运行在邮件防火墙之后,这些防火墙会共同地使用各种过滤器和黑名单,再加上自行研究的一些算法,来鉴别和剔除垃圾邮件。这些防火墙的算法更复杂,并且不与他人分享细节,对正常的邮件送达也可能有致命的影响,如部分邮件服务器要求有人工添加确认功能,针对大部分陌生地址邮件,华思邮件告诉各位我们应将采用拒收的策略。</span></p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/ql-smile/p/10938680.html
頁: [1]
查看完整版本: php使用邮箱发送验证码