PHP邮件程序不在cpanel上工作

时间:2016-04-21 08:37:19

标签: php email localhost phpmailer cpanel

我的php邮件程序在localhost上工作正常,但是当我在cpanel上运行相同的代码时,我收到错误消息:SMTP connect()失败

<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();                                     
$mail->Host = "tls://smtp.gmail.com"; 
$mail->SMTPAuth = true;                               
$mail->Username = '********@gmail.com';                 
$mail->Password = '********';                           

$mail->Port = 587;                                   

$mail->setFrom('sender@gmail.com', 'Mailer');
$mail->AddAddress('receiver@gmail.com', 'Joe User');    
$mail->addReplyTo('sender@gmail.com', 'Information');

$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';

if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>

请帮助我,我出错了?

4 个答案:

答案 0 :(得分:2)

默认情况下,CPanel会阻止访问外部SMTP服务器。

在whm&gt;中禁用此限制<安全中心> SMTP限制禁用

这有效

<?php
require_once('./class.phpmailer.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 = "smtp.mail.yahoo.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "xxxxxx@ymail.com";
$mail->Password = "xxxxxx";
$mail->SetFrom("xxxxx@ymail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("xxxxx@ymail.com");

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

答案 1 :(得分:1)

您可能忘记在GMail帐户中启用SMTP访问(这是设置中的IMAP访问的一部分)。

此外,“tls://smtp.gmail.com”不是有效的SMTP服务器地址。如果您想使用TLS,请使用$mail->SMTPSecure = "tls";

答案 2 :(得分:0)

感谢您的建议,非常感谢: - )

我得到的解决方案如下。

1>give Absolute path for PHPMailerAutoload.php
2>host name as "localhost"
3>create dummy emailId on server



<?php
require'/home/username/public_html/phpmailertesting/PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug =3;                               // Enable verbose debug output
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'localhost';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'info@hostname.com';                 // SMTP username
$mail->Password = '*****';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to
$mail->setFrom('info@hostname.com', 'Mailer');
$mail->addAddress('*******@gmail.com');               // Name is optional
 //$mail->addReplyTo('info@hostname.com', 'Information');
 //$mail->addCC('*******@gmail.com');
 //$mail->addBCC('bcc@example.com');
//$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$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.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit();
} else {
echo 'Message has been sent';
}
?>

答案 3 :(得分:0)

您需要从ISP公司购买Cpanel / WHM,才能访问Gmail等外部SMTP电子邮件。首先,禁用: WHM>安全中心> SMTP限制已禁用。
并确保已启用“ CSF防火墙为” SMTP_BLOCK”设置。 如果您在安全性设置中使用Gmail,则启用较少的安全性,即可进行smtp身份验证。 PHPMailer建议对所有防火墙和安全性问题使用SSL。