<?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代码已嵌入其中。该代码可以完美提交,但从不发送电子邮件。我怎样才能解决这个问题?
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 issendmail -t -i
, so if you get a workingsendmail -t -i < message.txt
in the Linux console - you will be done. You could also addmail.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 withsendmail -v -t -i < message.txt
. Filemessage.txt
should contain all headers of a message and a body, destination address for the envelope will be taken from theTo:
header. Example: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: