PHPMailer:Godaddy上的SMTP主机连接错误

时间:2016-11-23 11:32:52

标签: php

  

邮件程序错误:SMTP错误:无法连接到SMTP主机。;

当我在godaddy中运行此代码时会生成此错误。

<?php
require("PHPMailer_5.2.0/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host     = "smtpout.secureserver.net"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username@domain.com"; // SMTP username
$mail->Password = "******"; // SMTP password
$mail->From     = "username@domain.com";
$mail->FromName = "User";
$mail->AddAddress("Sendto@gmail.com"); // name is optional
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "Here is the subject";
$mail->Body    = "This is the HTML message body <b>in bold!</b>";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
if (!$mail->Send()) {
    echo "Message could not be sent. <p>";
    echo "Mailer Error: " . $mail->ErrorInfo;
    exit;
}
echo "Message has been sent";
?>

4 个答案:

答案 0 :(得分:0)

如果我理解正确,使用您正在使用的SMTP地址,您也需要指定端口(465)。

但是,根据godaddy文档,您应该使用以下SMTP服务器:

relay-hosting.secureserver.net

答案 1 :(得分:0)

此代码可能会对您有所帮助。

$mail->Host     = "smtpout.secureserver.net";

您的主机名应该与mydomain.com

类似
$mail->Port = 465;

答案 2 :(得分:0)

我使用localhost作为smtp服务器,端口25,没有SSL和没有身份验证来解决此问题。 我在Godaddy服务器上使用具有divi主题的wordpress。

答案 3 :(得分:0)

试试下面的代码,它对我有用!

<?php
require("src/PHPMailer.php");
require("src/SMTP.php");
require("src/Exception.php");

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

$mail = new PHPMailer(true);

try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->SMTPAuth = false;
$mail->SMTPAutoTLS = false;
$mail->Port = 25;

//Recipients
$mail->setFrom('from@gmail.com', 'Mailer');
$mail->addAddress('to@gmail.com', 'XYZTABC');
//Content
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
    echo "<pre>";
    print_r($e);
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}