我的邮件功能有什么问题?

时间:2014-03-17 05:58:43

标签: php email smtp gmail

我从开源的punbb论坛软件中获取了一个邮件功能。

这是:

function smtp_mail($to, $subject, $message, $headers, $username, $password, $host, $use_ssl){
    global $forum_config;

    $recipients = explode(',', $to);

    // Sanitize the message
    $message = str_replace("\r\n.", "\r\n..", $message);
    $message = (substr($message, 0, 1) == '.' ? '.'.$message : $message);

    // Are we using port 25 or a custom port?
    if (strpos($host, ':') !== false){
        list($smtp_host, $smtp_port) = explode(':', $host);
    }else{
        $smtp_host = $host;
        $smtp_port = 25;
    }

    if ($use_ssl == '1'){
        $smtp_host = 'ssl://'.$smtp_host;
    }

    if (!($socket = fsockopen($smtp_host, $smtp_port, $errno, $errstr, 15))){
        error('Could not connect to smtp host "'.$host.'" ('.$errno.') ('.$errstr.').', __FILE__, __LINE__);
    }

    server_parse($socket, '220');

    if ($username != '' && $password != ''){
        fwrite($socket, 'EHLO '.$smtp_host."\r\n");
        server_parse($socket, '250');

        fwrite($socket, 'AUTH LOGIN'."\r\n");
        server_parse($socket, '334');

        fwrite($socket, base64_encode($username)."\r\n");
        server_parse($socket, '334');

        fwrite($socket, base64_encode($password)."\r\n");
        server_parse($socket, '235');
    }else{
        fwrite($socket, 'HELO '.$smtp_host."\r\n");
        server_parse($socket, '250');
    }

    fwrite($socket, 'MAIL FROM: <'.$username.'>'."\r\n");
    server_parse($socket, '250');

    foreach ($recipients as $email){
        fwrite($socket, 'RCPT TO: <'.$email.'>'."\r\n");
        server_parse($socket, '250');
    }

    fwrite($socket, 'DATA'."\r\n");
    server_parse($socket, '354');

    fwrite($socket, 'Subject: '.$subject."\r\n".'To: <'.implode('>, <', $recipients).'>'."\r\n".$headers."\r\n\r\n".$message."\r\n");

    fwrite($socket, '.'."\r\n");
    server_parse($socket, '250');

    fwrite($socket, 'QUIT'."\r\n");
    fclose($socket);

    return true;
}

function server_parse($socket, $expected_response){
    $server_response = '';

    while (substr($server_response, 3, 1) != ' '){
        if (!($server_response = fgets($socket, 256))){
            error('Couldn\'t get mail server response codes.<br />Please contact the forum administrator.', __FILE__, __LINE__);
        }
    }

    if (!(substr($server_response, 0, 3) == $expected_response)){
        error('Unable to send e-mail.<br />Please contact the forum administrator with the following error message reported by the SMTP server: "'.$server_response.'"', __FILE__, __LINE__);
    }
}

function error($m){
    die($m);
}   

它大部分时间都有效,但是当发送到任何Gmail地址时,邮件会弹出以下错误代码(修改删除IP地址,电子邮件地址等):

This message was created automatically by mail delivery software.

A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:

  someone@gmail.com
    SMTP error from remote mail server after end of data:
    host gmail-smtp-in.l.google.com [173.194.64.26]:
    550-5.7.1 [1.1.1.1      11] Our system has detected that this message is
    550-5.7.1 not RFC 2822 compliant. To reduce the amount of spam sent to Gmail,
    550-5.7.1 this message has been blocked. Please review
    550 5.7.1 RFC 2822 specifications for more information. sm4si11387798obb.202 - gsmtp

------ This is a copy of the message, including all the headers. ------

Return-path: <someone@terratekkit.org>
Received: from [1.1.1.1] (port=50336 helo=ssl://mail.example.com)
    by mail.example.com with esmtpsa (UNKNOWN:DHE-RSA-AES256-GCM-SHA384:256)
    (Exim 4.80.1)
    (envelope-from <someone@example.com>)
    id 1WPQMJ-0001mk-Q4
    for someone@gmail.com; Mon, 17 Mar 2014 00:45:11 -0500
Subject: test email
To: <someone@gmail.com>


this is a test

我不知道错误是什么,我试图查看上面提到的规范,但它只是一堆对我没用的无用文本:/。

1 个答案:

答案 0 :(得分:1)

尝试使用http://phpmailer.worxware.com/发送gt接受的smtp身份验证电子邮件。 Gmail非常不受垃圾邮件的垃圾邮件......