alesch
alesch

Reputation: 842

Getting the original sender when forwarding emails

How can I get the address of the original sender, when an email has been forwarded to Mailgun?

The chain of events looks like this:

  1. originalSender sends message to someUser
  2. someUser forwards message to Mailgun
  3. Mailgun POSTs a parsed message to my server

Put in another way:

orignalSender (send)->  someUser (forward)->  mailgun (POST)->  myserver

The best I could get is doing a regex on the "body-plain" property.
The problem is that email clients do send this differently. Here are two examples.

Forwarding from GMail (I added the ...):

body-plain: "---------- Forwarded message ----------\r\nFrom: Kalle Kalleson <[email protected]>\r\nDate: 2014-02-13\r\n ..."

Forwarding from Apple's Mail (I added the ...):

body-plain: "(...)Begin forwarded message:\r\n\r\n> From: Kalle Kalleson <[email protected]>\r\n> Subject: New color printer\r\n> Date: 11 February, 2014 15:47:19 GMT+1\r\n> 

There must be a better way of doing this, right?
Thanks in advance!

Upvotes: 2

Views: 2631

Answers (4)

vijay
vijay

Reputation: 791

Not possible to take original sender of an email throw any mail services.

So, we implemented regex and take the first occurence of the match from mail html body.

Regex.Match only returns first match so used this with below regex.

From:\s(.*?)>

https://regex101.com/r/1pUpPU/1

Upvotes: 0

API_sheriff_orlie
API_sheriff_orlie

Reputation: 1273

Using a regular expression should do the trick in either case. Try:

/(From:.*>)/g

Upvotes: 0

danielh
danielh

Reputation: 354

Perhaps I am missing what you are looking for, but when Mailgun POSTs to your server, you should be able to pull the From field from the POST data. I'm using a node.js app to parse my messages, however, in PHP it would look something like:

<?php
    $from = $_POST["From"];
    echo "This message is from: ".$from;
?>

I apologize if I'm missing what you're asking.

Upvotes: 0

alesch
alesch

Reputation: 842

I've just been in contact with Mailgun support and they could not offer a different strategy.
That is, parsing the body myself, taking in account the differences between email clients.
Lame I would say, :-(

Here you can vote up the feature request.
http://mailgun.uservoice.com/forums/156243-general/suggestions/5528656-extract-the-original-sender-of-a-forwarded-email

Has anyone come up with a better answer?

Upvotes: 1

Related Questions