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条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改