没有收到来自php mail()函数的邮件 [英] not receiving emails from php mail() function

查看:113
本文介绍了没有收到来自php mail()函数的邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在通过创建网页来学习HTML,js,CSS和PHP,并将它们发布在我的raspberry pi上的apache2服务器上,并使用no-ip.com地址。我的第一个学习PHP的真正项目一直在创建一个简单的表单,然后发送一封电子邮件给我,但我不断遇到mail()函数的问题执行没有任何错误,但实际上没有发送电子邮件。



这是表单:

 < form class =action =/ FormTesting / sendMail.phpmethod =post> 
名称:< br>< input type =textname =namevalue =required>< br>
电子邮件:< br>< input type =textname =emailvalue =required>< br>
消息:< br>< textarea name =messagerows =8cols =80required>< / textarea>< br>
主题:
< input type =radioname =subjectvalue =Hello!>您好!
< input type =radioname =subjectvalue =Whats up> Whats Up!< br>
< input type =submitname =submitvalue =Submit>
< / form>

以下是表单action属性中的PHP文件引用:

 <?php 
if($ _ SERVER [REQUEST_METHOD] ==POST){
$ to =myExampleEmail @ gmail.com;
$ name = $ _POST [name];
$ email = $ _POST [email];
$ message = $ _POST [message];
$ subject = $ _POST [subject];

echo $ name;
echo $ email;
echo $ message;
echo $ subject;

$ headers = array(From:$ email,
Reply-To:$ to,
X-Mailer:PHP /PHP_VERSION) ;

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

} else {
echo $ _SERVER [REQUEST_METHOD];
}
?>

我知道这个问题以前已经被问过了,我已经尝试了大部分的方法其他答案,但我仍然似乎无法让PHP mail()函数工作。我所拥有的最好的信息是,我必须在服务器本身进行某种安装或格式化以允许发送电子邮件,但是我还没有找到它的文档和/或解释。 (即phpMailer? - 但仍然...如何?)



编辑:



我检查了返回值的邮件功能。 。 。

  if(mail(myExampleEmail@gmail.com,subject,message,$ headers)){
echoTRUE;
} else {
echoFALSE;
}

。 。 。它原来是返回错误。我假设这意味着我的代码中的错误,然后?



编辑:



显然我wasn'当我说我在研究这个问题之前,我清楚了。我尝试了所有的东西,作为一个答案,我的一个封闭的重复,所以我问这个问题,无论如何。

解决方案

我尝试了相同的脚本,并收到了电子邮件。但是,它去了垃圾邮件文件夹。标题应该有更多的细节。您可以检查邮件功能返回码,以确保该功能成功。以下是最佳做法。

  $ boundary === MP_Bound_xyccr948x ==; 
$ headers =MIME-Version:1.0\r\\\
;
$ headers。=Content-type:multipart / alternative; boundary = \$ boundary \\r\\\
;
$ headers。=From:OrganizationName< webmaster@mycompany.com> \r\\\
;
$ headers。=返回路径:webmaster@mycompany.com\r\\\
;


$ message =这是MIME格式的多部分消息\;
$ message。= - $ boundary\\\
;
$ message。=Content-Type:text / plain; charset = \utf-8\\\\
;
$ message。=Content-Transfer-Encoding:7bit\\\
\\\
;
$ message。= $ user_entered_message。 \\\
;
$ message。= - $ boundary--;
$ mailsent = mail($ to,$ subject,$ message,$ headers,-fbounces@mycompany.com);

if($ mailsent)
{
confirmation_msg();
}
else
{
email_error_msg();
}


I have recently been learning HTML, js, CSS, and PHP through creating web pages and posting them on my apache2 server hosted on my raspberry pi with a no-ip.com address. My first real project for learning PHP has been creating a simple form to then send an email to me, but I keep running into the issue of the mail() function executing without any errors, but not actually sending emails.

Here is the form:

<form class="" action="/FormTesting/sendMail.php" method="post">
  Name:<br><input type="text" name="name" value="" required><br>
  Email:<br><input type="text" name="email" value="" required><br>
  Message:<br><textarea name="message" rows="8" cols="80" required></textarea><br>
  Subject:
  <input type="radio" name="subject" value="Hello!">Hello!
  <input type="radio" name="subject" value="Whats up">Whats Up!<br>
  <input type="submit" name="submit" value="Submit">
</form>

Here is the PHP file reference in the form's 'action' attribute:

<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
  $to = "myExampleEmail@gmail.com";
  $name = $_POST["name"];
  $email = $_POST["email"];
  $message = $_POST["message"];
  $subject = $_POST["subject"];

  echo $name;
  echo $email;
  echo $message;
  echo $subject;

  $headers = array("From: " . $email,
"Reply-To: " . $to,
"X-Mailer: PHP/" . PHP_VERSION);

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

} else {
  echo $_SERVER["REQUEST_METHOD"];
}
?>

I understand that this question has been asked before, and I have tried most of the methods set out by other answers, but I still can't seem to get the PHP mail() function to work. The best information I have is that I have to do some kind of install or formatting on the server itself to allow for emails to be sent, but I have yet to find it well documented and/or explained. (i.e phpMailer? - but still... how?)

EDIT:

I checked the return value of the mail function . . .

  if(mail("myExampleEmail@gmail.com", "subject", "message", $headers)) {
    echo "TRUE";
  }else {
    echo "FALSE";
  } 

. . . and it turns out to be returning false. I assume that means it is an error in my code, then?

EDIT:

Apparently I wasn't clear when I said I did research before posting this question. I tried everything that was given as an answer to the question that mine was a closed as a "duplicate" of, hence me asking, the question anyway.

解决方案

I tried the same script and got the email. But, it went to spam folder. Header should have more details. You can check the mail function return code to make sure that the function succeeded. Following is the best practice.

            $boundary = "==MP_Bound_xyccr948x==";
        $headers = "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: multipart/alternative; boundary=\"$boundary\"\r\n";
        $headers .= "From: OrganizationName <webmaster@mycompany.com>\r\n";
        $headers .= "Return-path: webmaster@mycompany.com\r\n";


        $message = "This is a Multipart Message in MIME format\n";
        $message .= "--$boundary\n";
        $message .= "Content-Type: text/plain; charset=\"utf-8\"\n";
        $message .= "Content-Transfer-Encoding: 7bit\n\n";
        $message .= $user_entered_message . "\n";
        $message .= "--$boundary--";
        $mailsent = mail($to, $subject, $message, $headers, "-fbounces@mycompany.com");

        if ($mailsent) 
        {
         confirmation_msg();
        } 
        else 
        {
         email_error_msg();
        }

这篇关于没有收到来自php mail()函数的邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