简体   繁体   中英

Get from email address with email parser

I am using this absolute amazing piece of code: https://github.com/plancake/official-library-php-email-parser/blob/master/PlancakeEmailParser.php

But the one thing it is missing is the ability to get the From email address.

I have simple added:

public function getFromEmail()
{
    if (!isset($this->rawFields['from']))
    {
        return false;
    }

    return $this->rawFields['from'];
}

But how would I get only the email address part at the moment it returns: John Smith<john@gmail.com> ?

Also I would need this to work if the From address was only john@gmail.com ?

Thanks to the answers this was the finished code:

public function getFromEmail()
{
    $email = trim($this->rawFields['from']);

    if(substr($email, -1) == '>'){
        $fromarr = explode("<",$email);
        $mailarr1 = explode(">",$fromarr[1]);
        $email = $mailarr1[0];
    }

    return $email;
}

This is a very simple regular expression:

$output = array();
preg_match("/.*<(.*?)>.*?/", $this->rawFields['from'], $output);
$email_address = $output[1];

Care though: If someone's name contains < or > it might cause a security vulnerability. The lazy operator (*.?) is used to ensure the last set of < > is used.

HTH

PS: Use http://gskinner.com/RegExr/ to test Regular Expressions!

$mailid='John Smith<john@gmail.com>';
$mailarr=explode("<",$mailid);
$mailarr1=explode(">",$mailarr[1]);
$just_emailid=$mailarr1[0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM