<?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代码。
代码完美地提交,但从未发送电子邮件。我该如何解决这个问题?
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
$name = $_POST['name'];
$email = $_POST['email'];
$reciver = '/* Reciver Email address */';
if (filter_var($reciver, FILTER_VALIDATE_EMAIL)) {
$subject = $name;
// To send HTML mail, the Content-type header must be set.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:' . $email. "\r\n"; // Sender's Email
//$headers .= 'Cc:' . $email. "\r\n"; // Carbon copy to Sender
$template = '<div style="padding:50px; color:white;">Hello ,<br/>'
. '<br/><br/>'
. 'Name:' .$name.'<br/>'
. 'Email:' .$email.'<br/>'
. '<br/>'
. '</div>';
$sendmessage = "<div style=\"background-color:#7E7E7E; color:white;\">" . $template . "</div>";
// Message lines should not exceed 70 characters (PHP rule), so wrap it.
$sendmessage = wordwrap($sendmessage, 70);
// Send mail by PHP Mail Function.
mail($reciver, $subject, $sendmessage, $headers);
echo "Your Query has been received, We will contact you soon.";
} else {
echo "<span>* invalid email *</span>";
}
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个小时…别忘了检查你的垃圾邮件文件夹!