0

I have written the following code to send email from my website hosted in Namecheap.

<?php
include("constants.php");

require_once('libs/phpmailer/class.phpmailer.php');
require_once("libs/phpmailer/class.smtp.php");
require ("libs/phpmailer/PHPMailerAutoload.php");

try {
    $mailer = new PHPMailer();
    $mailer->IsSMTP();
    $mailer->SMTPSecure = 'tls';
    $mailer->Host = 'smtp.zoho.com';
    $mailer->SMPTDebug = 2;
    $mailer->Port = 587;
    $mailer->Username = EMAIL;
    $mailer->Password = PASSWORD;
    $mailer->SMTPAuth = true;
    $mailer->From = EMAIL;
    $mailer->FromName = "User";
    $mailer->Subject = 'New Query Received';
    $mailer->isHTML(true);
    $mailer->Body = '<p>Hello,</p><p> The following query was received from '.$_POST['name']. '.<br>"'. $_POST['message']. '"</p><p>'.'His email is '.$_POST['email'].'</p><p>Hope you will have a great day.</p><p>Best Regards,</p>';
    $mailer->AddReplyTo(EMAIL_REC, 'Contact');

    $mailer->AddAddress(ADDRESS, NAME);

    $mailer->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    ));


    if ($mailer->Send()) {
        echo "Sent";
    } 
    else {
        echo $mailer->ErrorInfo;     
    }
}
catch (phpmailerException $e) {
    echo $e->errorMessage(); 
} catch (Exception $e) {
    echo $e->getMessage(); 
}

header('Location:index.html');
exit;

?>

I am using zoho mail for my email host but I have tried the same using a gmail account too. I have already cross verified the EMAIL and its password.

The code works perfectly fine in my localhost but causes "SMTP connect() failed" in the Namecheap server.

If I comment $mailer->IsSMTP(); then no error is shone but email is not sent.

shreyansh
  • 108
  • 2
  • 11
  • Read the docs from both PHPMailer and NameCheap. Your ISP is blocking outbound SMTP; they probably provide a way around it (e.g. by providing a mail relay), but you'll need info from them. – Synchro Aug 13 '18 at 09:27
  • Possible duplicate of [Why php is sending email from localhost not from server using PHPMailer?](https://stackoverflow.com/questions/36786048/why-php-is-sending-email-from-localhost-not-from-server-using-phpmailer) – Synchro Aug 13 '18 at 09:28

0 Answers0