PHP发送邮件错误

4

请帮助我,我是PHP新手,过去5个小时一直在尝试发送邮件,现在真的很累了。谢谢。

Here is my code. I am using Gmail account.

include("class.phpmailer.php");
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body             = $mail->getFile('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP();
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server

$mail->Username   = "hussaintalha@gmail.com";  // GMAIL username
$mail->Password   = "xxxxxxxx";            // GMAIL password

$mail->AddReplyTo("hussaintalha@gmail.com","First Last");

$mail->From       = "hussaintalha@gmail.com.com";
$mail->FromName   = "First Last";

$mail->Subject    = "PHPMailer Test Subject via gmail";

//$mail->Body       = "Hi,<br>This is the HTML BODY<br>";                      //HTML Body
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap   = 50; // set word wrap

$mail->MsgHTML($body);

$mail->AddAddress("hussaintalha@gmail.com", "John Doe");

$mail->AddAttachment("images/phpmailer.gif");             // attachment

$mail->IsHTML(true); // send as HTML

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}
?>

When I run my file I get This Error:

Warning: fopen(contents.html) [function.fopen]: failed to open stream: No such file or directory in D:\xampplite\htdocs\WebEng\class.phpmailer.php on line 1870

Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:465 (Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?) in D:\xampplite\htdocs\WebEng\class.smtp.php on line 122 Mailer Error: SMTP Error: Could not connect to SMTP host.


大声喊叫并不礼貌。正确的强调方式是使用粗体或斜体。然而,我认为你用全大写字母写的内容并不需要强调。 - Calvin
2个回答

4
你的PHP安装(通过看起来是XAMPP)不支持SSL。请确保以下这行代码:
extension=php_openssl.dll

如果在您的php.ini文件中未注释掉,请重新启动Apache,如果仍然无法解决,请尝试将PHP目录中的ssleay32.dll和libeay32.dll覆盖(或复制)到Apache的二进制(.exe)目录中,然后重新启动Apache。


4
首先,当您收到错误消息时,这很好!因为在90%的情况下,您会发现其他人也遇到了这些问题,因此您可以在互联网上找到有关此错误消息的大量信息。
所以,第一步是当您收到一个您不知道的错误消息时,总是打开谷歌并将其复制粘贴到搜索框中。但是,请删除与您的系统唯一相关的任何路径或其他内容。
然后谈谈您的错误。特别是xampp light不支持SSL。也许您可以先尝试一个更简单的sendmail示例。像一个非常小的,然后逐步增加它。
这就是我在不知道为什么某些东西不起作用时经常做的事情。我从一行开始看它的表现,然后再添加另一行,依此类推。
假设您从以下内容开始并查看它是否有效:
<?php

include("class.phpmailer.php");

$mail             = new PHPMailer();

$mail->IsSMTP();
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "hussaintalha@gmail.com";  // GMAIL username
$mail->Password   = "xxxxxxxx";            // GMAIL password

$mail->From       = "hussaintalha@gmail.com";
$mail->Subject    = "PHPMailer Test Subject via gmail";
$mail->Body       = "Hi, this is a test";           
$mail->AddAddress("hussaintalha@gmail.com", "Hussain");

$mail->send();
?>

顺便提一下,你的邮件发件人地址多了一个.com!


我尝试运行了你的代码,没有出现任何错误,但是我也没有收到任何电子邮件。请问在XAMPP中是否有任何队列文件夹,可以检查该代码是否正在运行,就像我们在IIS中所拥有的一样。非常感谢您的帮助,请继续指导我,谢谢。 - Talha Bin Shakir
你有任何地方可以托管代码并在线测试吗?xampp自带Mercury(邮件服务器),但我不确定轻量版是否也有。我通常使用Mercury,并将所有内容转发到tester@localhost,然后在我的电子邮件客户端中创建一个POP帐户。 - markus
不,我没有任何托管。那么怎么知道呢?非常感谢您的帮助,我会检查一下,如果找不到,我会再联系您的。 - Talha Bin Shakir
当然,便宜与否取决于你的背景,但共享主机账户被认为是非常便宜的!如果你想认真学习PHP等编程语言,你应该考虑购买其中之一作为入门! - markus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接