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>
frommy_email@mywebapp.com
toemail_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....)
Return-Path
for that purpose. – 04FSMAIL FROM
address; the receiving mail server then adds that to the message as a return-path header. This is also where bounces will be sent. – Synchro