Mail2Blogger with Images (PHP)

blogger_smash

Blogger allows you to send posts via email, intended for on the go posting via mobile devices and such. To activate this feature you have to update your blog settings with a secret key.

1. Log into Blogger
2. Select “Settings for your Blog”
3. Select “Mobile and email”
4. Where it says “Posting using email” there is a box, in front of the box is your username, following the box is @blogger.com. In the box put your secret key.
5. On the radio selection below that make sure “Publish email immediately” is selected.
6. Click Save Settings

Now all you have to do to send a post to your blog is send it to that email address that contains your secret key. The title of your email will be the title. Keep in mind that if your email automatically adds a signature to your email that it will probably also appear on your post.

You can include an image that appears at the top of the article by attaching an image in your email. Blogger saves this to its server and hosts it (10MB limit for images)
Using PHP we can now easily post new blog articles at any time or even automatically using the PHPMAILER class from Synchro at Github. It is best that you use a gmail account email that is associated with your blog to reduce any imperial entanglements. Also by using gmail account and this method you are not required to have a running mail server or do any other funky relaying (great for those doing projects in XAMPP.)

include('class.phpmailer.php');

$gmail_your_name = "YOUR NAME";
$gmail_username = "YOUR GMAIL USERNAME";
$gmail_password = "YOUR GMAIL PASSWORD";
$gmail_email = "YOUR GMAIL EMAIL ADDRESS";
$blogger_secret_key = "YOUR BLOGGER SECRET KEY";
$blogger_url = "YOUR BLOGGER URL";
$image_location = 'C:/YOUR LOCATION OF IMAGE/IMAGE.JPG';
$email_title = "EMAIL TITLE";
$email_body = "EMAIL BODY"; // HTML OK

$mail = new PHPMailer();
$mail->IsHTML(true);
$mail->IsSMTP();
$mail->;SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = $gmail_username;
$mail->Password = $gmail_password;
$fromname = $gmail_your_name;

$To = trim($gmail_username.'.'.$blogger_secret_key.'@blogger.com',"\r\n");

$mail->AddAttachment($image_location);
$mail->From = $gmail_email;
$mail->FromName = $fromname;
$mail->Subject = $email_title;
$mail->Body = $email_body;
$mail->AddAddress($To);
$mail->set('X-Priority', '3'); //Priority 1 = High, 3 = Normal, 5 = low
$mail->Send();

If you are keeping a database of your posts for other projects such as twittering and other linking you can extract the new posts link with the following code. Just add it after the end of the above code.

sleep(15); // blogger enough time to receive and update the post (15 seconds)
$bf = file_get_contents($blogger_url);
list($t,$b1) = explode("'post-title entry-title'",$bf,2);
list($t,$b2) = explode('<a href=",$b1,2); list($b3,$t) = explode(">',$b2,2);
$bb = trim(str_replace('"','',$b3));
$bb = trim(str_replace("'",'',$bb));
$blog_post_url = trim(str_replace(' ','',$bb));<a href="http://charleshays.com/wp-content/uploads/2013/07/blogger_smash.png"><img src="http://charleshays.com/wp-content/uploads/2013/07/blogger_smash-300x300.png" alt="blogger_smash" width="300" height="300" class="alignnone size-medium wp-image-102" /></a>
echo 'NEW POST AT='.$blog_post_url;
</a>

Leave a Reply

*

code