PHP PEAR 邮件问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2284468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problem with PHP PEAR Mail
提问by Kenny
I am trying to use PEAR Mail to send using an external smtp server. It seems to hang for a while, then the script ends. It outputs all of my "echo" statements up till the one after the send. Nothing is output past the echo that says "before send". Can anyone tell me what might be wrong here? (dummy values substituted for smtp values). Mail is not being sent. Thanks for helping!
我正在尝试使用 PEAR Mail 使用外部 smtp 服务器发送。好像挂了一段时间,然后脚本结束。它输出我所有的“回声”语句,直到发送后的那个语句。没有任何输出超过说“发送前”的回声。谁能告诉我这里可能有什么问题?(虚拟值代替 smtp 值)。未发送邮件。感谢您的帮助!
echo "start";
$n = $_POST['txtName'];
$e = $_POST['txtEmail'];
$t = 'Kenny <[email protected]>';
$f = 'Kenny <[email protected]>';
$s = 'CPA TEST';
$b = "name: $n email: $e";
include("mail.php");
echo "after include";
/* mail setup recipients, subject etc */
$recipients = $t;
$headers["From"] = $f;
$headers["To"] = $t;
$headers["Subject"] = $s;
$mailmsg = $b;
/* SMTP server name, port, user/passwd */
$smtpinfo["host"] = "my_smtp_host";
$smtpinfo["port"] = "25";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "my_email";
$smtpinfo["password"] = "my_password";
echo "before object";
/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory("smtp", $smtpinfo);
echo "before send";
/* Ok send mail */
$send = $mail_object->send($recipients, $headers, $mailmsg);
echo "after send";
if (PEAR::isError($send)) { print($send->getMessage());}else{print "end";}
echo "done";
采纳答案by Todd Moses
Try this to ensure your mail is working:
试试这个以确保您的邮件正常工作:
<?php
require_once "Mail.php";
$from = "Sandra Sender <[email protected]>";
$to = "Ramona Recipient <[email protected]>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "mail.example.com";
$username = "smtp_username";
$password = "smtp_password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
If this does not work then you will need to check your PHP Configuration.
如果这不起作用,那么您需要检查您的 PHP 配置。
See http://php.net/manual/en/function.mail.phpfor more information.
有关更多信息,请参阅http://php.net/manual/en/function.mail.php。
回答by Vynz
If someone is running on linux and runs into the same problem as Jayme. Here is another simple solution for installing the missing "Net/" classes. These missing classes causes the script to interrupt.
如果有人在 linux 上运行并遇到与 Jayme 相同的问题。这是安装缺少的“Net/”类的另一个简单解决方案。这些缺失的类会导致脚本中断。
sudo pear install Net_SMTP
sudo pear install Net_SMTP
回答by Jayme Dunlap
I ran into the same issue where it hung on the send command. My first step was to run from the command line to see the full error message (as Rap suggested above).
我遇到了同样的问题,它挂在发送命令上。我的第一步是从命令行运行以查看完整的错误消息(如 Rap 上面建议的那样)。
php mymailsample.php
It spit out the following
它吐出以下内容
Warning: include_once(Net/SMTP.php): failed to open stream: No such file or directory in mail/Mail/smtp.php on line 348
PHP Warning: include_once(): Failed opening 'Net/SMTP.php' for inclusion (include_path='.:/usr/lib/php') in mail/Mail/smtp.php on line 348
PHP Fatal error: Class 'Net_SMTP' not found inmail/Mail/smtp.php on line 349
I downloaded the following, and put them in /Net
我下载了以下内容,并将它们放在 /Net 中
http://pear.php.net/package/Net_SMTP/download
http://pear.php.net/package/Net_Socket/download
http://pear.php.net/package/Net_SMTP/download
http://pear.php.net/package/Net_Socket/download
I had to adjust the permissions of the SMTP and Socket libraries so they could be read by Apache.
我必须调整 SMTP 和 Socket 库的权限,以便 Apache 可以读取它们。
And voila, it worked!
瞧,它奏效了!
回答by ericL
The reinstall of channel://pear.php.net/Mail-1.2.0 does'nt work for me.(Ubuntu 12.04)
重新安装 channel://pear.php.net/Mail-1.2.0 对我不起作用。(Ubuntu 12.04)
Just remove the caracter "&" before new
只需在新之前删除字符“&”
sudo vi /usr/share/php/Mail/smtp.php
349 /*$this->_smtp = &new Net_SMTP($this->host, */
350 $this->_smtp = new Net_SMTP($this->host,
回答by Rap
Todd's script is excellent, but will not solve your problem which is that your browser times out before the SMTP does. That is why you only see half your page and are not seeing any error messages that you can use to debug your SMTP settings.
Todd 的脚本非常好,但不能解决您的问题,即您的浏览器在 SMTP 之前超时。这就是为什么您只能看到一半的页面,而没有看到任何可用于调试 SMTP 设置的错误消息。
The solution is to run the PHP script directly. No timeout.
解决办法是直接运行PHP脚本。没有超时。
If you can't do that because your ISP doesn't give you shell access, create a cron job to run every minute. Cron will email the output to you which will have full debug details.
如果因为您的 ISP 没有给您 shell 访问权限而不能这样做,请创建一个每分钟运行一次的 cron 作业。Cron 会将输出通过电子邮件发送给您,其中包含完整的调试详细信息。
回答by Hymanson Leung
I've actually encountered the same problem. A production script broke down, and I had the hardest time troubleshooting it. Mainly because there was so much code that prevented errors from showing up. Ultimately, I used Kenny Ray's code, modified it to work for my environment, and ran a test. It turned out the Net_Socket somehow disappeared. I've uninstalled and re-installed it, and everything started working again. I hope this helps you.
我实际上遇到了同样的问题。一个生产脚本出现故障,我遇到了最困难的时间进行故障排除。主要是因为有太多代码可以防止出现错误。最终,我使用了 Kenny Ray 的代码,对其进行了修改以适用于我的环境,并进行了测试。原来 Net_Socket 不知何故消失了。我已经卸载并重新安装了它,一切又开始工作了。我希望这可以帮助你。

