使用PHP邮件程序在php中发送邮件时无法连接

时间:2013-08-12 07:47:31

标签: php gmail

我从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.

请让我知道这个问题,因为我看过其他帖子......但我没有得到任何提示

2 个答案:

答案 0 :(得分:0)

只需替换

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

此处无需指定SSL。

答案 1 :(得分:0)

使用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

获得完整的帮助
相关问题