<?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代码。 代码完美地提交,但从未发送电子邮件。我该如何解决这个问题?


当前回答

如果你受困于Hostgator上的应用,这便是解决我的问题的方法。非常感谢发布详细解决方案的人。如果有一天链接离线,你有总结:

在服务器中查找sendmail路径。一个简单的方法来检查它,是临时写以下代码在一个页面,只有你将访问,以读取生成的信息:php phpinfo ();? >。打开这个页面,寻找sendmail路径。(然后,别忘了删除这段代码!) 问题和修正:如果你的sendmail路径只说-t -i,那么编辑你的服务器的php.ini并添加以下行:sendmail_path = /usr/sbin/sendmail -t -i;

但是,在能够使用PHP mail()函数发送邮件后,我了解到它发送未经验证的电子邮件,这产生了另一个问题。这些邮件都落在了我Hotmail的垃圾邮箱里,有些邮件从来没有发送过,我猜这与它们没有经过认证有关。这就是为什么我决定从mail()转换到使用SMTP的PHPMailer。

其他回答

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

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;

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

如果只使用mail()函数,则需要完成配置文件。

您需要打开邮件扩展,并设置SMTP smtp_port等,最重要的是,设置您的用户名和密码。没有它,邮件就无法发送。此外,您还可以使用PHPMail类来发送。

你可以使用PHPMailer,它工作得很完美,这里有一个代码示例:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/phpmailer/phpmailer/src/Exception.php';
require 'vendor/phpmailer/phpmailer/src/PHPMailer.php';
require 'vendor/phpmailer/phpmailer/src/SMTP.php';
$editor = $_POST["editor"];
$subject = $_POST["subject"];
$to = $_POST["to"];

try {

    if ($_SERVER["REQUEST_METHOD"] == "POST") {

        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->Mailer = "smtp";
        $mail->SMTPDebug  = 1;
        $mail->SMTPAuth   = TRUE;
        $mail->SMTPSecure = "tls";
        $mail->Port       = 587;
        $mail->Host       = "smtp.gmail.com";//using smtp server
        $mail->Username   = "XXXXXXXXXX@gmail.com";//the email which will send the email 
        $mail->Password   = "XXXXXXXXXX";//the password

        $mail->IsHTML(true);
        $mail->AddAddress($to, "recipient-name");
        $mail->SetFrom("XXXXXXXXXX@gmail.com", "from-name");
        $mail->AddReplyTo("XXXXXXXXXX@gmail.com", "reply-to-name");
        $mail->Subject = $subject;
        $mail->MsgHTML($editor);




        if (!$mail->Send()) {
            echo "Error while sending Email.";
            var_dump($mail);
        } else {
            echo "Email sent successfully";
        }
    }
} catch (Exception $e) {
    echo $e->getMessage();
}

Debian 10.0.0 ('Buster')的Sendmail安装实际上很简单!

php . ini

[mail function]
sendmail_path=/usr/sbin/sendmail -t -i
; (Other directives are mostly windows)

标准sendmail包安装(允许'send'):

su -                                        # Install as user 'root'
dpkg --list                                 # Is install necessary?
apt-get install sendmail sendmail-cf m4     # Note multiple package selection
sendmailconfig                              # Respond all 'Y' for new install

其他有用的命令:

which sendmail                              # /usr/sbin/sendmail
which sendmailconfig                        # /usr/sbin/sendmailconfig
man sendmail                                # Documentation
systemctl restart sendmail                  # As and when required

验证(发送能力)

echo "Subject: sendmail test" | sendmail -v <yourEmail>@gmail.com

以上花费了大约5分钟。然后我浪费了5个小时…别忘了检查你的垃圾邮件文件夹!

对于那些不想使用外部邮件并且希望在专用的Linux服务器上使用mail()的人。

PHP如何发送邮件,详见PHP .ini中[邮件函数]一节。

参数sendmail-path描述如何调用sendmail。默认值是sendmail -t -i,因此如果您在Linux控制台中得到一个正常工作的sendmail -t -i < message.txt,那么您就完成了。您还可以将mail.log添加到调试中,并确保真正调用了mail()。

不同的mta可以实现sendmail。它们只是在这个名称上创建一个符号链接到它们的二进制文件。例如,在Debian中,默认是后缀。配置MTA以发送邮件并从控制台使用sendmail -v -t -i < message.txt进行测试。文件message.txt应该包含消息的所有头和正文,信封的目的地址将从To:头中获取。例子:

From: myapp@example.com
To: mymail@example.com
Subject: Test mail via sendmail.

Text body.

我更喜欢使用smtp作为MTA,因为它简单,不需要运行开放端口的守护进程。smtp仅适用于从本地主机发送邮件。它还可以通过公共邮件服务通过您的帐户发送经过认证的电子邮件。安装ssmtp,编辑配置文件/etc/ssmtp/ssmtp.conf。配置/etc/ssmtp/revaliases文件也能够接收本地系统邮件到Unix帐户(例如,从cron作业到root的警报)。

以下是我在Yandex邮箱上的账户配置:

root=mymail@example.com
mailhub=smtp.yandex.ru:465
FromLineOverride=YES
UseTLS=YES
AuthUser=abcde@yandex.ru
AuthPass=password