简体   繁体   English

PHP Mailer SMTP 错误:无法连接到服务器:(0) 但 SMTP 测试器工作

[英]PHP Mailer SMTP ERROR: Failed to connect to server: (0) but SMTP tester works

I'm trying to send a mail with PHP Mailer from my website using SMTP.我正在尝试使用 SMTP 从我的网站发送带有 PHP Mailer 的邮件。

I have the host, username, port, and SSL config all set up but for some reasing it's throwing:我已经设置了主机、用户名、端口和 SSL 配置,但出于某些原因,它正在抛出:

SMTP ERROR: Failed to connect to server:  (0)

However, when using the exact same parameters in a testing tool like https://www.smtper.net/ it works and sends the mail correctly.但是,当在像https://www.smtper.net/这样的测试工具中使用完全相同的参数时,它可以正常工作并正确发送邮件。

Any ideas?有任何想法吗?

EDIT - Code:编辑 - 代码:

$smtpHost = "my.host.com";
$smtpUser = "info@mydomain.com";
$smtpPass = "mypass1234";

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Port = 587; //Tried with 465 too
$mail->SMTPSecure = 'ssl'; //Tried with true too
$mail->IsHTML(true);
$mail->CharSet = "utf-8";

$mail->Host = $smtpHost;
$mail->Username = $smtpUser;
$mail->Password = $smtpPass;
$mail->SMTPDebug  = 2;

$mail->From = $smtpUser;
$mail->FromName = 'Some name';
$mail->AddAddress('someemail@example.com');

$mail->Subject = 'Some subject';
$body = "<h1>Some HTML</h1>";

$mail->Body = $body;

// I tried with and without this part
$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

$mailSent = $mail->Send();
var_dump($mailSent);

I'd point you towards two examples from PHPMailer's github:我会向您指出 PHPMailer 的 github 中的两个示例:

https://github.com/PHPMailer/PHPMailer/blob/master/examples/smtp.phps - SMTP w/ Authentication (check that your PHP time zone is set, as they mention) https://github.com/PHPMailer/PHPMailer/blob/master/examples/smtp.phps - 带有身份验证的 SMTP(检查您的 PHP 时区是否设置,正如他们所提到的)

https://github.com/PHPMailer/PHPMailer/blob/master/examples/ssl_options.phps - Using PHP to adjust the SSL settings if they are incorrect on the mail server https://github.com/PHPMailer/PHPMailer/blob/master/examples/ssl_options.phps - 如果邮件服务器上的 SSL 设置不正确,则使用 PHP 调整 SSL 设置

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

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