【发布时间】:2017-02-01 03:38:35
【问题描述】:
我搜索了堆栈溢出以及 Ubuntu 论坛。我正在编写一个网站,并使用 LAMP 服务器自己托管它。我有一个用户填写的表单,并希望它在用户单击提交按钮后发送电子邮件。到目前为止,我已经尝试过 sendmail、ssmtp、phpmailer 以及安装 mailutils 和使用 postfix。我认为这是邮件程序的问题的原因是因为在测试后我没有从代码中得到任何错误,并且我无法从命令行发送邮件。我不想使用 phpmailer,因为我不想在网站中对任何密码进行硬编码,以防万一它被泄露。我将不胜感激有人可以给我的任何帮助。如果您需要配置文件,我已包含邮件脚本,请告诉我提前谢谢。
<?php
session_start();
if(isset($_POST['submit'])){
$to = "jmaggsdf@gmail.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Website Email";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$type = 'plain'; // or HTML
$charset = 'utf-8';
$_SESSION['from'] = $_POST['email'];
$_SESSION['first_name'] = $_POST['first_name'];
$_SESSION['last_name'] = $_POST['last_name'];
$_SESSION['message'] = $_POST['message'];
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/plain; charset=utf-8' . "\r\n";
$headers .= 'To: ' . $to . '<' . $to . '>' . "\r\n";
$headers .= 'From: ' . $from . '<' . $from . '>' . "\r\n";
if($first_name == NULL || $last_name == NULL || $from == NULL || $message == NULL){
header("Location: mailError.php");
die();
}
else{
//$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
header("Location: thanksPage.php");
//mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
//echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
}
}
?>
【问题讨论】:
-
您的服务器是否配置了邮件服务器。请记住
mail()只是将邮件传递给邮件服务器,它实际上并不发送它自己 -
您尝试过 Google Mail API for PHP 吗?它非常方便快捷。这是了解更多信息的链接:cloud.google.com/appengine/docs/php/mail
-
或者几乎无处不在的
phpMailergithub.com/PHPMailer/PHPMailer 那你就不需要安装和维护邮件服务器了 -
试试phpmailer......
-
@RiggsFolly 我确实知道这一点,并且在我的研究中读到了这一点,我尝试了一些不同的程序,但仍然没有骰子,也没有错误。我在配置中遗漏了什么吗?此外,如果我为此目的使用 phpmailer,我是否必须对任何密码进行硬编码?
标签: php apache email ubuntu sendmail