Skip to content

How to Send Email with SMTP and PHP Mailer ?

In this post i am gonna write about Sending Emails Through the SMTP(Send Mail Transfer Protocol) and PHP Mailer Library. You can download PHP Mailer at https://github.com/Synchro/PHPMailer

Here is the simple Script for send emails through the SMTP:

require_once('PHPMailer/class.phpmailer.php');


    $msg  = 'Hello World';
    $subj = 'Test mail message';
    $to   = '[email protected]';
    $from = '[email protected]';
    $name = 'Name';

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


    function SendMailViaSmtp($to, $from, $name , $subject, $body)
    {
        global $error;
        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->SMTPAuth = true;
        $mail->Host = 'smtp.mail.google.com';
        $mail->Username = '[email protected]';  
        $mail->Port = 26;
        $mail->Password = '******';
        $mail->IsHTML(true);
        $mail->SetFrom($from, $name);
        $mail->Sender=$from;
        $mail->AddReplyTo($from, $from_name);
        $mail->Subject = $subject;
        $mail->Body = $body;
        $mail->AddAddress($to);
        if($mail->Send()) { // If the Send method returns true, then everything worked. If it returns false, then there was a problem.
            $error = 'Message sent!';
            return false;
        } else  {
            $error = 'Mail error: '.$mail->ErrorInfo;
            return true;
        }
    }
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments