php - 在php中发送邮件的困难

标签 php apache email ubuntu sendmail

这个问题在这里已经有了答案:





PHP mail function doesn't complete sending of e-mail

(30 个回答)


5年前关闭。




我搜索了堆栈溢出以及 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.";
    }
}
?>

最佳答案

如果您使用本地主机发送邮件,那么它将不会发送邮件。
它需要实时服务器。

关于php - 在php中发送邮件的困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41971491/

相关文章:

django - Google Apps 每隔几天就会停止发送电子邮件

php - 复选框问题以及在数据库中添加值

java - 我可以多次从 Apache PropertiesConfiguration 读取相同的配置而不损失性能吗?

apache - 为什么会在我的网站上向/eyeblaster 发出请求?

java - 带 mod_auth_openidc 的 Keycloak,检索代码和 id_token

python - 从 python 作为 google 组发送电子邮件

php - 如何使用 php 在一行中从 mysql 查询中获取单个字段?

php - Laravel - 不允许对图像设置为 Nullable 的请求验证并给出错误原因?

javascript - 通过php函数增加HTML表格行

android - 如何在电子邮件内容中添加新行?