使用提取(乏味)来往/从

时间:2019-01-24 15:14:54

标签: php email

所以我试图在本地服务器上启动Fetch(乏味的:https://github.com/tedious/Fetch)set并运行,并且一切都进行得很顺利。我可以使用$body = $message->getHtmlBody();来使身体正常运行,但是我我对如何实际到达/从那里有点困惑。我以为我可以简单地通过使用$sentFrom = $message->getAddresses('from')来获得,但是我没有得到正确的结果。我查看了Message.php文件,是一个名为getAddresses()的函数:

    /**
 * This function returns either an array of email addresses and names or, optionally, a string that can be used in
 * mail headers.
 *
 * @param  string            $type     Should be 'to', 'cc', 'bcc', 'from', 'sender', or 'reply-to'.
 * @param  bool              $asString
 * @return array|string|bool
 */
public function getAddresses($type, $asString = false)
{
    $type = ( $type == 'reply-to' ) ? 'replyTo' : $type;
    $addressTypes = array('to', 'cc', 'bcc', 'from', 'sender', 'replyTo');

    if (!in_array($type, $addressTypes) || !isset($this->$type) || count($this->$type) < 1)
        return false;

    if (!$asString) {
        if ($type == 'from')
            return $this->from[0];
        elseif ($type == 'sender')
            return $this->sender[0];

        return $this->$type;
    } else {
        $outputString = '';
        foreach ($this->$type as $address) {
            if (isset($set))
                $outputString .= ', ';
            if (!isset($set))
                $set = true;

            $outputString .= isset($address['name']) ?
                $address['name'] . ' <' . $address['address'] . '>'
                : $address['address'];
        }

        return $outputString;
    }
}

我可能只是犯了一个愚蠢的错误,但现在我不知道。任何见解都会很有帮助。

因此,当前,我只是遍历消息并输出消息的主题,从,到,以及正文。例如:

$server = new Server('imap.myserver.com', 993);
$server->setAuthentication('test@domain.com', 'mypassword');

$messages = $server->getMessages();

$i = 0;
$output = "";

foreach($messages as $message) {
    $html = true;
    $subject = $message->getSubject(); //get the subject of the email
    $sentFrom = $message->getAddresses('from'); // who sent the email
    $sentTo = $message->getAddresses('to'); //who is the email to
    $body = $message->getHtmlBody();

    //construct the email with a wrapper for little nicer look
    $output .= "<div class=\"email\">";
    $output .= "<div class=\"header\">";
    $output .= "<p>{$subject}</p>";
    $output .= "<p>{$sentFrom}</p>";
    $output .= "<p>{$sentTo}</p>";
    $output .= "</div>";
    $output .= "<div class=\"body\">";
    $output .= $body;
    $output .= "</div>";
    $output .= "</div>";
}

echo $output;
if (++$i = 10) break;

如果一切正常,当刷新页面时,脚本将连接到电子邮件服务器,检查电子邮件,并收集foreach中所有电子邮件的主题,主题,主题。它还会将电子邮件输出到屏幕。不幸的是,只有两个变量不能真正起作用:$ sentTo和$ sentFrom。我想我只是感到困惑,为什么当Message.php中的函数说:

时,为什么我没有使用$sentTo = $message->getAddresses('to');返回地址?
 * @param  string    $type     Should be 'to', 'cc', 'bcc', 'from', 'sender', or 'reply-to'.

1 个答案:

答案 0 :(得分:0)

因此,在深入研究之后,我找到了解决方案。看来,一旦我加入:

$arrayedTo = print_r($sentTo);
$arrayedFrom = print_r($sentFrom);

我得到了以下

Array
(
    [0] => Array
        (
            [address] => firstlast@mydomain.com
            [name] => My Name
        )

)

Array
(
    [address] => firstlast@sentfromdomain.com
    [name] => Name of Sender
)

因此,在我的foreach循环中,我必须执行以下操作才能获得正确的结果。我想我只是忽略了它返回一个数组的事实。

Sent From Address: {$sentFrom['address']}
Sent To Address: {$sentTo[0]['address']}