<?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.iniin section[mail function].Parameter
sendmail-pathdescribes how sendmail is called. The default value issendmail -t -i, so if you get a workingsendmail -t -i < message.txtin the Linux console - you will be done. You could also addmail.logto 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.txtshould 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/revaliasesfile.Here is my configuration for my account on Yandex mail: