【问题标题】:Error in sending mail from server?从服务器发送邮件时出错?
【发布时间】:2016-01-02 22:40:43
【问题描述】:

我正在尝试从服务器而不是本地主机发送邮件,它给出的状态代码为 200,但我仍然没有收到任何邮件。

响应图片:

发送消息的 PHP 代码:

 $name = $_POST['name'];
 $email = $_POST['email'];
 $message = $_POST['message'];
 $from = 'From: example@gmail.com'; 
 $to = 'example@gmail.com'; 
 $subject = 'Customer Inquiry';
 $body = "From: $name\n E-Mail: $email\n Message:\n $message";

 $headers .= "MIME-Version: 1.0\r\n";
 $headers .= "Content-type: text/html\r\n";
 $headers = 'From: example@gmail.com' . "\r\n" .
 'Reply-To: example@gmail.com' . "\r\n" .
 'X-Mailer: PHP/' . phpversion();

 mail($to, $subject, $message, $headers);

【问题讨论】:

    标签: php server sendmail


    【解决方案1】:

    可能还有更多错误,但让我们从显而易见的开始:

    $to = 'example@gmail.com'; 
    

    如果您想收到一些东西,这应该包含您的电子邮件地址。

    你能解决这个问题并再次测试吗?如果还不行,我们可以更进一步。


    在你回答后编辑:我已经做到了,我已经从我的 gmail 帐户向我的一个朋友 gmail 帐户发送消息。我是不是搞错了?

    使用您编写的代码,php 将尝试使用您配置中的邮件服务器发送电子邮件:要使用 GMAIL 发送电子邮件,您需要修复您的代码。这是一个如何使用 phpmailer 发送它的示例:

    //include the file
    require_once('class.phpmailer.php');
    
    $phpmailer          = new PHPMailer();
    
    
    $phpmailer->IsSMTP(); // telling the class to use SMTP
    $phpmailer->Host       = "ssl://smtp.gmail.com"; // SMTP server
    $phpmailer->SMTPAuth   = true;                  // enable SMTP authentication
    $phpmailer->Port       = 465;          // set the SMTP port for the GMAIL server; 465 for ssl and 587 for tls
    $phpmailer->Username   = "yourname@yourdomain"; // Gmail account username
    $phpmailer->Password   = "yourpassword";        // Gmail account password
    
    $phpmailer->SetFrom('name@yourdomain.com', 'First Last'); //set from name
    
    $phpmailer->Subject    = "Subject";
    $phpmailer->MsgHTML($body);
    
    $phpmailer->AddAddress($to, "To Name");
    
    if(!$phpmailer->Send()) {
      echo "Mailer Error: " . $phpmailer->ErrorInfo;
    } else {
      echo "Message sent!";
    } 
    

    来自:https://stackoverflow.com/a/16022357/2042240

    我真的建议您使用 phpmailer (http://phpmailer.worxware.com/),因为它非常强大且易于使用。下载库后,只需按照我向您展示的示例导入它即可。

    编辑 2:如果您仍然喜欢使用 mail() 函数,您可以在此处查看如何启用 GMAIL:https://www.digitalocean.com/community/tutorials/how-to-use-gmail-or-yahoo-with-php-mail-function

    【讨论】:

    • 我已经做到了,我已经从我的 gmail 帐户向我的一个朋友 gmail 帐户发送消息。我有什么错误吗?
    • @MukulAggarwal 给你。您应该修复您的代码以使其正常工作。
    【解决方案2】:

    已发送邮件的代码

        $to='comments@mydomain.com';
        $from = $_POST['email'];
        $name = $_POST['name'];
        $subject="comments from : ".$name;
        $message = "Comment from  :  ".$from."\r\n";
        $message.=$_POST['comments'];
        $message=wordwrap($message,70,"<br>");
        $message=str_replace("\n.","\n..",$message);
        $headers='From: info@mydomain.com';
        $mail=mail($to, $subject, $message , $headers);
        if ($mail==true)
           echo "success";
    

    【讨论】:

      猜你喜欢
      • 2019-11-26
      • 2018-04-22
      • 2013-08-27
      • 2018-05-09
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      • 2017-11-18
      • 2013-06-12
      相关资源
      最近更新 更多