Failed to connect to server: Connection refused (111)

拥有回忆 提交于 2019-12-04 13:35:03

try this its working fine

<?php
    include "emails/PHPMailer/PHPMailerAutoload.php"; 

    //Create a new PHPMailer instance
    $mail = new PHPMailer(); 

    $mail->IsSMTP(); 
    $mail->SMTPDebug = 1; 
    $mail->SMTPAuth = true; 
    $mail->SMTPSecure = 'ssl'; 
    $mail->Host = "smtp.gmail.com";

    $mail->Port = 465; 
    $mail->IsHTML(true);
    //Username to use for SMTP authentication
    $mail->Username = "@gmail.com";
    $mail->Password = "";
    //Set who the message is to be sent from
    $mail->setFrom('mzubim@gmail.com', 'Zubair Mushtaq');
    //Set an alternative reply-to address
    $mail->addReplyTo('replyto@gmail.com', 'Secure Developer');
    //Set who the message is to be sent to
    $mail->addAddress('abulogics@gmail.com', 'Abulogicss');
    //Set the subject line
    $mail->Subject = 'PHPMailer SMTP test';
    //Read an HTML message body from an external file, convert referenced images to embedded,
    //convert HTML into a basic plain-text alternative body
    $mail->msgHTML("convert HTML into a basic plain-text alternative body");
    //Replace the plain text body with one created manually
    $mail->AltBody = 'This is a plain-text message body';

    //send the message, check for errors
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    }

-> You Can Copy And Paste this code in you php file it is working file

-> But You need go to "google->account->security->Less secure app access" is by default is on please make sure you turn off to allow sending mail.

-> this is the link https://myaccount.google.com/security it is working in Nov/2019 but currently i don't know.

-> For Documentation of Php Mailer Please visit https://github.com/PHPMailer/PHPMailer

-> You can run this code before that you need to download vendor folder this folder you can download using this command

composer require phpmailer/phpmailer

-> this command only work if you download composer

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    /* Server settings */
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      /* Enable verbose debug output */
    $mail->isSMTP();                                            /* Send using SMTP */
    $mail->Host       = 'ssl://smtp.gmail.com';                    /* Set the SMTP server to send through */
    $mail->SMTPAuth   = true;                                   /* Enable SMTP authentication */
    $mail->Username   = 'youremailaddress@gmail.com';                     /* SMTP username */
    $mail->Password   = 'youremailaddresspassword';                               /* SMTP password */
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         /* Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted */
    $mail->Port       = 465;                                    /* TCP port to connect to */

    /* Recipients */
    $mail->setFrom('youremailaddress@gmail.com', 'Your Name');
    $mail->addAddress('receiveremailaddress@gmail.com', 'Receiver Name');     /* Add a recipient */
    $mail->addReplyTo('youremailaddress@gmail.com', 'Your 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}";
}

?>

-> In Your Hosting You Found SMTP Not Work Properly Then Please Check This WebPage https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!