为什么我的 php 邮件程序不能在 bamboozle 上运行,但可以在 hostinger 上运行?

Why my php mailer doesn't work on bamboozle but it works on hostinger?

今天我花了几个小时试图让我的 office 365 邮件在我的 php 邮件程序上使用 smtp tls,直到它最终在我的主机上工作但在 bamboozle 上不起作用,这就是我得到的:

无法发送消息 sent.Mailer 错误:SMTP connect() 失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

我正在使用以下 php 邮件程序。请记住,我通过 visual studio 终端安装了 php 邮件程序,它在托管程序上运行,所以同一文件夹中的相同代码在 bamboozle 上不起作用。

<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
$mail->isSMTP();                                            //Send using SMTP
$mail->Host       = 'smtp.example.com';                     //Set the SMTP server to send 
 through
$mail->SMTPAuth   = true;                                   //Enable SMTP authentication
$mail->Username   = 'user@example.com';                     //SMTP username
$mail->Password   = 'secret';                               //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
$mail->Port       = 465;                                    //TCP port to connect to; use 587 
if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

//Recipients
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');     //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');

//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name

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

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

请注意,我确实删除了 $mail->isSMTP();,但仍然出现相同的错误:/

我之前在构建联系表单时遇到过这个问题。

它在我的机器上运行良好,但是当我在 000webhost 上托管应用程序时,问题又开始出现了。

解决方案是:

  • STMPSecure 设置为 tls
  • 将端口更改为 587
$mail->SMTPSecure = 'tls'; 
$mail->Port       = '587';

这是我提到的工作代码:

// Initialize Object From PhpMailer Library.
$mail = new PHPMailer();


//Server settings
$mail->SMTPDebug = 0;                                       //Disable Debugging Output
$mail->isSMTP();                                            //Send using SMTP
$mail->Host       = 'smtp.gmail.com';                       //Set the SMTP server to send through
$mail->SMTPAuth   = true;                                   //Enable SMTP authentication
$mail->Username   = $target;                                //SMTP username
$mail->Password   = $pass;                                  //SMTP Password
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

// Content Settings
$mail->isHTML(true);
$mail->CharSet = "UTF-8";

希望也能帮助您解决问题 3>