WordPress: How to use wp-mail.php correctly in Gmail blog posts? This is my solution

Update: this blog post doesn’t apply to WordPress versions newer than 2.7.

Let’s say you wish to use the WordPress function for blogging via email posts. This work great for HTML emails in Yahoo Mail. It works great for Google’s Gmail in plain-text format. How about Gmail in HTML emails? Things get trickier. That’s why I present you my very first PHP small add-on.What ca you read in this blog post?
1. What does my software do?
2. What does my software doesn’t do?
3. How can you personalize the blog to work only for your email address?
4. What should you note?
5. Ok, ok, where’s the software? Download link?
6. How do you know it works?
Ok, now you know it. Let’s get going.

1. What does my software do?
My PHP add-on does one single trick: helps you put HTML emails. It works great with Gmail (tested it a lot on some of my blogs, see the end of this blog post), and it works not-so-bad with Yahoo! Mail (only done a few tests, though).
It also try to fix a bug of WordPress for breaking posts that contain Unicode characters.

2. What does my software doesn’t do?
Unicode characters are a big pain for WordPress. There are some characters which are poorly converted. This problem is also common for sending emails. What’s the solution? I’ll present my case story: whenever I send an email that has Unicode characters, I first try to convert those Unicode characters into ASCII. This can be solved quite easily in Notepad (under Windows Operating System). Just create a blank document in Notepad, put the Unicode text in there. Save it. Notepad usually warns you that if you want don’t save as Unicode, you’ll lose some formatting. Hit Ok, then reopen the document. Most Unicode texts are gone. There are still some problems (quotes, for example, are not converted) and for those … I’ve created a different software. See the blog post in here.

