views:

594

answers:

3

Hi guys - this may seem odd but I was wondering if it was possible to add custom header details to emails already in an inbox. Like lets say I wish to add in the Header of the email something like - myvariable = myvalue and then be able to query it somehow. I'm looking at code from Iloha mail and most of the details like subject and from recieved etc are in the headers and you can search through them. SO is it possible to add my own custom variable to an email header and query it in the same way? How can it be done using php?

EDIT ====================

Thanks I know how you can modify the headers of sent messages plus also query for custom variables in message headers however in this case I want to know if it would be possible to add a custom variable in a recieved message already in my inbox. Actually let me define the situation here.

I'm working on a google apps solution which requires maintaining references to emails. Basically the application is as such that when an email comes in - we create an order from that email and wish to maintain a reference to that EXACT email by some kind of identifier which would enable us to identify that email.

The fact is that we don't want to download the emails in a database and maintain a separate store as we would want to keep all the emailing on GMAIL. We just need:

A way to be able to 'link' to a specific email permanently - the UID is just a sequence number and not very reliable. We couldn't find any property of emails that could function as a unique ID or primary key and so we thought if we could instead generate a key on our end and store it in a custom variable on the email itself. However it seems unfortunately that there isn't a way to manipulate headers of an already existing email.

:( is there any solution to this problem I could use any IDEA !

+2  A: 

Yes you can add your own headers when sending an email...

<?php
$to      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'myvariables: myvalue';

mail($to, $subject, $message, $headers);
?> 

I doubt you can modify the existing emails headers unless they are stored in a database or something rather than just being retrieved from your POP/IMAP server.

fire
You could use IMAP to retrieve a message, modify it, upload it back (as new message) and delete the original. Of course, this might have undesirable consequences - for example, some information stored outside the email itself would get lost during the process, which is most likely undesirable.
Lukas Pokorny
I just need to have a way to add a certain variable to an email or uniquely identify an email - I can't use the UID property as it can be changed if sequence of emails is affected. Please check my update.
Ali
+1  A: 

I think the best solution is to either:

  • Connect to IMAP server, retrieve the emails, change header, put it back on the server, it seems at least that it was possible with gmail (Example in Java http://forums.sun.com/thread.jspa?threadID=5419712 for PHP look at http://www.php.net/manual/en/ref.imap.php but changing a header does not seem trivial)
  • If not possible anymore using gmail or with PHP, connect to the IMAP server, retrieve the emails, copy the content, create a new email with the new header, send it to a new mailbox connected to gmail.

Hope it helps.

snowflake
+1  A: 

Why don't you use the Message-ID header? That should be unique for every mail message (you can see it for any given Gmail message by clicking on the foldout menu and selecting "Show original").

Check section 3.6.4 of the Internet Message Format RFC (RFC2822) for more information (http://www.faqs.org/rfcs/rfc2822.html).

wimvds
No - this is a bad solution - the RFC clearly states the message ID is optional. Even if it was mandatory, this is generated by the origin nad the RFC also states that it is only unique within the scope of the origin host.However to be able to manage mail effectively, every repository must have a unique identifier for each message - although this may not be exposed by the protocol sitting on top of the repository.
symcbean