How to send email in PHP with domain name and gmail account both

0

 Most common interview question of PHP, How to send email in PHP with the domain name and Gmail account both:-



Send email is a very easy option in PHP, if you send an email from a domain name then google not authenticate and send it to spam.

PHP provides the best option to send emails using Gmail that will always show into customer inbox, not in spam.


I am discussing first to send email using self SMTP with Cpanel.


1)  Create email accounts using the Email option on Cpanel


2)    $to = $email;

         $subject = "This is subject";

         

         $message = "<b>Mewssage.</b>";

         $message .= "<h1>This is headline.</h1>";

         

         $header = "From:from@domain.com \r\n";

         $header .= "Cc:xyz@gmail.com \r\n";

         $header .= "MIME-Version: 1.0\r\n";

         $header .= "Content-type: text/html\r\n";

         $header.="X-Priority: 3";

         $header.="X-Mailer: smail-PHP ".phpversion()."";

        

    

         

         $retval = mail ($to,$subject,$message,$header);

         

         if( $retval == true ) {

            echo "Message sent successfully...";

         }else {

            echo "Message could not be sent...";

         }

  


If this mail delivered into spam then no meaning of sending an email.

Now I am providing a second option to send emails using PHP mailer.


Send email using Gmail with PHPMailer.


1) Download PHP Mailer 

    https://github.com/PHPMailer/PHPMailer


2)  Create a Gmail account and remove 2-level security then an email will be send


3)  put the PHPmailer folder under the project


4) write the following code on the button click.

use PHPMailer\PHPMailer\PHPMailer;

use PHPMailer\PHPMailer\Exception;

require 'PHPMailer-master/src/Exception.php';

require 'PHPMailer-master/src/PHPMailer.php';

require 'PHPMailer-master/src/SMTP.php';



$to ="abc@gmail.com";

$subject = 'Welcome to EroomRent Service';      

$msg = '<html><body>';

$msg.="<h1>Dear $fname, </h1> <br><br> <p>We provide online free services to find Room, Hostel, PG and Flat booking services online without paying any brokerage, our motive is to provide brokerless service and save your money, we do not charge anything from you.just register and provide your preferences we will suggest you room</p>";

 

 $msg.="<p>We have verified each listing and made sure that these are direct owners or shared accommodation parties and there are no middlemen or brokers. all listed proeprty is secure, relibale and contains right information.</p> <br><br> With Regards, <p>Shiva Gautam</p> <p> Founder of EroomRent </p> <p>Call at:- 9630623876</p>";

$msg .= '</body></html>';


$mail = new PHPMailer();

$mail->IsSMTP();

$mail->Mailer = "smtp";

$mail->SMTPDebug  = 0;  

$mail->SMTPAuth   = TRUE;

$mail->SMTPSecure = "tls";

$mail->Port       = 587;

$mail->Host       = "smtp.gmail.com";

$mail->Username   = "blablabla@gmail.com";

$mail->Password   = "xyz123";

$mail->IsHTML(true);

$mail->AddAddress($to, $subject);

$mail->SetFrom("xyz@gmail.com", "EroomRent");

$mail->AddReplyTo("xyz@gmail.com", "EroomRent");

$mail->AddCC("abc@gmail.com", "ERoomRent");

$mail->Subject = $subject;

$content = $msg;


$mail->MsgHTML($content); 

if(!$mail->Send()) {

    echo "<script>window.location='reg.php?q=registration not successfully';</script>";

  //var_dump($mail);

} else {

      echo "<script>alert('Emailid already exists');window.location='reg.php';</script>";

}




Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)