tags:

views:

378

answers:

2

I've done quite a bit of inbox manipulation with Gmail via IMAP functions in PHP, but one thing I haven't found is a way to create messages. I'm not sure if IMAP or SMTP is required, but I would like to use PHP to create a new message (specifically a draft) that is stored in my inbox with everything ready to hit send at a later date. How do I go about this?

+1  A: 

You might want to look at imap_mail_compose()

Edit This doesn't create the message on the server. You need to use imap_append() also.

Further Edit This seems to work ok:

<?php 
$rootMailBox = "{imap.gmail.com:993/imap/ssl}";
$draftsMailBox = $rootMailBox . '[Google Mail]/Drafts';

$conn = imap_open ($rootMailBox, "[email protected]", "password") or die("can't connect: " . imap_last_error());

$envelope["to"]  = "[email protected]";
$envelope["subject"]  = "Test Draft";

$part["type"] = TYPETEXT;
$part["subtype"] = "plain";
$part["description"] = "part description";
$part["contents.data"] = "Testing Content";

$body[1] = $part;

$msg = imap_mail_compose($envelope, $body);

if (imap_append($conn, $draftsMailBox, $msg) === false) {
        die( "could not append message: " . imap_last_error() )  ;
}
Tom Haigh
Great, thanks! I wasn't sure if this actually created the message on the server or not. The docs make it sound like it just creates a MIME string of the $envelope and $body arrays.
Kevin
yeah I think you might be right
Tom Haigh
Thanks for that edit, I just discovered imap_append as well.
Kevin
A: 

you should be able to create drafts just by moving the composed message into Drafts floder...

dusoft
Composing the message to begin with was the trouble I was having :)
Kevin