How to get your Mail Receipts in PHP

I accept that I am not a regular writer. However, very recently one of my friends requested me to supply a technique so that he will have one Delivery notification or Mail opening notification for each mail he sends to others. That was the reason – I am writing this article so that, other techie people may know the procedure.

The trick is very simple and used for quite a long time.

Normally, we use one HTML email reader which shows all the content that the HTML specifies. Therefore the HTML email reader might also show the external content if anything such embedded within the HTML mail.

Now the question is what will be the form of such external content.

In case we send an HTML email with an IMG tag within the body. Then, the IMG source will not point to an actual image, but instead it will point to a PHP page on sender’s web server as follows:

<code>

<img src=”http://senders.com/mailreceipt.php?who=recipient1&#8243; height = “1px” width=”1px” />

</code>

Save this HTML code to a file e.g. sample.html 
Then run this sample.html on your browser.
A blank page will be shown.
Press CTRL+A to select everything shown on the blank page.
Copy it with CTRL+C and paste on the body of your outgoing mail.

Now on the root directory of senders.com server – save or upload the file mailreceipt.php as follows:

<?php
       $from_address = 'none@none.com';
       $notify_to = 'you@yourdomain.com';
       $subject = "Mail opened by ".$_GET['who'];
       $body = $_SERVER['REMOTE_ADDR'].' '.$_SERVER['HTTP_USER_AGENT'];
       $headers = "From: ".$from_address."\r\nX-Mailer: php";
       mail($notify_to, $subject, $body, $headers);

?>

The $notify_to variable will be the email address where the return-receipt gets sent.

The $from_address variable is the address that the notification will appear to come from, and in most cases this can be a non-existing email address.

Now whenever you send any mail attached with the IMG tag as shown above it will call your remote PHP script when the recipient opens your email.

When you test my code,

  1. Do not forget to change your $notify_to variable – where you want the notification
  2. Change the domain server name senders.com – where you uploaded the mailreceipt.php
  3. who=recipient1 – will be who=<email address for whom the notification is required>