December 20, 2023 5:02 am

Send Email with SMTP and PHP Mailer

In this tutorial we have a simple script which send email through SMTP (Send Mail Transfer protocol) with PHP Mailer. We need phpmailer library and bellow given code.

[wpdm_file id=6]

PHP Code

Simple PHP file

    require_once('PHPMailer_v5.1/class.phpmailer.php'); //library added in download source.
    $msg  = 'Hello World';
    $subj = 'test mail message';
    $to   = '[email protected]';
    $from = '[email protected]';
    $name = 'My Name';

    echo smtpmailer($to,$from, $name ,$subj, $msg);

    function smtpmailer($to, $from, $from_name = '', $subject, $body, $is_gmail = true)
        global $error;
        $mail = new PHPMailer();
        $mail->SMTPAuth = true; 
            $mail->SMTPSecure = 'ssl'; 
            $mail->Host = '';
            $mail->Port = 465;  
            $mail->Username = '[email protected]';  
            $mail->Password = '*******';   
            $mail->Host = '';
            $mail->Username = '[email protected]';  
            $mail->Password = '******';
        $mail->From="[email protected]";
        $mail->Sender=$from; // indicates ReturnPath header
        $mail->AddReplyTo($from, $from_name); // indicates ReplyTo headers
        $mail->AddCC('[email protected]', 'CC: to');
        $mail->Subject = $subject;
        $mail->Body = $body;
            $error = 'Mail error: '.$mail->ErrorInfo;
            return true;
            $error = 'Message sent!';
            return false;

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.

Tutorial Categories:

29 responses to “Send Email with SMTP and PHP Mailer”

  1. daman sony says:

    This is not working!!
    Getting This error comes
    Fatal error: Class ‘SMTP’ not found in C:wampwwwFBSTOREphpmailerPHPclass.phpmailer.php on line 1147

  2. kuldeep sonawane says:

    hi, nice tutorials. if it is possible write a tutorial on send MSG to phone. Thanks by the way good job!

  3. Ghulam Ali says:

    amazing sir jee. i tried all others script but your script is working absoulutly amazing withour getting any error. thanx

  4. Alok says:

    anyone can provide editing file of index.php file……because my code i notn working………my email id is [email protected]…(without username and password)

  5. Hello sir I wan to know tht it may possibile to receive emails from smtp which email like [email protected] that is created from domain ?

  6. Gautam says:

    i have download the code but it gives following error :

    SMTP Error: Could not authenticate.1

    how could i solve it

    is there any changes needed is “php.ini” file

  7. dipjoy datta says:

    I am using same code but getting error like : SMTP Error: Could not connect to SMTP host. 1

  8. freesonglyrics says:

    i am getting the this error
    SMTP Error: Could not authenticate.
    i am using windows server where windows 2012 is installed. What will be the solution to get rid of this problem.


  9. রাজিউল ইসলাম says:

    Its not working ,Please update a new one

  10. nischaytv says:

    Warning: require_once(PHPMailer_v5.1/class.phpmailer.php): failed to open stream: No such file or directory in /home/appr7643/public_html/app/ma/index.php on line 2

  11. nischaytv says:

    Fatal error: Class ‘SMTP’ not found in /home/aaaa/public_html/app/ma/PHPMailer-master/class.phpmailer.php on line 1466

Leave a Reply

Your email address will not be published. Required fields are marked *