PHP Imap: which unique identifier should i use? - php

Im in the meddle of creating a web app that downloads all the emails using imap library in php but im troubled on which id should i used, This is a sample email header retrieve from a gmail account.
Array
(
[0] => stdClass Object
(
[subject] => test subject
[from] => someone
[to] => enquire#xxxx.com
[date] => 17 Jun 2016 10:33:13 +0900
[message_id] => <08a32A30-C6n7-4A4A-AE91-C35429AF48BA#x2mail.xxxx.com>
[size] => 8460
[uid] => 8645
[msgno] => 8522
[recent] => 0
[flagged] => 0
[answered] => 0
[deleted] => 0
[seen] => 0
[draft] => 0
[udate] => 1466127256
)
)
as you can see above there is a "uid" and "msgno" which should i use?

http://php.net/manual/en/function.imap-uid.php
Here I read: An UID is a unique identifier that will not change over time while a message sequence number may change whenever the content of the mailbox changes.
So, "uid" would probably be better.

Related

Accessing ddeboer/Imap Mail Header

I'm accessing a mailbox using ddeboer/imap. Connecting to the Server and retrieving Messages ist not a problem. But $message->getHeaders() returns the following (shortened Version):
Ddeboer\Imap\Message\Headers Object
(
[storage:ArrayIterator:private] => Array
(
[date] => Fri, 5 Jul 2019 07:00:47 +0200
[subject] => Test Mail
[message_id] => <108a4850-284e-170a-2c7d-b6f9g5218202#test.de>
[fromaddress] => "Test" <test#test.de>
[from] => Array
(
[0] => stdClass Object
(
[personal] => Test
[mailbox] => test
[host] => test.de
)
)
[deleted] =>
[draft] =>
[msgno] => 1
[maildate] => 5-Jul-2019 07:00:49 +0200
[size] => 223715
[udate] => 1562302849
)
)
How am I supposed to access the Information e.g. udate or from->mailbox?
You can use the get method and add the path
$message->getHeaders()->get('udate'):
$message->getHeaders()->get('from')[0]->mailbox

looping through object in php gives error

Okay.. I did a lot of research but I kept getting errors so I decided to ask the question directly..
I always converted objects to arrays, but I now want to try using an object directly to save website speed.. I got this object:
stdClass Object
(
[date] => Wed, 8 Feb 2017 15:03:44 +0000
[Date] => Wed, 8 Feb 2017 15:03:44 +0000
[subject] => asdasdasd
[Subject] => asdasdasd
[message_id] =>
[toaddress] => "test#hotmail.com"
[to] => Array
(
[0] => stdClass Object
(
[personal] => test#hotmail.com
[mailbox] => test
[host] => hotmail.com
)
)
[fromaddress] => Jason K
[from] => Array
(
[0] => stdClass Object
(
[personal] => Jason K
[mailbox] => JasonK
[host] => hotmail.com
)
)
[reply_toaddress] => Jason K
[reply_to] => Array
(
[0] => stdClass Object
(
[personal] => Jason K
[mailbox] => JasonK
[host] => hotmail.com
)
)
[senderaddress] => Jason K
[sender] => Array
(
[0] => stdClass Object
(
[personal] => Jason K
[mailbox] => JasonK
[host] => hotmail.com
)
)
[Recent] =>
[Unseen] =>
[Flagged] =>
[Answered] =>
[Deleted] =>
[Draft] =>
[Msgno] => 1
[MailDate] => 8-Feb-2017 16:03:25 +0100
[Size] => 7887
[udate] => 1486566205
)
I am trying to get the first date (wed, 8 feb 2017), the from->mailbox and the Size. I managed to get the from->mailbox with this code:
foreach($EmailHeaders->from as $from ){
echo $from->mailbox;
}
But I just cant find out how to obtain the other values aswell.. If i try:
foreach($EmailHeaders as $headers){
echo $headers->date;
}
then it doesnt work... Can anyone explain this to me? Sorry if this is already asked a thousand times before, I just cant figure it out..
check this http://php.net/manual/en/function.get-object-vars.php
var_dump(get_object_vars($EmailHeaders ));
It's simple, you don't have to iterate over the direct properties, just use
$EmailHeaders->date
To get the "date", you just need to use $EmailHeaders->date
You don't need a foreach. It's working for "from" because it contains an array of objects.
You can simply do
$EmailHeaders->date
OR
$EmailHeaders->Date
to get the date from your object.
While iterating through loop, your pointer is already on the date item, so you can't point to date index. It will point to the date index under date item.

Retrieve email address from imap header

