• View our reviews on Hot Scripts
  • JS Tutorial

Recent Comments

Powered by Disqus

Back to articles

Send email from Gmail using PHP

Sometimes it can get pretty complex to configure a mail server. It is more complex to make other mail servers believe that your mailserver is legit and not a spam bot. That is why sometimes, you may want to use some external services to do it, and here is a way to send an email from your Gmail account, using PHP.


I'm using PHPMailer class for this purpase, so here is gmail function: 

function gmail($to, $subject, $message){
    //path to PHPMailer class
    // optional, gets called from within class.phpmailer.php if not already loaded

    $mail = new PHPMailer();
    $mail->CharSet = "UTF-8";
    // telling the class to use SMTP
    // enables SMTP debug information (for testing)
    // 1 = errors and messages
    // 2 = messages only
    $mail->SMTPDebug  = 0;
    // enable SMTP authentication
    $mail->SMTPAuth   = true;
    // sets the prefix to the servier
    $mail->SMTPSecure = "ssl";
    // sets GMAIL as the SMTP server
    $mail->Host       = "";
    // set the SMTP port for the GMAIL server
    $mail->Port       = 465;
    // GMAIL username
    $mail->Username   = "";
    // GMAIL password
    $mail->Password   = "mypassword";
    //Set reply-to email this is your own email, not the gmail account 
    //used for sending emails
    $mail->FromName = "Firstname Surname";
    // Mail Subject
    $mail->Subject    = "My Subject";

    //Main message

    //Your email, here you will receive the messages from this form. 
    //This must be different from the one you use to send emails, 
    //so we will just pass email from functions arguments
    $mail->AddAddress($to, "");
        //couldn't send
        return false;
        //successfully sent
        return true;

You may also be interested in:

Powered by

blog comments powered by Disqus