Home » Php » PHPMailer is not sending mails

PHPMailer is not sending mails

Posted by: admin October 20, 2018 Leave a comment


Guys I have read many answers on this question but the problem still persists.
The PHPMailer is not sending emails giving the following error.

Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Following is the code:


require 'PHPMailer-master/PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
//$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = "mail.domain.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port =25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
//Username to use for SMTP authentication
$mail->Username = "mail@mail.com";
//Password to use for SMTP authentication
$mail->Password = "password";
//Set who the message is to be sent from
$mail->setFrom("mail@mail.com","Contact Form");
//Set an alternative reply-to address
$mail->addReplyTo($_POST['user_mail'], $_POST['user_name']);
//Set who the message is to be sent to
$mail->addAddress("mail@mail.com", "Contact Form");
//Set the subject line
$mail->Subject = $_POST['subject'] . " - Contact Form";
//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("Message from ". $_POST['user_name']." : <br/>".$_POST['message'] . "<br/> Contact Details : <br/> Email :" . $_POST['user_mail'] . "<br/> Contact Number : " . $_POST['user_number']);
//Replace the plain text body with one created manually
//$mail->AltBody = 'This is a plain-text message body';
//Attach an image file

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "<script>window.location.href='thankyou.html';</script>";