[英]PHP mailer not working on cpanel
我的 php 郵件程序在本地主機上運行良好,但是當我在 cpanel 上運行相同的代碼時,我收到錯誤消息:SMTP 連接()失敗
<?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';
}
?>
請幫助我,我哪里出錯了?
CPanel 默認阻止訪問外部 SMTP 服務器。
在 whm > security center > SMTP Restrictions disable 中禁用此限制
這有效
<?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";
}?>
您可能忘記為 GMail 帳戶啟用 SMTP 訪問(這是設置中 IMAP 訪問的一部分)。
此外,“tls://smtp.gmail.com”不是有效的 SMTP 服務器地址。 使用$mail->SMTPSecure = "tls";
如果你想使用 TLS。
感謝您的建議,真的很感激:-)
我得到的解決方案如下。
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';
}
?>
您需要從 ISP 公司購買 Cpanel/WHM 才能訪問 Gmail 等外部 SMTP 電子郵件。 首先,禁用: WHM > 安全中心 > SMTP 限制是禁用。
並確保啟用 CSF 防火牆“SMTP_BLOCK”設置。 如果您在安全設置中使用 Gmail,則啟用較低的安全性,您可以通過 smtp 身份驗證。 PHPMailer 建議對所有防火牆和安全問題使用 SSL。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.