Send Mail using PHP Mailer(SMTP) and Mail function in PHP

send mail in php

Hello All,

I am back with a new new topic, which is very user full for a WEB DEVELOPER , Yes Sending mail with the help of PHP MAILER(SMTP) and Mail Function in PHP is the most use full part while developing a website .This topic helps you to send mails to users while using contact us form or while a user registered in your website a automatic mail is send to that user after registration.

PHP is the most popular language for Web Development .When i used PHP mailer to send mail first time it created too many troubles so i thought that nobody should see this kind of trouble from now on. In this tutorial i will show you how we can send mail using Mail function and PHP Mailer function . if you want to know something more or interesting or want to tell me to create new post on any other topic so kindly follow me on Instagram my user id is – hemu_virus .

Now lets start the tutorial-

SEND MAIL USING MAIL FUNCTION

Step 1 – Copy the given code in your mail file and save it on server .

Step 2 – Change the given from address to your email address . and to address to recipient email address.

Step 3 – Open the page with live URL . check recipient email id a mail is received.

Also see – Basics of Web Designing & Web Development

Source Code –

<?php 
    $to = 'mr.hemu22@gmail.com'; // mail id of user to which you want to send mail
    $subject = "hemuvirus mailing tutorial";  // subject of mail 
    $txt = "Dear User,\nThis is the demo of mail function in php. Thank You!"; // Message you want to send
    $headers = "From: hemuvirus@gmail.com";//Mail id from which you want to send mail
    if(mail($to,$subject,$txt,$headers)){
        echo "Mail Send";
    } else{
        echo "Mail Not Send";
    }
}
?>

Output –

Mail Function

SEND MAIL USING PHP MAILER FUNCTION

Step 1 – For using PHP Mailer – 5.2 stable version we need to first Download – PHP Mailer for downloading Click Here .

Step 2 – Now extract the zip file and save it to server.

Step 3 -Now in the same directory create a file and paste the given code .

Step 4 – Now open the Page with live URL and check the recipient email id a mail is received .

Source Code –

<?php
require "PHPMailer/PHPMailerAutoload.php";
function smtpmailer($to, $from, $from_name, $subject, $body)
    {
        $mail = new PHPMailer();
        $mail->SMTPAuth = true; 
        $mail->SMTPSecure = 'tls'; 
        $mail->Host = 'mail.gmail.com';
        $mail->Port = 587;
        $mail->Username = 'hemuvirus@gmail.com'; // your email id
        $mail->Password = '********';   //your email password
        $mail->SMTPDebug = 3; // error debugger
        $mail->IsHTML(true);
        $mail->From="hemuvirus@gmail.com";
        $mail->FromName=$from_name;
        $mail->Sender=$from;
        $mail->AddReplyTo($from, $from_name);
        $mail->Subject = $subject;
        $mail->Body = $body;
        $mail->AddAddress($to);
        if(!$mail->Send())
        {
            $error ="Email not sent";
            return $error; 
        }
        else 
        {
            $error = "Email sent.";  
            return $error;
        }
    }
    $to   = 'mr.hemu22@gmail.com'; // recipient mail address
    $from = 'hemuvirus@gmail.com'; // your mail address
    $name = 'hemuvirus'; //name of sender
    $subj = 'hemuvirus tutorial'; //subject of mail
    $msg = 'Hey welcome to hemuvirus tutorial'; //message which you want to send
    $error=smtpmailer($to,$from, $name ,$subj, $msg);
?>
<html>
    <head>
        <title>HemuVirus</title>
    </head>
    <body>
        <center><?php echo $error; ?></center>
    </body>
</html>

Output –

And don’t be afraid about the yellow color warning message it is due to the wrong email address so kindly use correct email address if you don’t want these warnings in your mail.

Thank you !!!

Leave a Reply

%d bloggers like this: