Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




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: 

<?php
function gmail($to, $subject, $message){
    //path to PHPMailer class
    require_once('./phpmailer/class.phpmailer.php');
    // optional, gets called from within class.phpmailer.php if not already loaded
    include("./phpmailer/class.smtp.php"); 

    $mail = new PHPMailer();
    $mail->CharSet = "UTF-8";
    // telling the class to use SMTP
    $mail->IsSMTP();
    // 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       = "smtp.gmail.com";
    // set the SMTP port for the GMAIL server
    $mail->Port       = 465;
    // GMAIL username
    $mail->Username   = "username@gmail.com";
    // GMAIL password
    $mail->Password   = "mypassword";
    //Set reply-to email this is your own email, not the gmail account 
    //used for sending emails
    $mail->SetFrom('username@gmail.com');
    $mail->FromName = "Firstname Surname";
    // Mail Subject
    $mail->Subject    = "My Subject";

    //Main message
    $mail->MsgHTML($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, "");
    if(!$mail->Send()) 
    {
        //couldn't send
        return false;
    } 
    else 
    {
        //successfully sent
        return true;
    }
}
?>

You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus