首页 文章

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

提问于
浏览
1

我正在使用php的邮件功能发送电子邮件 . 问题是,来自不同域的电子邮件不会被发送 . 仅发送来自我的域的电子邮件 . 例如,当我从username@mydomain.com发送电子邮件时,它会被发送 . 当我使用from username@yahoo.com或username @ gmail .com发送电子邮件时,不会发送电子邮件 .

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

我需要绕过这个限制哪些 Headers ?或任何其他解决方案 .

谢谢 .

4 回答

  • 0

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

  • 0

    您可以使用PEAR邮件功能http://pear.php.net/package/Mail . 使用此功能,您可以从交换服务器发送电子邮件 . 但是如果您使用sendmail服务器,则可以使用以下代码 . 以下代码不适用于Microsoft Exchange服务器

    希望它对你有所帮助 .

    <?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";}
    
    ?>
    
  • 5

    这听起来像是SMTP server is not allowing relay.与您的邮件服务器管理员交谈并询问他们是否会为您的应用程序的IP地址打开中继 .

  • 2

    尝试在mail()函数中添加第五个参数:

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

    注意:$ from必须与您在 Headers 中指定的“发件人”电子邮件地址相似 .

相关问题