Gmail to Domain email Sending Emails in PHP- SMTP, PHPmailer
Many developers are facing issues when they are sending emails from Gmail to any other webmail or domain mail. Here you will get very easy method fix this issues by using PHPmailer and SMTP settings. You have to follow below 3 step,
Step-1: Download PHPmailer Class file. Click Here
Step-2: Make your gmail as less secure app click here
Step -3 : Use Below Code for sending emails.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php require 'class/class.phpmailer.php'; $mail = new PHPMailer; $mail->IsSMTP(); //Sets Mailer to send message using SMTP //$mail->SMTPDebug = 3; $mail->Host = 'smtp.gmail.com'; $mail->Port = '465'; //Sets the default SMTP server port $mail->SMTPAuth = true; //Sets SMTP authentication. Utilizes the Username and Password variables $mail->Username = 'gmail'; //Sets SMTP username $mail->Password = "password"; //Sets SMTP password $mail->SMTPSecure = 'ssl'; //Sets connection prefix. Options are "", "ssl" or "tls" $mail->From = 'freeonlineprojectz@gmail.com'; //Sets the From email address for the message $mail->FromName = "Free Online"; //Sets the From name of the message $mail->AddAddress('info@freeonlineprojectz.com', 'Free Online'); //Adds a "To" address $mail->AddCC("freeonlineprojectz@gmail.com", "Free Online"); //Adds a "Cc" address $mail->WordWrap = 50; //Sets word wrapping on the body of the message to a given number of characters $mail->IsHTML(true); //Sets message type to HTML $mail->Subject = "Text Subject"; //Sets the Subject of the message $mail->Body = "Text Message"; if($mail->Send()) //Send an Email. Return true on success or false on error { $error = '<label class="text-success">Thank you for contacting us</label>'; } else { $error = '<label class="text-danger">There is an Error</label>'; // echo $mail->ErrorInfo; } echo $error; ?> |
If you are facing any issues with this code, please put the issues in comments sections. I will see that issue.