首页 > 解决方案 > 使用 Fetch(乏味)来接收/接收电子邮件

问题描述

所以我试图让 Fetch(乏味:https ://github.com/tedious/Fetch )在本地服务器上设置和运行,一切都在顺利进行。我可以通过使用来很好地获得身体:$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。我想我只是困惑为什么$sentTo = $message->getAddresses('to');当 Message.php 中的函数说时我没有得到返回给我的地址:

 * @param  string    $type     Should be 'to', 'cc', 'bcc', 'from', 'sender', or 'reply-to'.

标签: phpemail

解决方案


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

$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']}

推荐阅读