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

    if ($_POST['submit']) {
        if (mail ($to, $subject, $body, $from)) {
            echo '<p>Your message has been sent!</p>';
        } else {
            echo '<p>Something went wrong, go back and try again!</p>';
        }
    }
?>

我试着创建了一个简单的邮件表单。表单本身在我的index.html页面上,但它提交到一个单独的“感谢您的提交”页面thanks . PHP,其中嵌入了上述PHP代码。 代码完美地提交,但从未发送电子邮件。我该如何解决这个问题?


当前回答

对于任何发现这一点的人,我不建议使用邮件。有一些答案涉及到这个问题,但没有解释为什么会这样。

PHP's mail function is not only opaque, it fully relies on whatever MTA you use (i.e. Sendmail) to do the work. mail will only tell you if the MTA failed to accept it (i.e. Sendmail was down when you tried to send). It cannot tell you if the mail was successful because it's handed it off. As such (as John Conde's answer details), you now get to fiddle with the logs of the MTA and hope that it tells you enough about the failure to fix it. If you're on a shared host or don't have access to the MTA logs, you're out of luck. Sadly, the default for most vanilla installs for Linux handle it this way.

邮件库(PHPMailer、Zend Framework 2+等)的功能与邮件非常不同。它们直接打开接收邮件服务器的套接字,然后通过该套接字直接发送SMTP邮件命令。换句话说,类充当自己的MTA(注意,您可以告诉库使用邮件来最终发送邮件,但我强烈建议您不要这样做)。

这意味着您可以直接看到来自接收服务器的响应(例如,在PHPMailer中,您可以打开调试输出)。不用再猜测邮件是否发送失败或原因。

如果您正在使用SMTP(即您正在调用isSMTP()),您可以使用SMTPDebug属性获得SMTP对话的详细记录。 通过在脚本中包含如下一行来设置该选项: $mail->SMTPDebug = 2;

您还可以获得更好的界面。对于邮件,你必须设置所有的标题,附件等。有了库,就有了专门的函数来实现这一点。这也意味着函数正在处理所有棘手的部分(比如头文件)。

其他回答

你可以通过以下方法查看错误:

error_reporting(E_ALL);

我的示例代码是:

<?php
    use PHPMailer\PHPMailer\PHPMailer;
    require 'PHPMailer.php';
    require 'SMTP.php';
    require 'Exception.php';

    $name = $_POST['name'];
    $mailid = $_POST['mail'];
    $mail = new PHPMailer;
    $mail->IsSMTP();
    $mail->SMTPDebug = 0;                   // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';         // Specify main and backup server
    $mail->Port = 587;                      // Set the SMTP port
    $mail->SMTPAuth = true;                 // Enable SMTP authentication
    $mail->Username = 'someone@gmail.com';  // SMTP username
    $mail->Password = 'password';           // SMTP password
    $mail->SMTPSecure = 'tls';              // Enable encryption, 'ssl' also accepted

    $mail->From = 'someone@gmail.com';
    $mail->FromName = 'name';
    $mail->AddAddress($mailid, $name);       // Name is optional
    $mail->IsHTML(true);                     // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'Here is your message' ;
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    if (!$mail->Send()) {
       echo 'Message could not be sent.';
       echo 'Mailer Error: ' . $mail->ErrorInfo;
       exit;
    }
    echo 'Message has been sent';
?>

您可以使用CodeIgniter配置电子邮件。例如,使用SMTP(简单方式):

$config = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'mail.domain.com', // Your SMTP host
        'smtp_port' => 26, // Default port for SMTP
        'smtp_user' => 'name@domain.com',
        'smtp_pass' => 'password',
        'mailtype' => 'html',
        'charset' => 'iso-8859-1',
        'wordwrap' => TRUE
);
$message = 'Your msg';
$this->load->library('email', $config);
$this->email->from('name@domain.com', 'Title');
$this->email->to('emaildestination@domain.com');
$this->email->subject('Header');
$this->email->message($message);

if($this->email->send()) 
{
   // Conditional true
}

这对我很管用!

如果你在本地服务器上运行这段代码(也就是你的计算机用于开发目的),它不会将电子邮件发送给收件人。它将在名为mailoutput的文件夹中创建一个.txt文件。

在这种情况下,如果您正在使用免费的软管服务,如000webhost或hostinger,这些服务提供商将禁用mail()函数,以防止意外使用电子邮件欺骗,垃圾邮件等。我希望你联系他们,看看他们是否支持这个功能。

如果您确定服务提供者支持mail()函数,您可以查看PHP手册以进一步参考,

PHP邮件()

要检查您的托管服务是否支持mail()函数,请尝试运行这段代码(记得更改收件人的电子邮件地址):

<?php
    $to      = 'nobody@example.com';
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: webmaster@example.com' . "\r\n" .
        'Reply-To: webmaster@example.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

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

我有这个问题,并发现剥离标题帮助我获得邮件。 所以这个:

$headers  = "MIME-Version: 1.0;\r\n";
$headers .= "Content-type: text/plain; charset=utf-8;\r\n";
$headers .= "To: ".$recipient."\r\n";
$headers .= "From: ".__SITE_TITLE."\r\n";
$headers .= "Reply-To: ".$sender."\r\n";

成为这个:

$headers = "From: ".__SITE_TITLE."\r\n";
$headers .= "Reply-To: ".$sender."\r\n";

不需要To:标头。

邮件客户端非常擅长嗅出url并将其重写为超链接。所以我不需要编写HTML并在content-type头文件中指定text/ HTML。我只是在消息体中添加了带\r\n的新行。 我知道这不是编码纯粹主义者的方法,但它适用于我所需要的。

总是尝试在邮件函数中发送标题。 如果您通过localhost发送邮件,则执行SMTP设置以发送邮件。 如果您通过服务器发送邮件,请检查您的服务器上是否启用了电子邮件发送功能。