doupeng5320 2018-06-02 13:45
浏览 267


I followed all of the instructions in this question:

SMTP connect() failed PHPmailer - PHP

But still I never succeeded in getting the PHPMailer to work. I searched elsewhere - no solutions.

You can view the results here:

When I try to send a test email using my gmail account credentials, it returns the "SMTP connect() failed" error.

I am using this template code:

require 'PHPMailerAutoload.php';
$email = $_POST['email'];
$password = $_POST['password'];
$to_id = $_POST['toid'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$mail = new PHPMailer;
$mail->Host = 'ssl://';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $email;
$mail->Password = $password;
$mail->Subject = $subject;
if (!$mail->send()) {
$error = "Mailer Error: " . $mail->ErrorInfo;
echo '<p id="para">'.$error.'</p>';
else {
echo '<p id="para">Message sent!</p>';
echo '<p id="para">Please enter valid data</p>';

Edit: I don't know if there's a new way to send emails through PHP now. All the tutorials and lessons I am using teaches it this way (i.e. using the PHPMailer library).

I had a tough time finding the PHPMailer library that includes the PHPMailerAutoload.php file, which makes me think it's a little outdated or deprecated, but how else would I send emails? I don't know.

  • 写回答

3条回答 默认 最新

  • dongyong2906 2018-06-02 14:53

    The reason you're having a hard time finding PHPMailerAutoload.php is because it's old and no longer supported. Get the latest and base your code on the gmail example provided. If you're new to PHP, learn to use composer.

    These three lines are conflicting:

    $mail->Host = 'ssl://';
    $mail->Port = 587;
    $mail->SMTPSecure = 'tls';

    The ssl:// in Host overrides the tls in SMTPSecure, resulting in it trying to use implicit TLS to a port expecting explicit TLS. Either use ssl with port 465 or tls with port 587, not other combos. Regardless, it appears that's not your problem anyway.

    As the troubleshooting guide says about this exact "SMTP connect() failed" error:

    This is often reported as a PHPMailer problem, but it's almost always down to local DNS failure, firewall blocking (for example as GoDaddy does) or another issue on your local network. It means that PHPMailer is unable to contact the SMTP server you have specified in the Host property, but doesn't say exactly why.

    It then goes on to describe several techniques you can use to try to diagnose exactly why you can't connect. Amazing stuff, documentation.

    I tried your form with some random data and saw that you failed to include the most important error message in your question:

    2018-06-02 14:47:25 SMTP ERROR: Failed to connect to server: Network is unreachable (101)
    2018-06-02 14:47:25 SMTP connect() failed

    That suggests your ISP is probably blocking outbound SMTP, so you have your answer - perhaps confirm that using the steps the guide suggests (telnet etc), and refer to your ISP's docs or support.

    You also have a major omission - you're not setting a "from" address:

    $mail->setFrom('', 'My Name');

    Note that if you're sending through gmail, you can only use your account's address, or preset aliases (set in gmail prefs), not arbitrary addresses.

    Meanwhile, this is a somewhat crazy thing to implement as you have anyway - why would anyone ever enter their gmail credentials on a form like that?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 计算300m以内的LD衰减
  • ¥15 Lxml库中Xpath查找指定节点
  • ¥15 数据爬取,python
  • ¥15 怎么看 cst中一个面的功率分布图,请说明详细步骤。类似下图
  • ¥15 为什么我的pycharm无法用pyqt6的QtWebEngine
  • ¥15 FOR循环语句显示查询超过300S错误怎么办
  • ¥15 数电设计题 没有设计思路 不知道用什么芯片进行设计 求提供设计思路
  • ¥60 设计一种优化算法结合案例给出智能仓储四向穿梭车的调度计划
  • ¥15 Errno2:No such file or directory,在当前文件确实没有该图片,怎么解决?
  • ¥15 博世摄像头数据存储的问题(iscsi)