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);
Related
I have a PHP web app with a section to send invoice by email to all partners.
Each partner could have more than 1 email.
For example: COMPANY-1 with emails: email_1#company1.com and email_2#company1.com
I send all with phpmailer.
I save into my database the send-response from server mail ( in this case gmail).
If sent, i'll save true, else false - foreach email.
But some emails sometimes are incorrect (due to error typing when insert) or are dismissed by the company (in this case the company do not inform us and we still send to this deleted email) and I like to retrieve these "problems" using web app.
(If I login into google webmail I can see the email returned for each sent to an not existing email, with some advice/error code inside... but i don't know how to retrieve using PHP)
I tried using
$inbox = imap_open($hostname,$username,$password);
$emails = imap_search($inbox,'ALL');
This script works, but using ALL i get timeout.
I don't know the best method to retrieve these email of "NOT DELIVERED EMAILS FOR SOME PROBLEMS".
My first ideas was to set a custom Message-ID in the header when I send:
$messageID = "<" . md5($_SERVER['HTTP_HOST'].(idate("U")-1000000000).uniqid()).'-'.idate("U") . '#' . $_SERVER['HTTP_HOST'] . '>';
$mail->MessageID = $messageID;
and then in my script, using a command like:
$messageID = retrieve_from_database(...);
$emails = imap_search($inbox,'HEADER ' . $messageID);
but it doesn't exist a query HEADER
I'd like in my web app to show, for each email sent, eventually problem of bounced or undelivered but I don't know how and what I need to "ask" to imap_search to retrieve responses associated to the original sent email.
I read criterias list from docs, but I don't find anything usefull I think: https://www.php.net/manual/en/function.imap-search.php
Example of a scenario I need:
I send an email with Message-ID: <test#testXYZ> from my_email#mywebapp.com to email_not_existing#test.com. The server response true cause sent is done.
I insert a new entry on my database table emails_sent with send result true: INSERT INTO emails_sent (ID, email_dest, sent, MessageID, when) VALUES (455, 'email_not_existing#test.com', TRUE, $messageID, '2019-05-30 8:00:00')
The gmail daemon reply to my_email#mywebapp.com with a message of undelivered email
In my web app, when I go into page of sent emails, foreach ID from emails_sent table, i need to search on imap eventually response email with errors. In this example case when I ask for ID 455, I need to search on imap_search messages replyes associated with my email sent with MessageID assigned by me (or other method i don't know....)
I would advise you to get away from shared hosting and get a proper server (try scaleway.com). As you're discovering, it means you don't get sufficient control.
I'd recommend against using IMAP as your database - as you're finding it's not good enough for the kind of searching you want to do. If you import all the bounced messages into your own DB, you're free to parse and search in whatever way you like, and it's also easy to get all messages from IMAP.
The most efficient way to handle bounces is to get your SMTP server to pipe bounce messages directly into a script. This will give you the complete bounced message, along with environment variables that tell you more about the delivery. That of course requires that you have config-level access to your mail server, or a hosting provider with a clue.
Be warned that bounce handling is an exceedingly unpleasant task, and is very difficult to do well. You may want to look at a 3rd-party bounce handler like Boogie Tools bounce studio to save you some sanity! Aside from some very broad top-level standards, there is very little consistency at a lower level, and bounces are often useless. For example, MS Exchange can produce bounces that do not contain the bounced message's original target address at all! The way around this is to use VERP, where every address (or even every message) gets its own unique return path, allowing you to know where a message bounced from even if the message content contains no clues.
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.
I want to find out if there is a way to read email information from a certain address with php, and take the From,To,Subject,Date and store them into a MySQL database. I would like to either check for new mails on a button click for example, or ideally when an email is received to get somehow the new mail and store the info i want.
I hope i have explained my question well..
it depends if the site is yours.
if you are the owner of it or the coder it is possible to save it while sending simply by collection the info and saving it and only then send the email.
but if the email service itself is not yours (for example Gmail, Yahoo etc..)
you cannot save the data with php. creating a program that reads emails could be possible, but i myself havent gone far enough to make a program that reads sites.
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.
I am sending some mailings to a 5k list. I am not using any mailing commerce services. As expected I am getting some mails back from my mail-deamon and I would like to delete the those from the recipient-list. Is there a way to read the pop-mails programmatically?
is there maybe an easy and quick workaround?
A way to read POP mail via PHP: PHP: Download incoming email from POP3 or IMAP, parse it, and mark it as read/delete on server
(The code provided in the answer is using the functions suggested by Lèse majesté in its comment, but you find a ready to use small piece of working code thet does exacly what you need).
Obviously you will need to write also the code to programatically delete the address from your newsletter's DB.
I would use a cronjob (very easy to set on an hosting with cPanel) to programatically call the code that read the POP3 account and deletes the invalid addresses from DB.
A workaround? I don't know.
Can't you just set your "from" and "reply-to" address to an email address you can check? and then just check that email inbox to see what has bounced.