【问题标题】:Difficulty with sending mail in php [duplicate]在php中发送邮件的困难[重复]
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 我知道,但只是简单地安装 sendmail 或 postfix 就可以了,还是我需要更改服务器上的 php.ini 或邮件配置文件?
  • mail() 可以,只是参考一些例子或者你可以使用第三方软件如phpmailer。
猜你喜欢
  • 2015-10-12
  • 2012-05-11
  • 1970-01-01
  • 2016-05-10
  • 2020-03-25
  • 1970-01-01
  • 2014-09-08
  • 2014-12-03
  • 1970-01-01
相关资源
最近更新 更多