dongzhang1864 2015-10-06 06:00
浏览 57

从服务器发送邮件时出错?

I am trying to send mail from the server not from localhost, it give status code as 200, but still i am not receiving any mails.

Response Image: Response Image

Php Code to send message:

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

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

 mail($to, $subject, $message, $headers);
  • 写回答

2条回答 默认 最新

  • dqp4933 2015-10-06 06:47
    关注

    There might be more things wrong but let's start with the obvious:

    $to = 'example@gmail.com'; 
    

    This should contain your email address if you want to receive something.

    Can you fix that and test again? If it doesn't work yet we can go a step further.


    EDIT AFTER YOUR ANSWER: I have done that already i am messaging from my gmail account to one of my friend gmail account. Is I am making any mistake?

    With the code you made php will try to send the email with the mail server which is in your configurations: to send an email with GMAIL you need to fix your code. This is an example of how you can use phpmailer to send it:

    //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!";
    } 
    

    from: https://stackoverflow.com/a/16022357/2042240

    I really suggest you to use phpmailer (http://phpmailer.worxware.com/) since it is very powerful and easy to use. When you have downloaded the library just import it as in the example i showed you and you're ready to go.

    EDIT 2: if you still prefer using the mail() function here you can see how you can enable GMAIL with it: https://www.digitalocean.com/community/tutorials/how-to-use-gmail-or-yahoo-with-php-mail-function

    评论

报告相同问题?

悬赏问题

  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录