php中mail函数发送邮件失利的化解措施

PHP SMTP发送邮件函数使用方法 sendmail(‘发送地址’, ‘邮件焦点’,
‘邮件内容’)代码来源:

本机景况:LAMP(ubuntu1二.十);

正文实例讲述了php中mail函数发送邮件战败的缓慢解决措施。分享给我们供大家参照他事他说加以调查。具体分析如下:

复制代码 代码如下:

[PHP]代码

SMTP服务器用的是stmp.1陆叁.com。刚起始对于那么些依然蛋疼的,初阶使用的是stmp.qq.com结果发的邮件都被Tencent真是垃圾邮件管理了发不出去,

php中mail函数是三个自带的发邮件的函数,可是只要您确实要选拔mail函数来发邮件这必须是要给您系统安装sendmail组件本事够,上边小编就碰见mail发送不了邮件的难题,上边一齐来探视解决办法吧.

[mail function]
; For Win32 only.
SMTP = mail3.focuschina.com
smtp_port = 25

$bfconfig = Array (
    'sitename' => '网站名称',
    );

$mail = Array (
    'state' => 1,
    'server' => 'smtp.abc.com',
    'port' => 25,
    'auth' => 1,
    'username' => 'admin@abc.com',
    'password' => '123456',
    'charset' => 'gbk',
    'mailfrom' => 'admin@abc.com'
    );

function sendmail($mail_to, $mail_subject, $mail_message) {

    global $mail, $bfconfig;

    date_default_timezone_set('PRC');

    $mail_subject = '=?'.$mail['charset'].'?B?'.base64_encode($mail_subject).'?=';
    $mail_message = chunk_split(base64_encode(preg_replace("/(^|(\r\n))(\.)/", "\1.\3", $mail_message)));

    $headers .= "";
    $headers .= "MIME-Version:1.0\r\n";
    $headers .= "Content-type:text/html\r\n";
    $headers .= "Content-Transfer-Encoding: base64\r\n";
    $headers .= "From: ".$bfconfig['sitename']."<".$mail['mailfrom'].">\r\n";
    $headers .= "Date: ".date("r")."\r\n";
    list($msec, $sec) = explode(" ", microtime());
    $headers .= "Message-ID: <".date("YmdHis", $sec).".".($msec * 1000000).".".$mail['mailfrom'].">\r\n";

    if(!$fp = fsockopen($mail['server'], $mail['port'], $errno, $errstr, 30)) {
        exit("CONNECT - Unable to connect to the SMTP server");
    }

    stream_set_blocking($fp, true);

    $lastmessage = fgets($fp, 512);
    if(substr($lastmessage, 0, 3) != '220') {
        exit("CONNECT - ".$lastmessage);
    }

    fputs($fp, ($mail['auth'] ? 'EHLO' : 'HELO')." befen\r\n");
    $lastmessage = fgets($fp, 512);
    if(substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250) {
        exit("HELO/EHLO - ".$lastmessage);
    }

    while(1) {
        if(substr($lastmessage, 3, 1) != '-' || empty($lastmessage)) {
            break;
        }
        $lastmessage = fgets($fp, 512);
    }

    if($mail['auth']) {
        fputs($fp, "AUTH LOGIN\r\n");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 334) {
            exit($lastmessage);
        }

        fputs($fp, base64_encode($mail['username'])."\r\n");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 334) {
            exit("AUTH LOGIN - ".$lastmessage);
        }

        fputs($fp, base64_encode($mail['password'])."\r\n");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 235) {
            exit("AUTH LOGIN - ".$lastmessage);
        }

        $email_from = $mail['mailfrom'];
    }

    fputs($fp, "MAIL FROM: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $email_from).">\r\n");
    $lastmessage = fgets($fp, 512);
    if(substr($lastmessage, 0, 3) != 250) {
        fputs($fp, "MAIL FROM: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $email_from).">\r\n");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 250) {
            exit("MAIL FROM - ".$lastmessage);
        }
    }

    foreach(explode(',', $mail_to) as $touser) {
        $touser = trim($touser);
        if($touser) {
            fputs($fp, "RCPT TO: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $touser).">\r\n");
            $lastmessage = fgets($fp, 512);
            if(substr($lastmessage, 0, 3) != 250) {
                fputs($fp, "RCPT TO: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $touser).">\r\n");
                $lastmessage = fgets($fp, 512);
                exit("RCPT TO - ".$lastmessage);
            }
        }
    }

    fputs($fp, "DATA\r\n");
    $lastmessage = fgets($fp, 512);
    if(substr($lastmessage, 0, 3) != 354) {
        exit("DATA - ".$lastmessage);
    }

    fputs($fp, $headers);
    fputs($fp, "To: ".$mail_to."\r\n");
    fputs($fp, "Subject: $mail_subject\r\n");
    fputs($fp, "\r\n\r\n");
    fputs($fp, "$mail_message\r\n.\r\n");
    $lastmessage = fgets($fp, 512);
    if(substr($lastmessage, 0, 3) != 250) {
        exit("END - ".$lastmessage);
    }

    fputs($fp, "QUIT\r\n");

}

怎么该内容都没过。然后自个儿改成了stmp.gmail.com,然后张开谷歌(谷歌)邮箱的POP服务,但总不可能表达成功。最终仍旧注册三个1陆3,运营代码就成功了。

事先的那台服务器转移过来后,发掘网址用mail()发送邮件发不了,可是开采sendmail明明已经安装,要是未有安装sendmail能够实践,代码如下:

; For Win32 only.
;sendmail_from = me@example.com

无痛苦,无压力~~

复制代码 代码如下:

只是配置了上述的代码,未有利用用户名和密码,测试代码成功发送邮件
好读书,不求甚解。。以上

代码如下:

yum install sendmail

发送的测试代码:

复制代码 代码如下:

对了,主机名要设置2个域名格式的,举个例子:jb5一.net,不然重启sendmail的时候会专程久才起步起来,不然要等很久,代码如下:

复制代码 代码如下:

<?php
 require(“PHPMailer/class.phpmailer.php”);
 require(“PHPMailer/class.smtp.php”); 

复制代码 代码如下:

<?php

发表评论

电子邮件地址不会被公开。 必填项已用*标注