Using imap_search filters in PHP
I need to filter the mails on the basis of the available headers.
I did for "To" field using something like "TO: admin@mysite.com", but I also n开发者_StackOverflow中文版eed to apply filter to "From" field, but the from field is dynamic — it can be admin@yoursite.com or support@yoursite.com or any other@yoursite.com domain, so the from field is never the same. So I was wanting to know if there is any "like" filter for "From" field ,or any better solution, so I can set something like "%@yoursite.com" so it will be dynamic?
You can use substring for imap_search function, so the following code should work in your case:
imap_search($conn, 'TO "admin@mysite.com" FROM "@yoursite.com"');
Edit: removed the @
sign from the search.
Edit2: I looked in both the RFC mentioned on the imap_search
page, i.e. 2060 and 1176, they don't say nothing about @
sign in the address, neither for any other character which should not be used when searching in addresses.
I tried this code on my mailserver and it does work, so @
is allowed when searching for addresses (it'd be useless if haven't).
<?php
$conn = imap_open('{imap.example.com:993/imap/ssl}INBOX', 'foo@example.com', 'pass123', OP_READONLY);
$some = imap_search($conn, 'FROM "@example.com"', SE_UID);
So you can definitely use the @
, since it will match what you've requested.
精彩评论