4

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

这是我的代码。我正在使用 Gmail 帐户。

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!";
}
?>

当我运行我的文件时,出现此错误:

警告:fopen(contents.html) [function.fopen]: 无法打开流:第 1870 行的 D:\xampplite\htdocs\WebEng\class.phpmailer.php 中没有这样的文件或目录

警告:fsockopen() [function.fsockopen]:无法连接到 ssl://smtp.gmail.com:465(无法找到套接字传输“ssl” - 您在配置 PHP 时是否忘记启用它?) D:\xampplite\htdocs\WebEng\class.smtp.php 在第 122 行邮件程序错误:SMTP 错误:无法连接到 SMTP 主机。

4

2 回答 2

4

您的 PHP 安装(从外观上看是 XAMPP)不支持 SSL。确保线路

extension=php_openssl.dll

没有在您的 php.ini 中注释掉,重新启动 Apache,如果仍然不起作用,请尝试将 PHP 目录中的 ssleay32.dll 和 libeay32.dll 覆盖(或复制)到 Apache 的二进制 (.exe) 目录中,然后重新启动 Apache

于 2009-04-09T21:02:54.427 回答
4

首先,当您收到错误消息时,那就太好了!因为在 90% 的情况下,您会发现其他人也有过它们,因此您会在 Internet 上找到有关此错误消息的大量信息。

因此,当收到您还不知道的错误消息时,第 1 步总是打开 google 并将其复制粘贴到那里。但是,请取出与您的系统唯一连接的任何路径或其他东西。

然后关于你的错误。特别是 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 太多!

于 2009-04-09T21:29:12.833 回答