PHP - IMAP - 是否可以从已发送的电子邮件中获取收件人的电子邮件地址?
PHP - IMAP - Is it possible to get receivers email address from a sent email?
我正在尝试从我的电子邮件客户端发送的电子邮件中检索收件人的电子邮件地址。我使用 PHP 和 IMAP 来完成工作,但我现在卡住了,因为我无法从电子邮件中获取收件人的电子邮件地址。似乎只能得到收件人的名字。
有谁知道这是否可以做到以及如何做到?
可以从消息header信息中获取收件人的邮箱地址。您需要为每条消息调用 imap_headerinfo()
函数。
imap_headerinfo($connection, $msg);
这会 return 一个 object,其中包含所有 header,如下所示:
stdClass Object
(
.
.
[to] => Array
(
[0] => stdClass Object
(
[mailbox] => testemail
[host] => gmail.com
)
)
)
您可以将 to
属性 的 mailbox
和 host
连接起来以获得收件人的电子邮件。这是一个例子:
$connection = imap_open($host, $uname, $pwd) or die(imap_last_error());
$messages = imap_search($connection, 'ALL');
foreach($messages as $msg) {
$header = imap_headerinfo($connection, $msg);
foreach($header->to as $receiver){
echo $receiver->mailbox.'@'.$receiver->host.'<br/>';
}
}
希望对您有所帮助!
我正在尝试从我的电子邮件客户端发送的电子邮件中检索收件人的电子邮件地址。我使用 PHP 和 IMAP 来完成工作,但我现在卡住了,因为我无法从电子邮件中获取收件人的电子邮件地址。似乎只能得到收件人的名字。
有谁知道这是否可以做到以及如何做到?
可以从消息header信息中获取收件人的邮箱地址。您需要为每条消息调用 imap_headerinfo()
函数。
imap_headerinfo($connection, $msg);
这会 return 一个 object,其中包含所有 header,如下所示:
stdClass Object
(
.
.
[to] => Array
(
[0] => stdClass Object
(
[mailbox] => testemail
[host] => gmail.com
)
)
)
您可以将 to
属性 的 mailbox
和 host
连接起来以获得收件人的电子邮件。这是一个例子:
$connection = imap_open($host, $uname, $pwd) or die(imap_last_error());
$messages = imap_search($connection, 'ALL');
foreach($messages as $msg) {
$header = imap_headerinfo($connection, $msg);
foreach($header->to as $receiver){
echo $receiver->mailbox.'@'.$receiver->host.'<br/>';
}
}
希望对您有所帮助!