首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php mail()不从不同的域发送电子邮件

php mail()不从不同的域发送电子邮件
EN

Stack Overflow用户
提问于 2011-05-04 20:35:29
回答 4查看 6.4K关注 0票数 2

我正在使用php的邮件功能发送电子邮件。问题是,来自不同域的电子邮件无法发送。只有来自我的域的电子邮件才会发送。例如,当我从username@mydomain.com发送一封电子邮件时,它就被发送了。当我使用from as username@yahoo.com或username@gmail .com发送电子邮件时,不会发送电子邮件。

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

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

谢谢。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-05-04 20:36:40

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

票数 6
EN

Stack Overflow用户

发布于 2011-05-04 22:13:22

您可以使用PEAR邮件功能http://pear.php.net/package/Mail .By使用此功能您可以从交换服务器发送电子邮件如果您使用sendmail server.But,则可以使用以下code.The下面的代码将不适用于microsoft exchange server

希望能对你有所帮助。

代码语言:javascript
复制
<?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";}

?>
票数 2
EN

Stack Overflow用户

发布于 2012-03-22 17:55:19

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

代码语言:javascript
复制
mail($to,$subject,$message,$headers,"-f ".$from);

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5883440

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档