3. How can you personalize the blog to work only for your email address?
You can replace this line:
if (( is_email($author) ) && ($author == ‘PUTYOUREMAILADDRESSINHERE@SOMEMAIL.COM’)) {

with this line:
if ( is_email($author) ) {

Also, replace these two lines:
// Author not found in DB, set status to pending.  Author already set to admin.
$post_status = ‘publish';

with these two lines:
// Author not found in DB, set status to pending.  Author already set to admin.
$post_status = ‘pending';

Finally, replace these three lines:
if (!$userdata) {
$post_author = 1;
$author_found = false;

with these three lines:
if (!$userdata) {
$post_author = 1;
$author_found = true;

4. What should you note?
I am not responsible for errors you might encounter with your software. You can leave a comment below and explain your errors, and I’ll see what I can do, you can post your errors on WordPress’s support page or search its archives. Please note that this is my very first software in PHP and I’ve learned the commands mostly by trial and error.

5. Ok, ok, where’s the software? Download link?
You can download my software in here:
wp-mail.php made by Olivian BREDA. Use at your own risk

Use it at your own risk.

6. How do you know it works?
Well, I’ve tested it for a few months now. These are the blogs where I’ve tested my software:

Update: this blog post doesn’t apply to WordPress versions newer than 2.7.

Comentarii

  1. AL Geddis a scris

    I just uploaded your wp-mail.php file to my site. I then went to http://www.mysite.com/blog/wp-mail.php and got this message: POP3 connect: Error [97] [Address family not supported by protocol]

    I also do not understand how this would work-I want to be able to send a post via my gmail account, but to WHERE would I send it?

  2. a scris

    Hello AL Geddis,

    Thanks for the comment.

    1. Could you please try the original wp-mail.php, the one provided by WordPress and tell me the error you get? I can then find out if the error is in my software or in your configuration;

    2. You must configure an email address on your server on which to send emails to;
    so if your site is greatsite.com, you’ll have to have an address like myemail@greatsite.com. You can configure this address in the Settings area (or Options for older versions of WordPress); Then go to Writing Tab and voila. At the end of the page you’ll find these settings;

    You can read all about it here:
    http://codex.wordpress.org/Blog_by_Email

    If you wish, you can let us know how’s it going.blushing

    Olivian BREDA’s last blog post..Botnets – first understand them and then fight them. A practical guide

  3. a scris

    I am still getting this error!!!

    Hey! Welcome to my optimized-for-Yahoo-and-Gmail interface.

    I’ve read the following encoding before actually reading the email (to evoid gmail errors):7bit

    I’ve read the following encoding before actually reading the email (to evoid gmail errors):7bit

    Gmail address found. Setting specific settings.

    Yahoo address NOT found. Going to normal email settings.
    Author of the blog post = ghajinii2008@gmail.com

    Author: 1

    Posted title: Test

    Mission complete, message 1 deleted.

    You can always check out my web site for more IT tricks: http://olivian.ro/:(:(

    Souvik Banerjee’s last blog post..Movie Poster

  4. a scris

    THANK YOU! I work for the government and so I can’t really blog anythiung political from work, but with email, I feel OK doing it. Now that I can easily get HTML in there, I’m excited!!

    SOmething else that would be nice: being able to assign categories through email. Right now, there’s just the default email category, but if I could select more…

    just a thought if you are feeling like writing up other enhancements winking

    thanks again!!

    moleboy’s last blog post..Huckabee: Prop 8 doesn’t ban gay marriage

  5. a scris

    This is working for me, however in step 3:
    if (( is_email($author) ) && ($author == ‘PUTYOUREMAILADDRESSINHERE@SOMEMAIL.COM’)) {

    with this line:
    if ( is_email($author) ) {

    I get an error…
    Parse error: syntax error, unexpected ‘@’ in /homepages/4_/d_________/htdocs/samburu/blog/wp-mail.php on line 112

    any suggestions? I changed it back to default, and by golly, it is working! I would like to have only one sender email address set though.
    Thanks!

  6. a scris

    ok, I may have had it backwards… Currently, line 112 has:

    if ( is_email($author) ) {

    I was thinking I had to change it to:
    if (( is_email($author) ) && ($author == ‘PUTYOUREMAILADDRESSINHERE@SOMEMAIL.COM’)) {
    adding my sending email address.

    Are the posts supposed to post automatically, or do I have to install WP-Cron? I noticed they show up after running wp-mail.php from my url window…

    Thanks! David

  7. a scris

    all right, I did install WP-Cron, that is working now,
    I did change the line to:

    if (( is_email($author) ) && ($author == ‘myaccountname@gmail.com’)) {

    and get this error still:
    Parse error: syntax error, unexpected ‘@’ in /homepages/40/d_________/htdocs/samburu/blog/wp-mail.php on line 112

    very odd!

  8. a scris

    I tried just tried removing the “@gmail.com”
    leaving only my username on line 112
    and it seems to be working now.

    Well… I just did 2 tests, and they are listed in posts as pending. I will check my cron settings. What alternative to wp-cron can you suggest?

    Thanks again!

  9. a scris

    I’m glad it worked for you.

    Regarding Cron: This is what I personally use:
    http://activeblogging.com/info/wordpress-cron-plugin/

    Basically, whenever a user visits my blog, it runs the function. If 15 minutes (or whatever) passed since I last run the code, then the code is executed (run wp-mail.php). I use more than one blog and all of them have this function.

    It’s less bother to me than do a WP-Cron server side. happy

    Olivian BREDA’s last blog post..Why vote? The one reason for which anyone should vote. Read this message, and then go voting!

  10. a scris

    Hey Olivian, I tried the plugin you mentioned above, it hasn’t started working yet. what do you set you “time” (00:00) for?

    Looks like everything else is working great. Thanks again, David

  11. a scris

    my settings are like this:
    1. an hour to start like 17:30 (you can also put 00:10)
    2. 15m
    3. http://olivian.ro/conferinte/wp-mail.php

    You should also check out this thing:
    Your theme MUST have the function wp_footer included in it, like this:
    < ?php wp_footer(); ?>

    (most newer themes already have this in footer.php or possibly index.php).

    So: put it to 10 minutes.

    After 12 minutes pass, visit your blog.

    After you visit your blog, visit it once again.

    Olivian BREDA’s last blog post..Why vote? The one reason for which anyone should vote. Read this message, and then go voting!

  12. a scris

    Yeah, I think that’s been something a lot of people have tried and not been able to do.
    Oh well.
    I was actually trying to fake my way through to get it to handle posts emailed from Google Reader better. But, not knowing anything about php made that rather difficult. I might take another whack at it at some point…just to get it to maybe strip out the graphics or, at the very least, the signature that this came from google reader, etc.

    moleboy’s last blog post..Daily Kitteh!

  13. a scris

    Hi, does this hacked version of wp-mail.php support categories? Thats really all I want and dont want to fly the route of ‘postie’, just looking for a revised version that allows the old [1] or [category] in the subject title. This is killing me. Im thinking i should just take a look, not sure why im posting first really, guess in the hope of a quick fire response…

    gd day

  14. a scris

    Hi thanks for the quick response. And fair enough. Thing is I have found close but not exactly it, and some that are exactly it but out of date (see code below if u can be arsed) and no use. The actual wordpress codex page for ‘post by email’ says at the bottom that it is possible as a hack in the new versions. But in practise it just doesnt seem to work, so Im figuring its just a little slice of code im missing that must be out there, surely there is not the need to use the full postie plugin for this. aHwell. this was the guy that did it previously seemingly, but not much action on the subject and its an obvious bugger, cant be doing with ugrading to 2.7 at the mo either.

    // Author: Anthony Jay Bull
    // URL: http://www.thebullog.net
    // Date: December 15th, 2007 (Tweaked for 2.3.0 support
    // Purpose: Add categories using brackets in the subject

    // Parse out the categories
    if ( ereg(“[{1}(.*)]{1}”, $subject, $regs) ) {

    // Do a lookup on the categories table
    $result = $wpdb->get_row(“SELECT term_id FROM
    $wpdb->categories WHERE name=’$regs[1]‘ ORDER BY term_id DESC LIMIT 1″);

    if ($result) {
    // If you get a result, set that ID
    // for use further below…
    $post_categories[0] = $result->term_id;
    } else {
    // else, insert a new row and get its new ID
    $ret_id = wp_insert_category(array(‘cat_name’ => $wpdb->escape($regs[1])));
    $post_categories[0] = $ret_id;
    }

    $subject = substr($subject, 0, strpos($subject, “[“) – 1);
    }
    }

  15. Harry Chefitz a scris

    I put your script in place and it runs without error.

    However, I am not using Yahoo or Gmail and it only posts the Subject line (as the title).

    Here is the output:

    Hey! Welcome to my optimized-for-Yahoo-and-Gmail interface.

    I’ve read the following encoding before actually reading the email (to evoid gmail errors):7BIT

    I’ve read the following encoding before actually reading the email (to evoid gmail errors):7BIT

    Gmail address NOT found. Going to normal email settings …

    Yahoo address NOT found. Going to normal email settings.
    Author of the blog post = xxx@xxx.com

    Author: 1

    Posted title: field trip: IMPORTANT DROP OFF REMINDER

    Mission complete, message 1 deleted.

    You can always check out my web site for more IT tricks: http://olivian.ro/

    My test blog is here:
    http://netivotmontessori.com/students/

  16. a scris

    Hello,

    From my experience, 7bit messages include pictures in email.

    Try sending only an email with text only. Then try sending an email with basic formatting (bold, italic, underline). Then try sending an email with pictures embedded.

    WP-Mail doesn’t “know” about pictures, and me “teaching” it would be hard. sad

    If you’re not putting pictures, you might be putting pictures from your email provider (a signature or such).

    Anyhow, mail encoded with 7bit is usually not displayed. sad

    Olivian BREDA’s last blog post..Why vote? The one reason for which anyone should vote. Read this message, and then go voting!

  17. Harry Chefitz a scris

    I’m using a Mac. So the email program is Apple Mail.

    I tested it with non-formatted text and it worked.

    I tested it with one bold word and it didn’t work.

  18. a scris

    Try and view the source of the email you send in non-formatted case and in formatted case.

    If you see 7-bit when you just bold a word, well that’s bad. sad

    Look at the source: if you manage to understand anything from the 7bit, then my software is not good. If you don’t get to understand the email, neither can the PHP software. sad

    It’s not typical & right to convert an email to 7-bit only for bolding one word.

    May I suggest another email software for Mac?

    Olivian BREDA’s last blog post..Why vote? The one reason for which anyone should vote. Read this message, and then go voting!

  19. a scris

    I tired with gmail a/c but didn’t work, it didn’t publish when it received new mails/post.

    The URL I used is: ssl://pop.gmail.com, is this right? However, I am still facing the error/status as “Slow down cowboy, no need to check for new mails so often!”. Any help will be appreciated.

  20. niki a scris

    is this even working now ???

    if we are doing this

    if (( is_email($author) ) && ($author == ‘PUTYOUREMAILADDRESSINHERE@SOMEMAIL.COM’)) {

    with this line:
    if ( is_email($author) ) {

    then where to specify email id ???

  21. a scris

    niki, this version worked fine for WordPress 2.7. I don’t know about the new versions, sorry; you could ask on the wordpress.org forums; let me know if I can help you with something.

  22. niki a scris

    thanks for your kind reply dude … i am searching to post via email to my self hosted wordpress blog and not able to find any thing for past 2 days .. can you please help with some other that is working …

    thanks in advance …

Trackback-uri (referințe externe)

Lasă un răspuns

Adresa ta de email nu va fi publicată. Câmpurile necesare sunt marcate *