Skip to content

Send email using Gmail and PHPMailer

The new automatic update generator is ready, it has been a long time since OCRALight has been finished and little bit of this and that has been polished on the update generation.

The process is fairly complex, it involves reverse-engineering, data-mining, packaging, distribution and a lot o fighting with our crappy Windows server that is between me and the final Linux liberation.

Every step in the road has been automatized, one by one, every problem has been solved and polished, now the final piece is in his place, the automatic email generation. Now the updates will be made and send everyday, even weekends and vacations.

If you are interested in the technical aspect keep reading:

How it has been done:

First of all, you need to have PHP with OpenSSL support, for Windows you’ll need to Install PHP and carefully select OpenSSL in the components list, if you already have PHP installed, don’t worry a re-install will keep your configuration, and you’ll be able to select OpenSSL.

Then download PHPMailer,  and extract it near your main php file.

You will need to have a Gmail account(obviously) I recommend you to make a new one just for this, mainly because the configuration need to be very precise, and you wouldn’t be able to use it freely without loosing functionality or risking to break the configuration.

Configure your Gmail account to use POP mail, but not IMAP, ONLY POP, just POP.

And now the code:

<?php
require(”PHPMailer/class.phpmailer.php”);

$update_emails = array(
    ‘Juan Perez’ => ‘Juan_Perez@jalisco.gob.mx’,
    ‘Francisco Garcia’ => ‘fgarcia@hotmail.com’,
    ‘Diana la del Tunel’ => ‘diana@gmail.com’
  );

echo “\nSending Update Email\n”;

$mail = new PHPMailer()// Instantiate your new class
$mail->IsSMTP();          // set mailer to use SMTP
$mail->SMTPAuth = true;   // turn on SMTP authentication
$mail->Host = “smtp.gmail.com”; // specify main and backup server
$mail->SMTPSecure= ’ssl’; //  Used instead of TLS when only POP mail is selected
$mail->Port = 465;        //  Used instead of 587 when only POP mail is selected

$mail->Username = “youremail@gmail.com”;  // SMTP username, you could use your google apps address too.
$mail->Password = “yaourextremelynotlamepassword”; // SMTP password

$mail->From = “youremail@gmail.com”; //Aparently must be the same as the UserName
$mail->FromName = “Your name”;
$mail->Subject = ‘The subject’;
$mail->Body = “The body of your message”;

foreach ($update_emails as $name => $email) {
  $mail->AddBcc($email, $name);
}

if(!$mail->Send())
{
  echo “There was an error sending the message:” . $mail->ErrorInfo;
  exit;
}
echo “Done…\n”;
?>
 


In this code I send the email to a group of people, thus I use the “Bcc:” field instead of the “To:” one, to add a “To:” you would use AddAddress($email, $name).

A possible upgrade would be to use a MySQL database to store the addresses,&amp;nbsp; and provide a web interface to add and remove them. for the moment, this is enough.

Soo remember:

  1. PHP with OpenSSL
  2. PHPMailer
  3. Create a Gmail Account
  4. Activate POP
  5. Host: smtp.gmail.com
  6. SMTPAuth=true
  7. SMTPSEcure=ssl
  8. Port: 465
  9. User with Domain
  10. Password
  11. $Mail-&amp;gt;send()

7 Comments

  1. roltio wrote:

    i need a php mailer

    Sunday, April 20, 2008 at 12:37 pm | Permalink
  2. Eduardo wrote:

    Hi!

    Do you know the limit for outgoing email through Gmail account?

    Tks

    Tuesday, May 13, 2008 at 8:35 pm | Permalink
  3. Argel Arias wrote:

    Not really, but this gmail page should answer a lot of your questions.

    http://www.google.com/mail/help/bulk_mail.html

    Tuesday, May 13, 2008 at 10:17 pm | Permalink
  4. Anjo wrote:

    visit this blog http://moneyandjobs.blogspot.com/2008/06/phpmailer-settings-to-send-email-using.html i had also create a 3 simple steps sending email using your gmail account via phpmailer

    Friday, June 27, 2008 at 6:49 pm | Permalink
  5. aleale123 wrote:

    Please, advise me how to open ssl? I open ssl module but error: unable to connect to ssl://smtp.gmail.com:465
    Thanks!

    Wednesday, July 9, 2008 at 1:55 pm | Permalink
  6. jersey wrote:

    @aleale123

    i have provided solution on your problem click my
    name and you can find it there..

    Thursday, July 24, 2008 at 3:38 pm | Permalink
  7. Mekaiel Hussain wrote:

    Works like a top I used SendMail… Pegasus all that junk….. should have come across this much earlier…… Simple to use and implement. Thanks a bunch……

    Friday, April 23, 2010 at 2:08 pm | Permalink

One Trackback/Pingback

  1. PHP – How to Create a Contact Form using Phpmailer | MesraOnline on Saturday, November 19, 2011 at 4:37 pm

    […] http://blog.levhita.net/2008/03/13/send-email-using-gmail-and-phpmailer/ […]

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*