PHP邮件功能无法完成电子邮件的发送

<?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页面上,但是会提交到单独的“谢谢您的提交”页面,thankyou.php上面的PHP代码已嵌入其中。该代码可以完美提交,但从不发送电子邮件。我怎样才能解决这个问题?

猪猪猴子2020/03/20 10:08:03

For those who do not want to use external mailers and want to mail() on a dedicated Linux server.

The way, how PHP mails, is described in php.ini in section [mail function].

Parameter sendmail-path describes how sendmail is called. The default value is sendmail -t -i, so if you get a working sendmail -t -i < message.txt in the Linux console - you will be done. You could also add mail.log to debug and be sure mail() is really called.

Different MTAs can implement sendmail. They just make a symbolic link to their binaries on that name. For example, in Debian the default is Postfix. Configure your MTA to send mail and test it from the console with sendmail -v -t -i < message.txt. File message.txt should contain all headers of a message and a body, destination address for the envelope will be taken from the To: header. Example:

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

Text body.

I prefer to use ssmtp as MTA because it is simple and does not require running a daemon with opened ports. ssmtp fits only for sending mail from localhost. It also can send authenticated email via your account on a public mail service. Install ssmtp and edit configuration file /etc/ssmtp/ssmtp.conf. To be able also to receive local system mail to Unix accounts (alerts to root from cron jobs, for example) configure /etc/ssmtp/revaliases file.

Here is my configuration for my account on Yandex mail:

root=mymail@example.com
mailhub=smtp.yandex.ru:465
FromLineOverride=YES
UseTLS=YES
AuthUser=abcde@yandex.ru
AuthPass=password
小小猿2020/03/20 10:08:02

Make sure you have Sendmail installed in your server.

If you have checked your code and verified that there is nothing wrong there, go to /var/mail and check whether that folder is empty.

If it is empty, you will need to do a:

sudo apt-get install sendmail

if you are on an Ubuntu server.

神奇JinJin2020/03/20 10:08:02

如果仅使用该mail()功能,则需要完成配置文件。

您需要打开邮件扩展,并设置SMTP smtp_port等等,最重要的是您的用户名和密码。否则,将无法发送邮件。另外,您可以使用PHPMail该类进行发送。

A泡芙2020/03/20 10:08:01
  1. 始终尝试在邮件功能中发送标题。
  2. 如果要通过本地主机发送邮件,请执行SMTP设置以发送邮件。
  3. 如果要通过服务器发送邮件,请检查服务器上是否启用了电子邮件发送功能。