php - 使用 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'.
解决方案
所以,在深入研究之后,我找到了解决方案。看来,一旦我添加:
$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']}
推荐阅读
- sas - 从多个 Excel 创建单个报告
- database - 微服务横向扩展/横向扩展数据库的最佳实践或设计
- hadoop-yarn - Apache flink - 在hadoop / yarn集群上提交作业后超时
- angular - 在 Angular 中多次嵌入一个组件?
- javascript - 如何使用轨道控制添加不同的相机?
- r - 保留所有不同行时如何指定要排除的列?
- javascript - MomentJS:显示两个任意日期之间的格式化范围
- zurb-foundation - 如何在基础 6 中使用 readmore.js
- java - TestNG 断言似乎不适用于 Cucumber / Gherkin Doc Strings?
- django - 如何只允许特定国家的用户访问我的 django 网站?