How to download emails through IMAP when uid and msgno can change - php

I've written a small script that simply (as simple as email can be) downloads emails so I can play with them later. The part that I don't seem to be able to grasp quite perfectly is that the msgno can (will) change when new emails get added or emails get deleted. Okay, that's fine, but then for the uid, this also can (will) change when emails are moved from folder to folder.
So in PHP, for example, when I execute imap_search and get a list of msgnos or uids, how do I reliably use these to get emails for a changing email box? In this example, there are multiple Outlook clients connected to this mailbox with many rules for auto-moving emails from/to specific people, etc. that's bound to move emails after I've already gotten the ID's, and before I actually get to processing that email.
In this case, what do you do? I would very much so like to avoid the email just "not existing" by the time I get to it, or worse, the ID in that IMAP mailbox be resigned to a new email.

Related

Simple way to forward emails using PHP IMAP

I'm writing a script that will get all the new emails in different accounts on the same server, check for keywords and then move each email to different account and folder in that account depending on the keywords found.
I've been looking for a simple way to move the emails from one account to another, and to a folder in that account. There doesn't seem to be a simple imap_forward() function. imap_mail_move() only works within the same account. I've been searching the internet for hours looking for a way to do this without having to loop through attachments and fetching each part of the email before sending it to the destination address like I would a new email. I need to make sure no info gets lost during this process and the email arrives at the destination exactly the same as it was initially received. It can look like a forwarded message, that's fine, as long as the attachments, from address and body are intact.
If you also have IMAP access to the destination mailbox, you can have two IMAP connections open simultaneously. Use one connection to read the message from the source account and the other to imap_append() it to the destination account.
You'll have to manually re-build the message headers, but that should be pretty simple, looks like you can just do this:
$newMessage = imap_headers($sourceStream) . "\r\n" . imap_body($sourceStream)
imap_append($destinationStream, $destinationMailbox, $newMessage);

Email to trigger PHP Script... Possible?

I'm in need of setting up an auto-response from an email account that I control, based on trigger words within the body of the email. But also, it needs to add/delete rows from a database table based on trigger words sent to this email account (that belongs to my site) and it needs to create a topic, in some cases, within a forum, if sent to a different email address (but on the same server). I know PHP to be able to do this, but not really sure how to trigger a PHP script to be executed when an email gets sent to a specific email address account that I control. Or if there is another solution to accomplish this, please let me know.
This is basically an inquiry on how to accomplish something like this, based on an email sent to a specific email address on my server with words like: "Join", "Leave", "Set Mail", "Set Digest", etc. etc.
There will be another email address account set aside that will need to send those subscribers in the database, the same exact email (Mass Send). This is for a CDB-L ListServ. Kind of old school I suppose, but we want to transfer this ability to our server, since these old school methods still work today and is very much active.
Curious on security issues, what type of server software I'll need and just a basic approach on how to set something like this up.
You can alias your email address directly to a php script if you run your own mail server (on linux this would just be in the /etc/alias file or equivalent where the target was your php script instead of an email address) eg http://www.topwebhosts.org/bbs/board.php?bo_table=server_mgmt&wr_id=73
If not, then your only real choice is to set up a php process that checks an email address for mail every x minutes.
I have used both these methods over the years to great success
You can walk through emails with PHP's IMAP functions and undertake action (based on conditions/content). More info.

Save all incoming emails to MySQL database

SO what I would like to do, is have all my emails routed to a PHP script, and then for that PHP script to save it to the correct staff members message box in my CRM.
My CRM is already able to send out emails but I cant seem to allow it to recive messages from customers.
My idea, is be able to keep track of which emails they are sending, and easy access to emails that need there attechion.
The Idea being there would be no need for them to open webmail or outlook.
I have followed this tutorial and it worked for me (using Exim) with a little playing around:
http://www.evolt.org/incoming_mail_and_php
Note: the link seems dead so you might need to look up the Google cache:
http://webcache.googleusercontent.com/search?q=cache:6rwThYgNHe0J:www.evolt.org/incoming_mail_and_php+php+receive+email&cd=2&hl=en&ct=clnk

PHP - Email-in posting (like Posterous)?

I have a small web app to which users upload photos. I want to let them email in their photos (from their iPhones/Android phones) and a number associated with that photo. It'd be kind of like posterous but only allow emails with one photo and one number.
How could this be done?
I see two ways, but I'm not sure if I'm even close.
1) Have one email (like Posterous) that users send mail to, then send to it from the same email that their account is under.
2) Have a unique email for every user that they can email to. (But this has the problem that anyone could send emails to the unique email.)
To do either way, you'd need to have a filter on the mail server that takes incoming mail and fires off a script that inserts that information into the db, or does whatever else with it.
It's not too hard, but usually requires admin (or root) access to the machine, i.e., can't be done in a shared hosting environment.

PHPMailer - Double Opt In - Spam - Confirm script and multiple email addresses

I have written a script for my clients to generate a newsletter form, embed it on their website, collect email addresses and then email them using a php while and PHPMailer.
Currently, the script uses a "double opt in" which sends an email from "myclient#myclientsemail.com" to the subscriber to click on a confirm link that then goes to "http://myserver.com" to complete the confirmation process.
My question is,
the mail address they are receiving the confirm script from and the address they are confirming their email address with is different, will this be a problem?
In other words, does the spam filter add the confirm email "from address" to the friends list or the address of the link in the email?
The desired end result obviously being the "from address" (my clients address) being the one that will not be junked in future.
Thanks,
Anthony
My company does something similar. The double opt-in is something that you're doing on your end to ensure that you have valid results, not an email sent in by the user's enemy wanted to load up their account with spam. The theory goes that if someone is going to be malicious like that, they wouldn't have actual read access and time/means to respond to the second opt-in, making it about as fail proof as possible.
The spam filters don't know if you did an opt in at all, they just know what they see and who's doing the sending. So as long as you're sure the user you're getting approval from is the same one, you should be good to go. If you've properly configured headers, ensured list accuracy, aren't mail bombing (i.e. loading a host with thousands of messages in a short amount of time) and include content that's not "spammy" you'll be fine. Just remember, there's virtually no way to 100% guarantee delivery of an email thanks to overzealous IT guys and spam filters and the distributed nature of the web.
Note that being a new system, your host is not yet "trusted" so you'll have higher than average bounce stats likely. I'd recommend doing a "break" script when you send to ensure that emails are "trickling" out at first while you gain hosts trust. Even with well-established servers, my company's policy is no consecutive emails to a host will be delivered with less than 50 ms break in between, and it's worked well for us (we deliver 100k+ per day) And no matter what, include CAN-Spam items such as opt-out, physical address, and proper subject line...no exceptions.

Categories