PHP 邮件功能在我的服务器上不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21823110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:28:23  来源:igfitidea点击:

PHP mail function is not working on my server?

phpemail

提问by Priya jain

My mail function is on online server. It was working fine. But not now. I am confused why it has stopped sending mail. It shows the message that email sent, but no email is received in inbox.

我的邮件功能在在线服务器上。它工作正常。但是不是现在。我很困惑为什么它停止发送邮件。它显示电子邮件发送的消息,但收件箱中没有收到电子邮件。

Here is my php code:

这是我的php代码:

<?php
if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];

    $headers = "From: " . $email . "\n"; //from address
    $to = ""; //my mail id
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1 \n";
    $subject = "Test mail";

    $body = '<html>
        <head>
            <meta http-equiv="content-type" content="text/html; charset=utf-8" />
            <title>"."<h3>"."Hello Test,."</h3>".
    </title>
        </head>
        <body><p></p>
            <p style="color: #00CC66; font-weight:600; font-style:italic; font-size:14px; float:left; margin-left:7px;">You have received an inquiry from your website.  Please review the contact information below.

    :</p>';
    if (mail($to, $subject, $body, $headers)) {
        ?>
        <h3 style="color:#d96922; font-weight:bold; height:0px; margin-top:1px;">Thank You For Contacting us!!</h3>

        <?php
    } else {

        print_r(error_get_last());
    }
}

What's wrong with it? Please help me to find out the solution. And also help me to echo error?

它出什么问题了?请帮我找出解决方案。还帮我回显错误?

回答by jerdiggity

I'd give this a try, ending each line with \r\nand also adding a Toheader (redundant as it might seem). Also, declaring charset=utf-8in the header should be enough. If not though, make sure it matches (right now there's a mismatch).

我会试一试,以每行结尾\r\n并添加一个To标头(看起来是多余的)。此外,charset=utf-8在标题中声明应该就足够了。如果不是,请确保它匹配(现在有一个不匹配)。

<?php
  $subject = "Test mail";
  $to_email = "[email protected]";
  $to_fullname = "John Doe";
  $from_email = "[email protected]";
  $from_fullname = "Jane Doe";
  $headers  = "MIME-Version: 1.0\r\n";
  $headers .= "Content-type: text/html; charset=utf-8\r\n";
  // Additional headers
  // This might look redundant but some services REALLY favor it being there.
  $headers .= "To: $to_fullname <$to_email>\r\n";
  $headers .= "From: $from_fullname <$from_email>\r\n";
  $message = "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">\r\n
  <head>\r\n
    <title>Hello Test</title>\r\n
  </head>\r\n
  <body>\r\n
    <p></p>\r\n
    <p style=\"color: #00CC66; font-weight:600; font-style: italic; font-size:14px; float:left; margin-left:7px;\">You have received an inquiry from your website.  Please review the contact information below.</p>\r\n
  </body>\r\n
  </html>";
  if (!mail($to_email, $subject, $message, $headers)) { 
    print_r(error_get_last());
  }
  else { ?>
    <h3 style="color:#d96922; font-weight:bold; height:0px; margin-top:1px;">Thank You For Contacting us!!</h3>
  <?php
  }
  ?>

I'd also replace the first line with something like this, to avoid notices / errors:

我也会用这样的东西替换第一行,以避免通知/错误:

if ($_POST && isset($_POST['submit']) && isset($_POST['name']) && isset($_POST['email']) && !empty($_POST['name']) && !empty($_POST['email'])) {

回答by Aouwsaf

Working with godadday server it works as expected, but on bigrock server it will not work till we did not create a respond mail ( registered mail from cpanel). Login to your cpanel provided by big rock, scroll down to register a mail id. create that mail id e.g [email protected].

使用godaddday服务器它按预期工作,但在bigrock服务器上它不会工作,直到我们没有创建回复邮件(来自cpanel的注册邮件)。登录到 big rock 提供的 cpanel,向下滚动以注册邮件 ID。创建该邮件 ID,例如 [email protected]

Once you will create that mail id wait for 10-30 minutes after that mail function will start working.

创建该邮件 ID 后,请等待 10-30 分钟,然后该邮件功能将开始工作。

for more details read this article

有关更多详细信息,请阅读这篇文章

回答by Peter Mwangi

Had a similar challenge, on my end however the error was not on the code, i had missed creating the actual sent from email on cpanel. Its an easy thing to miss but can frustrate you

有一个类似的挑战,在我的最后但是错误不在代码上,我错过了在 cpanel 上创建从电子邮件发送的实际内容。它很容易错过,但会让你感到沮丧