Send Mail using PHP Mailer(SMTP) and Mail function 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-


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 –

    $to = ''; // 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:";//Mail id from which you want to send mail
        echo "Mail Send";
    } else{
        echo "Mail Not Send";

Output –

Mail 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 –

require "PHPMailer/PHPMailerAutoload.php";
function smtpmailer($to, $from, $from_name, $subject, $body)
        $mail = new PHPMailer();
        $mail->SMTPAuth = true; 
        $mail->SMTPSecure = 'tls'; 
        $mail->Host = '';
        $mail->Port = 587;
        $mail->Username = ''; // your email id
        $mail->Password = '********';   //your email password
        $mail->SMTPDebug = 3; // error debugger
        $mail->AddReplyTo($from, $from_name);
        $mail->Subject = $subject;
        $mail->Body = $body;
            $error ="Email not sent";
            return $error; 
            $error = "Email sent.";  
            return $error;
    $to   = ''; // recipient mail address
    $from = ''; // 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);
        <center><?php echo $error; ?></center>

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 !!!