Aaron Hill
Aaron Hill

Reputation: 641

How to get the sender's email address from Zend_Mail_Message?

I'm using the Zend_Mail_Message class to display emails inside of my PHP app, and am able to output the subject, content, date and even the NAME of the sender (using $message->from) but I can't figure out how to get the email address of the person who sent the message. The documentation is no help and googling finds a million results for how to send messages with Zend, but nothing about getting the address that sent the message.

Upvotes: 4

Views: 2177

Answers (3)

da2ky
da2ky

Reputation: 81

This works well..

        $senderMailAddress = null;
        foreach ( $message->getHeader('from')->getAddressList() as $address ) {
            if ( $senderMailAddress === null) {

                $senderMailAddress = $address->getEmail();
            }
        }

Upvotes: 2

Aaron Hill
Aaron Hill

Reputation: 641

EDIT:

This is how I ended up doing it. After some more digging, I found the sender's email in a field called 'return-path'. Unfortunately, this field has a dash in the name (WTF??) so to access it, I had to do this: $return_path = 'return-path'; $message->reply_to = $zendMessage->$return_path;


Using the return-path caused some problems with some emails, specifically messages from no-reply accounts ([email protected], [email protected] etc). These addresses would end up looking something like this:

m-9xpfkzulthmad8z9lls0s6ehupvordjdcor30geppm12kbvyropj1zs5@bounce.linkedin.com

...which obviously doesn't work for display in a 'from' field on the front-end. Email clients like Apple Mail and Gmail show [email protected] or [email protected], so that's what I was going for too.

Anyways, after some more research, I discovered that the 'from' field in a Zend Mail Message object always looks something like this:

"user account name" <[email protected]>

The part in < > is what I was after, but simply doing $from = $zend_message->from only gave me the user's account name (hence my original question). After some more playing around, this is how I finally got it working:

  $from   = $zendMessage->from;
  $start  = strpos($from, '<');
  $email  = substr($from, $start, -1);
  $result = str_replace('<', '', $email);

Hopefully this will save someone some frustration. If anyone knows of a simpler way of doing this, please let me know.

Upvotes: 3

Adrian World
Adrian World

Reputation: 3128

The main problem here is that many email programs, relay agents and virus scanner along the way do funny stuff to an actually simple and well defined email standard.

Zend_Mail_Message extends to Zend_Mail_Part which has a method called getHeaders(). This will have all the data from an email stored in the head versus the body which is accessed with getContent() and the actual email message.

With this method you'll get an array of all the key/value pairs in the header and while developing you should be able to determine which header field you will actually want. Once you know that you can then get the actual field with getHeader('field_name') or with its actual name directly.

However, if you have to many different email senders you may want to stick with the complete header array though and evaluate multiple fields for the best result like if there's an "reply-to" address. Again there are many uncertainties because the standard isn't always obeyed.

Upvotes: 1

Related Questions