Skip to main content

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

 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>";

}




Comments

Popular posts from this blog

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

JDBC Database Connectivity using JSP and Servlet, Database connectivity on Java

JDBC Database Connectivity using JSP and Servlet, Database connectivity on Java JDBC:-   JDBC means Java database connectivity, it is used to connect from the front-end(application server) to the back-end(database server) in the case of Java web application. The database provides a set of tables to store records and JDBC will work similarly to the  bridge between the database table and application form. 1)  Class.forName("drivername")  // Manage Drive         Class.formName("com.mysql.jdbc.Driver");  // MYSQL      Class.forName ("oracle.jdbc.driver.OracleDriver"); //Oracle 2)  Manage Connection String     It establish connection from application server to database server, Java provide DriverManage class and getConnection that will return Connection object.    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename","username","password"); 3)  Manage Statement to...