I'm using php imap function imap_header to extract full header and imap_fetch_overview to extract raw header. Both the functions gives me stdClass object and arrays respectively.
I would like to always clean my from and to before further processing. Sometimes a FROM or TO can contain some thing like this,
Test user <test#test.com>
Also currently in my FROM I only see Test User and no email address until I use firebug.
How do I just get the test#test.com from these objects, arrays?
This is the result I get from imap_fetch_overview
Array
(
[0] => stdClass Object
(
[subject] => Testing
[from] => Test User
[to] => testuser2#test.com
[date] => Wed, 17 Apr 2013 18:43:46 +0530
[message_id] => <abcdef1244.93784jgsfk#test.com>
[size] => 3443
[uid] => 1234
[msgno] => 123
[recent] => 0
[flagged] => 0
[answered] => 0
[deleted] => 0
[seen] => 0
[draft] => 0
[udate] => 1366204439
)
)
There is a hidden <test#test.com> next to Test User. How do I extract that email address?
Similarly this is what I get from imap_header
stdClass Object
(
[date] => Wed, 17 Apr 2013 18:43:46 +0530
[Date] => Wed, 17 Apr 2013 18:43:46 +0530
[subject] => Test
[Subject] => Test
[message_id] => <abcdef1244.93784jgsfk#test.com>
[toaddress] => testuser#test.com
[to] => Array
(
[0] => stdClass Object
(
[mailbox] => testuser
[host] => test.com
)
)
[fromaddress] => Test User
[from] => Array
(
[0] => stdClass Object
(
[personal] => Test User
[mailbox] => test
[host] => test.com
)
)
[reply_toaddress] => Test User
[reply_to] => Array
(
[0] => stdClass Object
(
[personal] => Test User
[mailbox] => test
[host] => test.com
)
)
[senderaddress] => Test User
[sender] => Array
(
[0] => stdClass Object
(
[personal] => Test User
[mailbox] => test
[host] => test.com
)
)
[Recent] =>
[Unseen] => U
[Flagged] =>
[Answered] =>
[Deleted] =>
[Draft] =>
[Msgno] => 123
[MailDate] => 17-Apr-2013 13:13:59 +0000
[Size] => 3443
[udate] => 1366204439
)
A preg_match would be the obvious answer but just cant seem to figure out how to perform it on from bit where email address is not seen in the browser but is present when inspected with firebug. Any help is appreciated.
Thanks.
You can go one step further with imap_rfc822_parse_adrlist()Docs:
$toAddresses = imap_rfc822_parse_adrlist('Test user <test#test.com>', 'localhost');
print_r($toAddresses);
Array
(
[0] => stdClass Object
(
[mailbox] => test
[host] => test.com
[personal] => Test user
)
)
If name of array of std object is $arr
$arr[0]->to
returns what you want.
in second case
$stdObject <- object that is returned by second function
foreach($stdObject->to as $to)
{
echo $to->mailbox;
echo $to->host;
}
I've just discovered this thread and whilst it's old I hope this comment might help. I believe I've answered the query. As it happens, the format of the sender email is what's causing the issue.
From Name <from#name.com>
Because of the <> tag, the browser is making it into an HTML tag, I guess something akin to a span. By using str_replace to change "<" into " " (or whatever) you remove the issue before it gets to HTML

Storing Array in memcache using PHP , array element gets unset after sometime from memcache

I am saving an array of 29 elements in a memcache key 'getBannerInfo' . The array is something like :
[bannerid] => 5059
[campaignid] => 2687
[contenttype] => png
[storagetype] => url
[filename] =>
[imageurl] => http://testanalyst.org/c2687/13381938962791_50x50.png
[htmltemplate] =>
[htmlcache] =>
[append] =>
[width] => 50
[height] => 50
[weight] => 1
[url] => http://www.google.com
[alt] => new image
[bannertext] => New image
[description] => test image alt text 23
[autohtml] => f
[alt_contenttype] =>
[comments] =>
[status] => 0
[compiledlimitation] => xyz
[acl_plugins] => lmn
[acls_updated] => 2012-05-28 16:49:02
[parameters] => N;
[custom_data] => abc
[target] =>
[capping] => 0
[block] => 0
[is_deleted] => 0
Now , the odd behavious that I noticed that allthough initially it is set as expected , but after sometime , when , it fetches the data from the memcache key , the alt key=>val pair is not present in the fetched array .
I am using
set($key,$arrayName,3600); and get($key)
of the Memcache extension class.
Why is this happening with the alt key specifically , this key I have added to this array recently and since then this issue has been noticed .
A cache is just a temporary buffer and it can delete records, if it runs out of space, or the records expired. That is the expected behaviour. If you need a persistent storage, use a database.

PHP: Problems with Microsoft Exchange, no have body of email in PHP

I have a litte problem with imap_fetchstructure, Microsoft SMTP Server (?) and signed mails by application/pkcs7-signature. Normally when I send signed email to gmail server I don't have any troubles. But when I send this on Microsoft Exchange I have this structure from IMAP:
stdClass Object
(
[type] => 1
[encoding] => 0
[ifsubtype] => 1
[subtype] => SIGNED
[ifdescription] => 0
[ifid] => 0
[bytes] => -1
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => protocol
[value] => application/pkcs7-signature
)
[1] => stdClass Object
(
[attribute] => micalg
[value] => sha1
)
[2] => stdClass Object
(
[attribute] => boundary
[value] => ----34EFCEA0D98F83964735A9A256302F5D
)
)
[parts] => Array
(
)
)
As you can see, I don't have parts, and my question is, why I don't recived this element of IMAP structure?
Additionally, when I print imap_body, I can see what is in email.
I don't have access to Microsoft server, anyone have something like this?
php imap class is very buggy, when I wrote my email system, i got very big amount of bugs. There is workaround, you have to search it. Try to use some imap_class.php open source.

Categories