zoukankan      html  css  js  c++  java
  • 夹缝中求生存-在一无所有的php虚拟主机环境下利用smtp发送邮件(二)

    夹缝中求生存

    前言:在上一篇随笔中,以163个人邮箱作为发送邮箱地址,当收件邮箱为QQ邮箱时,极有可能会被直接扔进邮件垃圾箱里,为了解决这个问题,申请注册企业邮箱,可以减少发出的邮件被当作垃圾邮件的可能性。本文以腾讯企业邮箱为例。

    • 既然是企业邮箱,首先前提是你得有一个自己的域名。然后就可以去申请免费的腾讯企业邮箱账户,传送门
    • 按照免费腾讯企业邮箱的申请要求,验证域名所有权后在域名提供商处设置好MX解析。下图是我在阿里云的域名设置。
    • 设置好网站邮箱管理员后,可添加一个成员账户,比如命名为service@xxx.com(xxx.com是你的域名),该邮箱地址将作为对外发邮件的账户。
    • 记得要确保设置好开启该邮箱的账户smtp服务,邮箱->客户端设置里设置。
    • 下面就可以愉快的玩耍了。

    mail.php

     1 <?php
     2 function send_mail($name, $to, $subject = 'No subject', $body) {
     3     $loc_host = "ooo";                 //主机名,随意
     4     $smtp_acc = "service@XXX.com";        //你的邮箱
     5     $smtp_pass="XXXXX";              //邮箱登陆密码,不是授权码,163个人邮箱才是授权码。
     6     $smtp_host="smtp.exmail.qq.com";    //腾讯企业邮箱地址
     7     $from="service@xxx.com";              //你的邮箱
     8     $headers = "Content-Type: text/html; charset="utf-8"
    Content-Transfer-Encoding: base64";
     9     $lb="
    ";                         //linebreak
    10     $hdr = explode($lb,$headers);     
    11     if($body) {
    12         $bdy = preg_replace("/^./","..",explode($lb,$body));
    13     }
    14 
    15     $smtp = array(
    16 
    17         array("EHLO ".$loc_host.$lb,"220,250","HELO error: "),
    18 
    19         array("AUTH LOGIN".$lb,"334","AUTH error:"),
    20 
    21         array(base64_encode($smtp_acc).$lb,"334","AUTHENTIFICATION error : "),
    22 
    23         array(base64_encode($smtp_pass).$lb,"235","AUTHENTIFICATION error : ")
    24     );
    25 
    26     $smtp[] = array("MAIL FROM: <".$from.">".$lb,"250","MAIL FROM error: ");
    27 
    28     $smtp[] = array("RCPT TO: <".$to.">".$lb,"250","RCPT TO error: ");
    29 
    30     $smtp[] = array("DATA".$lb,"354","DATA error: ");
    31 
    32     $smtp[] = array("From: ".$name."<".$from.">".$lb,"","");
    33 
    34     $smtp[] = array("To: ".$to.$lb,"","");
    35 
    36     $smtp[] = array("Subject: ".$subject.$lb,"","");
    37 
    38     foreach($hdr as $h) {$smtp[] = array($h.$lb,"","");}
    39 
    40     $smtp[] = array($lb,"","");
    41 
    42     if($bdy) {foreach($bdy as $b) {$smtp[] = array(base64_encode($b.$lb).$lb,"","");}}
    43 
    44     $smtp[] = array(".".$lb,"250","DATA(end)error: ");
    45 
    46     $smtp[] = array("QUIT".$lb,"221","QUIT error: ");
    47 
    48 
    49     $fp = @fsockopen($smtp_host, 25);
    50     if (!$fp) echo "Error: Cannot conect to ".$smtp_host."";
    51     while($result = @fgets($fp, 1024)){
    52         if(substr($result,3,1) == " ") { break; }
    53     }
    54 
    55     $result_str="";
    56 
    57     foreach($smtp as $req){
    58 
    59         @fputs($fp, $req[0]);
    60 
    61         if($req[1]){
    62 
    63             while($result = @fgets($fp, 1024)){
    64                 if(substr($result,3,1) == " ") { break; }
    65             };
    66             if (!strstr($req[1],substr($result,0,3))){
    67                 $result_str.=$req[2].$result."";
    68             }
    69         }
    70     }
    71 
    72     @fclose($fp);
    73     return 1;
    74 }
    75 
    76 
    77 ?>

    send.php

    1 <?php
    2 header("Content-Type: text/html; charset=utf-8");
    3 include_once("mail.php");
    4 send_mail("发件人", "收件邮箱","邮件标题"],"内容");
    5 echo "<h1>邮件已发送</h1>";
    6 ?>
  • 相关阅读:
    [模板] 文艺平衡树
    [模板]平衡树splay
    [ZJOI2015]诸神眷顾的幻想乡
    [HAOI2016]找相同字符
    SP8093 JZPGYZ
    SP1812 LCS2
    SP1811 LCS
    AHOI2013 差异
    TJOI2015 弦论
    工艺(SAM)
  • 原文地址:https://www.cnblogs.com/scrazy/p/7643659.html
Copyright © 2011-2022 走看看