【问题标题】:unable to connect while sending the mail in php using the PHP mailer使用 PHP 邮件程序在 php 中发送邮件时无法连接
【发布时间】:2015-01-05 19:01:36
【问题描述】:

我从https://github.com/PHPMailer/PHPMailer 下载了一个邮件文件,并尝试在我的程序中使用它来发送电子邮件,但它导致我出现了一些错误。首先让我们看一下代码

include 'PHPMailer-master/class.phpmailer.php';
include 'PHPMailer-master/class.smtp.php';

$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 587; // or 587
$mail->IsHTML(true);
$mail->Username = "........@gmail.com";
$mail->Password = "*****";
$mail->SetFrom('.....@gmail.com');
$mail->Subject = "Test";
$mail->Body = "this the mail for subscription";
$mail->AddAddress('......@gmail.com');
 if(!$mail->Send())
 {
     echo "Mailer Error: " . $mail->ErrorInfo;
 }
 else
 {
     echo "Message has been sent";
 }
?>

运行此代码后,我收到错误消息:

SMTP -> ERROR: Failed to connect to server: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? (1546610735)SMTP Connect() failed. Mailer Error: SMTP Connect() failed.

请让我知道问题所在,因为我已经看到其他帖子...但我没有得到任何提示

【问题讨论】:

标签: php gmail


【解决方案1】:

只需替换

$mail->Host = 'smtp.gmail.com';

此处无需指定 SSL。

【讨论】:

  • 现在我得到的错误是:CLIENT -> SMTP: EHLO localhost CLIENT -> SMTP: STARTTLS 警告:stream_socket_enable_crypto(): 此流不支持 C:\xampp\htdocs\ 中的 SSL/加密program\mailsending1\mailsending_v1\PHPMailer-master\class.smtp.php 在第 254 行 CLIENT -> SMTP: quit SMTP -> ERROR: SMTP server denied quit command: SMTP Connect() failed。邮件程序错误:SMTP Connect() 失败。
  • 你删除了 ;在这一行的 php.ini 中? extension=php_openssl.dll
  • 删除它,保存文件并重新启动xamp服务器。
【解决方案2】:

使用 phpmailer 发送邮件,使用如下代码

require_once('../class.phpmailer.php');

$mail             = new PHPMailer(); // defaults to using php "mail()"

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");

$mail->Subject    = "PHPMailer Test Subject via mail(), basic";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";

//可选,注释掉并测试

$mail->MsgHTML($body);

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

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

您可以从here获得完整的帮助

【讨论】:

    猜你喜欢
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    • 2020-04-19
    • 1970-01-01
    相关资源
    最近更新 更多