【问题标题】:Unable to send email using PHP Pear无法使用 PHP Pear 发送电子邮件
【发布时间】:2014-01-05 00:32:52
【问题描述】:

我尝试使用 pear 发送电子邮件。这是我的代码

<?php
require_once "/usr/share/pear/Mail.php";
require_once "/usr/share/pear/Mail/mime.php";

$to = "Info <info@mydomain.com>";
$subject = "Contact Form - mydomain.com\r\n\r\n";

$host = "smtp.zoho.com";
$username = "noreply@mydomain.com";
$password = "abc@123";
$port = "465";

//Sender Details
$sender_name = $_POST['name'];
$from = $_POST['name']." <".$_POST['email'].">";
$sender_phone = $_POST['phone'];

//Create Message
$body = $_POST['message'];
//$body = wordwrap($body, 70, "\r\n");
$body = $body . "\r\n" . "Phone: " .$sender_phone;

if($sender_name != "" && $_POST['email'] != "" && $body != "" && $sender_phone != "")
{
    $headers = array ('From' => $from,
      'To' => $to,
      'Subject' => $subject,
      'Reply-To: ' => $from);
    $smtp = Mail::factory('smtp',
      array ('host' => $host,
        'port' => $port,
        'auth' => true,
        'username' => $username,
        'password' => $password));

    $mail = $smtp->send($to, $headers, $body);
    if (PEAR::isError($mail)) {
      echo("<p>" . $mail->getMessage() . "</p>");
    } else {
      header("Location:contact-us.php?mcode=1");
    }
}
else
{
    header("Location:contact-us.php?mcode=2");
}
?>

现在这段代码在浏览器中给了我以下错误:

当我检查我的 php error_log 时,我发现了这个:

[Tue Dec 17 08:46:56 2013] [notice] child pid 7416 exit signal Segmentation fault (11) [Tue Dec 17 08:47:44 2013] [error] [client 117.198.137.57] PHP Notice: Undefined index: name in /var/www/html/contact-us-process.php on line 14 [2013 年 12 月 17 日星期二 08:47:44] [错误] [客户端 117.198.137.57] PHP 堆栈跟踪: [2013 年 12 月 17 日星期二 08:47:44] [错误] [客户端 117.198.137.57] PHP 1. {main}() /var/www/html/contact-us-process.php:0 [2013 年 12 月 17 日星期二 08:47:44] [错误] [客户端 117.198.137.57] PHP 注意:未定义索引:/var/www/html/contact-us-process.php 中的名称在第 15 行 [2013 年 12 月 17 日星期二 08:47:44] [错误] [客户端 117.198.137.57] PHP 堆栈跟踪: [2013 年 12 月 17 日星期二 08:47:44] [错误] [客户端 117.198.137.57] PHP 1. {main}() /var/www/html/contact-us-process.php:0 [Tue Dec 17 08:47:44 2013] [error] [client 117.198.137.57] PHP Notice: Undefined index: email in /var/www/html/contact-us-process.php on line 15 [2013 年 12 月 17 日星期二 08:47:44] [错误] [客户端 117.198.137.57] PHP 堆栈跟踪: [2013 年 12 月 17 日星期二 08:47:44] [错误] [客户端 117.198.137.57] PHP 1. {main}() /var/www/html/contact-us-process.php:0 [Tue Dec 17 08:47:44 2013] [error] [client 117.198.137.57] PHP Notice: Undefined index: phone in /var/www/html/contact-us-process.php on line 16 [2013 年 12 月 17 日星期二 08:47:44] [错误] [客户端 117.198.137.57] PHP 堆栈跟踪: [2013 年 12 月 17 日星期二 08:47:44] [错误] [客户端 117.198.137.57] PHP 1. {main}() /var/www/html/contact-us-process.php:0 [Tue Dec 17 08:47:44 2013] [error] [client 117.198.137.57] PHP Notice: Undefined index: message in /var/www/html/contact-us-process.php on line 19 [2013 年 12 月 17 日星期二 08:47:44] [错误] [客户端 117.198.137.57] PHP 堆栈跟踪: [2013 年 12 月 17 日星期二 08:47:44] [错误] [客户端 117.198.137.57] PHP 1. {main}() /var/www/html/contact-us-process.php:0 [Tue Dec 17 08:47:45 2013] [notice] child pid 6887 exit signal Segmentation fault (11) [Tue Dec 17 08:50:26 2013] [notice] child pid 7414 exit signal Segmentation fault (11) [2013 年 12 月 17 日星期二 09:35:48] [错误] [客户端 117.198.124.73] 文件不存在:/var/www/html/favicon.ico [Tue Dec 17 09:39:46 2013] [notice] child pid 8617 exit signal Segmentation fault (11)

我正在使用带有 Zoho 电子邮件系统的 Rackspace 云。

当我这样做时:

[root@mydomain /]# find -name Mail.php
./usr/share/pear/Mail.php

这就是为什么我直接将它包含在 php 文件中,

当我这样做的时候,

[root@mydomain /]# find -name mime.php
./usr/share/pear/Mail/mime.php

所以我使用了require_once "/usr/share/pear/Mail/mime.php",但是这是在一个例子中,我不太清楚我为什么使用这条线。

这可能是什么原因,我该如何解决?

【问题讨论】:

  • 我可以看看你的表单html代码吗? php 没有从提交的表单中获取 post 值
  • @crack 页面获取数据,当我删除 require_once "/usr/share/pear/Mail.php"; require_once "/usr/share/pear/Mail/mime.php"; 它回显一切正常
  • Chrome 加载器也会旋转几分钟,然后出现这个 chrome 错误,[有问题的屏幕截图]
  • 通常 "/usr/share/pear/Mail.php" 这不应该对 www-data 或 apache2 用户具有访问权限。实际上你不需要使用这个require_once "/usr/share/pear/Mail.php"; require_once "/usr/share/pear/Mail/mime.php"; 来发送电子邮件。您只需要安装 postfix(邮件服务器)和邮件功能即可发送电子邮件。如果您想使用 stml,那么我建议您使用 phpmailer。无需这些必需文件即可使用
  • Django - 这是您在 PHP 代码引发 SegFaults 的地方发布的第二个问题。显然,您的服务器/安装有问题。可能值得重建并重新部署以获得全新的状态。

标签: php email smtp pear rackspace-cloud


【解决方案1】:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';

$mail->Host       = "mail.example.com"; // SMTP server example
$mail->SMTPDebug  = 0;                     // enables SMTP debug information (for testing)
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Port       = 25;                    // set the SMTP port for the GMAIL server
$mail->Username   = "username"; // SMTP account username example
$mail->Password   = "password";        // SMTP account password example

您可以在此处找到有关 PHPMailer 的更多信息:https://code.google.com/a/apache-extras.org/p/phpmailer/

【讨论】:

  • 但是这样会导致致命错误PHP Fatal error: Class 'Mail' not found in /var/www/html/contact-us-process.php on line 26
  • 我有请求,尝试使用 phpmailer 发送电子邮件的救命库
  • 别忘了在你的机器上安装 postfix
  • 但是我在使用 phpmailer SMTP Error: data not accepted. 时遇到了错误,谷歌搜索了一段时间后,我找不到任何具体的解决方案。你能帮忙吗?
  • 调试后发现这是一个完全错误command failed: 553 Relaying disallowed SMTP Error: data not accepted. Mailer Error: SMTP Error: data not accepted.
猜你喜欢
  • 2011-05-11
  • 2017-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-30
  • 2012-05-29
  • 1970-01-01
相关资源
最近更新 更多