doupeng5320 2018-06-02 13:45
浏览 267
已采纳

PHPMailer不工作:有没有新方法这样做?

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: https://unidrones.co.za/JuneSecond

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:

<?php
require 'PHPMailerAutoload.php';
if(isset($_POST['send']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$to_id = $_POST['toid'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $email;
$mail->Password = $password;
$mail->addAddress($to_id);
$mail->Subject = $subject;
$mail->msgHTML($message);
if (!$mail->send()) {
$error = "Mailer Error: " . $mail->ErrorInfo;
echo '<p id="para">'.$error.'</p>';
}
else {
echo '<p id="para">Message sent!</p>';
}
}
else{
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://smtp.gmail.com';
    $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('myname@gmail.com', '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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题