简体   繁体   English

PHPMailer似乎不能在实时站点上工作,但可以在localhost上工作?

[英]PHPMailer doesn't seeem to work on live site, but works on localhost?

I have a PHPMailer script, and I've checked all the other threads that are similar to this, and tried all the solutions that follow the question. 我有一个PHPMailer脚本,并且检查了所有与此相似的其他线程,并尝试了解决该问题的所有解决方案。 But here's the error I'm receiving 但是这是我收到的错误

2014-01-03 02:25:16 SMTP ERROR: Failed to connect to server: Connection timed out (110) SMTP connect() failed. Mailer Error: SMTP connect() failed.

And here's my script: 这是我的脚本:

$body = "test";

$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // I have also tried tls
$mail->SMTPKeepAlive = true;            
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // I have also tried 25
$mail->Username   = "xxxx@gmail.com";  // GMAIL username
$mail->Password   = "xxxxx";            // GMAIL password

$mail->SetFrom("johndoe@gmail.com","John  Doe");

$mail->AddReplyTo("johndoe@gmail.com","John Doe");

$mail->Subject    = "Booking from " . $_POST['person'] . ' at ' . $_POST['name'];

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test


$mail->MsgHTML($body);

$address = "johndoe@hotmail.co.uk";
$mail->AddAddress($address, "John Doe");

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

However I just cannot get it to work. 但是我只是无法使它工作。 I've tried to many different solutions. 我尝试了许多不同的解决方案。 I'm even happy to use the default php mail however I just need it to send, the php mail wouldn't even get past the hotmail spam filters (by that I mean it didn't even send it to spam). 我什至很高兴使用默认的php邮件,但是我只需要发送它,php邮件甚至都不会通过hotmail垃圾邮件过滤器(这意味着我什至没有将其发送到垃圾邮件)。

Any solutions or suggestions here? 这里有任何解决方案或建议吗? Thanks a lot! 非常感谢!

您是否确定实时服务器和localhost服务器上的php.ini邮件设置相同?

Install PHP Mailer From 从安装PHP Mailer

https://github.com/Synchro/PHPMailer https://github.com/Synchro/PHPMailer

And Read "A Simple Example" 并阅读“一个简单的例子”

<?php
require_once('PHPMailer/PHPMailerAutoload.php'); // Load your PHPMailerAutoload.php Correctly Here

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;// debugging: 1 = errors and messages, 2 = messages only                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'yourmail@gmail.com';                 // SMTP username
$mail->Password = '**********';                           // SMTP password
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail  // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;           // or 587                         // TCP port to connect to

$mail->setFrom('fromemail@gmail.com', 'Vijay Rami');
$mail->addAddress('toemail@gmail.com', 'Vijay Rami');     // Add a recipient
//$mail->addAddress('ellen@example.com');               // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.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;
} else {
    echo 'Message has been sent';
}

只需评论这一行:

$mail->IsSMTP();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM