How to send mail from php from local host using gmail smtp | Wave the world

How to send mail from php from local host using gmail smtp

Hi Friends,

How are you all? Good. I'm also good. Today I'll tell you how to send mail from local host in php.

I'm using php mailer to send mail. PHPMailer is a class library for PHP that provides a collection of functions to build and send email messages. PHPMailer supports several ways of sending email: mail(), Sendmail, qmail & direct to SMTP servers. You can use any feature of SMTP-based e-mail, multiple recepients via to, CC, BCC, etc. In short: PHPMailer is an efficient way to send e-mail within PHP.

PHP has a built-in mail() function. So why use PHPMailer? Isn't it slower? Not really, because before you can send a message you have to construct one correctly, and this is extremely complicated because there are so many technical considerations. PHPMailer makes it easy to send e-mail, makes it possible to attach files, send HTML e-mail, etc. With PHPMailer you can even use your own SMTP server and avoid Sendmail routines used by the mail() function on Unix platforms.
You can download phpmailer from here. 
After downloading the file, you need to extract the content and save it under your server folder. For me I'm saving in htdocs because i'm using XAMPP. The best practice is save it inside your website folder. My website folder name is test. So I saved it inside test folder.
To send mail you need a html file in which you can provide the details, Google id and password and php file in which all the mail logic written.
Lets create a simple html form,
<html>
    <body>
        <form name="email" method="post" action="test.php">
            <table>
                <tr>
                    <td>To</td>
                    <td><input type="text" name ="to" required></td>
                </tr>
                <tr>
                    <td>From</td>
                    <td><input type="text" name="from" required></td>
                </tr>
                <tr>
                    <td>CC</td>
                    <td><input type="text" name="cc"></td>
                </tr>
                <tr>
                    <td>Subject</td>
                    <td><input type="text" name ="subject"></td>
                </tr>
                <tr>
                    <td>Message</td>
                    <td><textarea rows="5" cols="22" name="message" required>
                    </textarea></td>
                </tr>
                <tr>
                    <td><input type="submit"></td>
                    <td><input type="reset"></td>
                </tr>
        </form>
    </body>
</html>
I'm not adding any validation in the form. Just one that is you have to enter something. You can also email validation if the email id is valid or not.
You can save the file with any name. I'm saving it test1.html. It will look like,

I did not added any css. If you want you can add and modify code according to your need. Now we created html file. Lets write some php code.

<?php
    require 'PHPMailer/PHPMailerAutoload.php';

    if (!empty($_POST["to"]) && !empty($_POST["from"]) 
        && !empty($_POST["message"])){
        $to = $_POST["to"];
        $from = $_POST["from"];
        $message = $_POST["message"];
        $subject = $_POST["subject"];
        //Create a new PHPMailer instance
        $mail = new PHPMailer;   
        $mail->isSMTP();
        $mail->SMTPDebug = 0;
        $mail->Debugoutput = 'html';
        $mail->Host = 'smtp.gmail.com';
        $mail->Port = 587;
        $mail->SMTPSecure = 'tls';

        //use SMTP authentication
        $mail->SMTPAuth = true;

        //Username to use for SMTP authentication
        $mail->Username = "yourEmail@gmail.com";
        $mail->Password = "yourEmailIdPass";
        $mail->setFrom($from);
        $mail->addReplyTo($to);
        $mail->addAddress($to);
        $mail->Subject = $subject;
        // $message is gotten from the form
        $mail->msgHTML($message);
        $mail->AltBody = "";
        if (!$mail->send()) {
            echo "Error while sending mail";
        } else {
            echo "Congratulations. Success";
            }
    }
    else
        header('Location: http://localhost/test/test1.html')
?> 

when you write your php mail code, check require 'PHPMailer/PHPMailerAutoload.php' . If you saved PHPMailer in another folder then you have to give proper path.

After checking path, see these two lines,
        $mail->Username = "yourEmail@gmail.com";
        $mail->Password = "yourEmailIdPass";
Replace yourEmail@gmail.com with your email id and yourEmailIdPass with your password for that email id. Two-Step verification should be disable on the account that you are using in php file. If two-step verification is enabled then you have to disable it from your gmail settings.

Now save your file with the name of test.php.

You almost completed. But you have make some setting in your Gmail. For less secure apps you need to change account access. Go to following link.


It is turn off by default. You have to turn it on.


So everything is setup. It's time to test our mail if it is working or not.

Enter the email id in To and From, Enter a subject and a message and then click on Submit button.

If everything works as expected then you will get the success message,

Now go to your mailbox to check if you really get the email or is it just showing success message Congratulations. Success .

Great, I got the mail.

Enjoy :)






2 comments:

  1. Thank you for your article on Gmail. Gmail is by far my favourite email service.
    It was nice to learn something new.

    ReplyDelete

 

Pro

About