0
votes

I am trying to send contact form to my gmail when user submit the form, but I have not receiving any email on Gmail when I am using Gmail SMTP server, I am trying both tls with port 587 & ssl with port 465.

But when I used my server SMTP it's working properly and I also received email on my web server. Code is working on PLESK but not working on Cpanel. Code is here:

<?php
require('phpmailer/class.phpmailer.php');

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "tls";
$mail->Port     = 587;  
$mail->Username = "**********@gmail.com";
$mail->Password = "**********";
$mail->Host     = "smtp.gmail.com";
$mail->Mailer   = "smtp";
$mail->SetFrom($_POST["userEmail"], $_POST["userName"]);
$mail->AddReplyTo($_POST["userEmail"], $_POST["userName"]);
$mail->AddAddress("**********@gmail.com");  
$mail->Subject = "Website Contact Us Form Email";
$mail->WordWrap   = 80;
$mail->Body = "User Name: ".$_POST["name"]."<br/><br/>"."User Phone Number: ".$_POST["phone"]."<br/><br/>"."User Email: ".$_POST["email"]."<br/><br/>"."User Message: ".$_POST["message"]."<br/><br/>";

if(is_array($_FILES)) {
$mail->AddAttachment($_FILES['attachmentFile']['tmp_name'],$_FILES['attachmentFile']['name']); 
}

$mail->IsHTML(true);

if(!$mail->Send()) {
    echo "<p class='error'>Problem in Sending Mail.</p>";
} else {
    echo "<p class='success'>Contact Mail Sent.</p>";
}   
?>
1

1 Answers

0
votes

Assuming your authentication credentials are correct, it sounds like this could be a networking issue. Have you confirmed that your firewall(s) have permitted outbound traffic on port 587? The local mail agent may be listening on localhost and sending the mail back to you that way.

Also, since SMTP is used to SEND messages, I believe the from address should actually be the same as the authenticated account (you're sending this email to yourself, not from the user's email account). However, if you're using two different email's this doesn't apply completely.