Web Bots

Auto Post to Posthaven via Email with Images (PHP)

posthaven_smash

If you were a fan of Posterous before it was bought out by Twitter, you will want to check out Posthaven. It is as of right now still in development but looks very promising, offering a more flexible and simpler blogging solution then with other sites.  This new blogging service isn’t free, but at $5 a month for 10 sub domain blogs it is extremely affordable.  If you want to lock in a wealth of prime real-estate on this upcoming service in the way of subdomain keywords, now is the time.

Like previous articles I have covered on automating blog posting, Posthaven works the same and is really easy. However there is a huge advantage using Posthaven over the other blog services and that is your automated posts can also get put right into Facebook page and Twitter account at the same time. That is cool! And it leverages your blogs big time.

Start by creating a Posthaven account, then login and create one or more site accounts.

To post to your account via email you need to then click on “Edit Your Account” and goto the section “Post by Email Settings”. Click on the checkbox next to ”Use a secret word to verify my emails” and enter a secret password in the field.

You also need to make sure that the email address you will be sending from is listed at the top where it says “Your Email Addresses”

For example lets say your secret is “mysecret” and your site account sub domain is “abc”. Then the email address you will send to post a new blog article would be post.mysecret@abc.posthaven.com

The subject of the email will be the articles title. The body will be the article itself and if you want to include an image then you send it with the email as an attachment. Lets see some code in PHP that will email post to your Posthaven blog. (We will use PHPMAILER found here.)

 

include('class.phpmailer.php');

$posthaven_account = "YOUR SUBDOMAIN NAME"; //Example "abc" NOT "abc.posthaven.com"
$posthaven_secret = "YOUR POSTHAVEN SECRET";

$gmail_your_name = "YOUR NAME";
$gmail_username = "YOUR GMAIL USERNAME";
$gmail_password = "YOUR GMAIL PASSWORD";
$gmail_email = "YOUR GMAIL EMAIL ADDRESS";
$image_location = 'C:/YOUR LOCATION OF IMAGE/IMAGE.JPG';
$email_title = "EMAIL TITLE";
$email_body = "EMAIL BODY"; // (LIMITED) 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;

$posthaven_blog_email = 'post.'.$posthaven_secret.'@'.$posthaven_account.'.posthaven.com';

$To = trim($posthaven_blog_email,"\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 then want to capture the URL of the newly posted posthaven blog you can use the following code:

$posthaven_url = 'http://'.$posthaven_account.'.posthaven.com';
sleep(30); // give it enough time to receive and update the post (30 seconds)
$bf = file_get_contents(rtrim($posthaven_url,'/').'/posts.atom');
list($t,$b1) = explode("<updated>",$bf,2);
list($t,$b2) = explode('href="',$b1,2);
list($b3,$t) = explode('"',$b2,2);
$bb = trim(str_replace('"','',$b3));
$bb = trim(str_replace("'",'',$bb));
$bb = trim(str_replace(' ','',$bb));
echo '<li>LINK IS='.$bb;

WordPress.com Post By Email with Images (PHP)

wordpress_smash

In an earlier blog I showed how one can use a simple PHP script to send blog posts to blogger via email. The same thing can also be done to other blogs such as wordpress.com. When I say WordPress.com I mean the hosted site not the self hosted WordPress offered by wordpress.org (I will address self hosted WP sites in a future blog.)

Here are the steps to turn on Post By Email for WordPress.com blogs.

1. From the main account area: At the top of your wordpress.com login click “My Blogs” then under the blog listing “Blog Admin”, this will put you in the blogs dashboard
2. From the dashboard: On the left side menu, mouse over “Settings” on the sub menu select “Writing”. At the bottom of the Writing Settings page there is a section called “Post by Email” Click on the “My Blogs” link.
3. There should now be a list of all your blogs, in a table, one of the columns is called “Post by Email” and there is a button to push called “Enable” push it and it will generate a secret email that you can use to post blogs via emails to. Write this down we will use it in the code below.

When you send an email to this secret email address it will post the contents of the email to the blog and the subject of the email will become the title of the article. If you include an image as an attachment then the image will also be included in the blog and hosted on wordpress.com’s server.
However there is an issue at this time with such image attachments, the image is posted following the blog article contents, not at the top. In the forums the developers state they are looking into a fixing this issue but I don’t think it is a priority for them.

Another issue regards HTML content. Not all HTML tags and formatting will make as wordpress.com strips some out. Currently I have no list of what is and isn’t passed through but form and javascript tags are stripped.

I should also note that Post By Email for wordpress.com includes some extra custom tagging options. These options include:

• [category x,y,z]
• [excerpt]some excerpt[/excerpt]
• [tags x,y,z]
• [delay +1 hour]
• [comments on | off]
• [status publish | pending | draft | private]
• [password secret-password]
• [slug some-url-name]
• [title Your post title]
• [end] – everything after this shortcode is ignored (i.e. signatures). Make sure it’s on its own line with a blank line above it.
• [slideshow] – replaces the auto-gallery with a slideshow
• [nogallery] – disables the auto-gallery and displays all images inline
• [more] – more tag
• [nextpage] – pagination
• [geotag on | off] – override your geotagging privacy defaults to enable or disable the showing of geo information
• [publicize off|twitter|facebook] – change Publicize options
• [poll]question and answers[/poll] – insert a Polldaddy poll into your post

For details on how these tags operate you can read the documentation page on the wordpress.com site here: http://en.support.wordpress.com/post-by-email/

Now for the PHP code. I recommend using the PHPMAILER class and using a gmail account for the sender, this seems to be the most stable way of performing the email posts.

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";
$wordpress_email = "YOUR WORDPRESS SECRET EMAIL";
$wordpress_url = "YOUR WORDPRESS.COM BLOG URL";
$image_location = 'C:/YOUR LOCATION OF IMAGE/IMAGE.JPG';
$email_title = "EMAIL TITLE";
$email_body = "EMAIL BODY"; // (LIMITED) 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($wordpress_email,"\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 wanting to capture the URL for the newly posted blog after it has been made you can use the following.

sleep(15); // give it enough time to receive and update the post (15 seconds)
$bf = file_get_contents(rtrim($d2s[html_code],'/').'/feed/');
list($t,$b1) = explode("<item>",$bf,2);
list($t,$b2) = explode('<link>',$b1,2);
list($b3,$t) = explode('<',$b2,2);
$bb = trim(str_replace('"','',$b3));
$bb = trim(str_replace("'",'',$bb));
$bb = trim(str_replace(' ','',$bb));
echo '<li>LINK IS='.$bb;

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>