Examples » Sending Emails using GMail
In this example, I will show you how send emails using GMail. It is most suitable if you're looking for a free newsletter system.
If you've been in PHP programming for a while, you may have heard phpMailer. Its a PHP class for sending emails using SMTP protocol. And it works for GMail too. While we can use phpMailer for sending emails with GMail, we'll use HTTP scripting for some reasons:
- Not all mail providers open their SMTP server to public.
- You can do something else, like checking your Inbox or saving to Draft.
- It gives more control.
- It's easier.
- It's a lot of fun.
Anyway, let's get started. First open gmail.
include 'phpWebHacks.php';
$h = new phpWebHacks;
/* gmail.com */
$h->get('http://gmail.com');
It will show you the login box. Extract the hidden values, fill in your username & password, and login.
obtain the form's name from the HTML source
note that we don't pass the HTML string since phpWebHacks 'remember'
the last HTML page */
$form = $h->parseForm('gaia_loginform', &$action);
/* fill in your username & password
obtain the fields' name from the HTML source */
$form['Email'] = 'myusername';
$form['Passwd'] = 'mypassword';
/* login */
$h->post($action, $form);
To start writing an email, go to the compose page. View the HTML source to get the URL of the "Compose" link, which is ?v=b&pv=tl&cs=b.
Note that you can use relative URLs. It will be converted to
absolute URL using the last URL as the base URL */
$h->get('?v=b&pv=tl&cs=b');
Just like before, we need to extract the compose form's hidden fields. After that type your message, select an attachment, and click the 'Send' button.
obtain the form name from the HTML source */
$form = $h->parseForm('f', &$action);
/* compose your message */
$form['to'] = 'dede_blackheart@hotmail.com';
$form['subject'] = 'phpWebHacks rocks!';
$form['body'] = 'Yeah it rocks!';
$form['nvp_bu_send'] = 'Send';
/* browse the attachment */
$file = array('file0' => 'myphoto.jpg');
/* send */
$h->post($action, $form, $file);
Don't forget to logout when you're done.
$h->get('?logout&hl=en');
Using this *very* simple script, you can have a sophisticated newsletter system. Just remember to set the delay between requests, or your IP address will be blocked. And one more thing, we all hate spams just like you.
michael grobe on Oct 21, 2009:
$fp = @fsockopen(($scheme=='https' ? "ssl://$host" : $host), $scheme == 'https' ? 443 : $port);