繁体   English   中英

php mail() 不发送来自不同域的电子邮件

[英]php mail() not sending emails from different domains

我正在使用 php 的邮件 function 发送电子邮件。 问题是,来自不同域的电子邮件不会被发送。 只发送来自我域的电子邮件。 例如,当我从 username@mydomain.com 发送 email 时,它被发送。 当我从 username@yahoo.com 或 username@gmail.com 发送 email 时,Z0C83ZF57C786AC7EB43 未发送。

当电子邮件被发送时,它们最终会进入垃圾邮件部分。

我需要哪些标头来绕过此限制? 或任何其他解决方案。

谢谢。

这取决于您的 SMTP 服务器。 这可能会防止垃圾邮件流出。

您可以使用 PEAR 邮件 function http://pear.php.net/package/Mail 。通过使用它,您可以从交换服务器发送电子邮件。但是如果您使用 sendmail 服务器,则可以使用以下代码。以下代码不适用于 microsoft exchange server

希望它对你有帮助。

<?php
//new function
$to=//receipent email addressaddress;
$message=//Message;
$nameto = "Name of receipent";
$from = "abc@example.com";//sender address
$namefrom = "Test";//from name
$subject = "Subject of the mail";


function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
{
    //SMTP + SERVER DETAILS
    /* * * * CONFIGURATION START * * * */
    $smtpServer = "mail.example.com";//mail server address
    $port = "25";//port
    $timeout = "30";
    $username = "test";//user name
    $password = "****";//password of sender
    $localhost = "1.2.3.4";//Ip address of mail server
    $newLine = "\r\n";
    /* * * * CONFIGURATION END * * * * */

    //Connect to the host on the specified port
    $smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
    $smtpResponse = fgets($smtpConnect, 515);
        if(empty($smtpConnect))
        {
            $output = "Failed to connect: $smtpResponse";
            return $output;
        }
        else
        {
            $logArray['connection'] = "Connected: $smtpResponse";
        }

    //Request Auth Login
    fputs($smtpConnect,"AUTH LOGIN" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['authrequest'] = "$smtpResponse";

    //Send username
    fputs($smtpConnect, base64_encode($username) . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['authusername'] = "$smtpResponse";

    //Send password
    fputs($smtpConnect, base64_encode($password) . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['authpassword'] = "$smtpResponse";

    //Say Hello to SMTP
    fputs($smtpConnect, "HELO $localhost" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['heloresponse'] = "$smtpResponse";

    //Email From
    fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['mailfromresponse'] = "$smtpResponse";

    //Email To
    fputs($smtpConnect, "RCPT TO: $to" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['mailtoresponse'] = "$smtpResponse";

    //The Email
    fputs($smtpConnect, "DATA" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['data1response'] = "$smtpResponse";

    //Construct Headers
    $headers = "MIME-Version: 1.0" . $newLine;
    $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
    $headers .= "To: $nameto <$to>" . $newLine;
    $headers .= "From: $namefrom <$from>" . $newLine;

    fputs($smtpConnect, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['data2response'] = "$smtpResponse";

    // Say Bye to SMTP
    fputs($smtpConnect,"QUIT" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['quitresponse'] = "$smtpResponse";
}

if(authSendEmail($from, $namefrom, $to, $nameto, $subject, $message))
{echo "send";}
else{echo "failed";}

?>

听起来您的SMTP 服务器不允许中继。 与您的邮件服务器管理员交谈,询问他们是否会为您的应用程序的 IP 地址打开中继。

尝试在您的 mail() function 中添加第五个参数:

mail($to,$subject,$message,$headers,"-f ".$from);

注意:$from 必须与您在 header 中指定的 'from' email 地址相似。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM