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;

Leave a Reply

*

code