PEAR::Mail and GMail
If you ever need to send email from PHP and don’t have access to a SMTP server, but do have a GMail account use this code as a template.
You will need:
- PHP Pear::Mail class installed
- A GMail account
- Port 465 open to smtp.gmail.com in your firewall
<?php include('Mail.php'); $from = "firstname.lastname@example.org"; $to = "email@example.com"; $subject = "Your subject here"; $body = "\n\nEmail contents here"; $host = "ssl://smtp.gmail.com"; $port = "465"; $username = "firstname.lastname@example.org"; $password = "yourGmailPassword"; $headers = array ( 'From' => $from, 'To' => $to, 'Subject' => $subject); $smtp = Mail::factory('smtp', array ( 'host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password)); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) die($mail->getMessage()); ?>
A couple of important things to note with this code.
First off, its a template, at least have a basic understanding of what it does before you try to use it or ask a question.
Second, yes, its rough and the error handling is poor at best, see the first point.
Third, and if you’ve gotten this far, you probably knew this already, the $smtp->send() function will wait for the socket to close before continuing, so this would probably be best for queued or deferred processing of email.