gpt4 book ai didi

php - 使用 PHP IMAP 打开电子邮件

转载 作者:行者123 更新时间:2023-11-30 23:34:35 24 4
gpt4 key购买 nike

w3schools 论坛的一位用户帮助我编写了一些代码,这些代码使用 IMAP 功能检查我在私有(private)服务器上的邮件收件箱并使用它做我喜欢的事情,我创建了自己的一组功能来将电子邮件内容发布到一个 MySQL 表。

谁能帮我找到一个解决方案,如何打开电子邮件收件箱,检查收件箱中的电子邮件(那里只有一封,因为以前的电子邮件将被自动删除。定义打开的电子邮件$open_email_msg 形式的消息允许我启动用于将电子邮件发布到 MySQL 表的命令集,然后删除电子邮件并关闭收件箱吗?

这是帮助我的人的代码:

<?php

$now = time(); // current time

$mailbox = '{192.168.150.11:143/imap/novalidate-cert}'; // see http://www.php.net/manual/en/function.imap-open.php
$mbox = imap_open($mailbox, 'username', 'password'); // log in to mail server

if (!$mbox)
echo ('Failed opening mailbox<br>' . print_r(imap_errors(), true)); // remove the print_r for production use
else
{
$box = imap_check($mbox); // get the inbox

for ($imap_idx = 1; $imap_idx <= $box->Nmsgs; $imap_idx++) // loop through the messages
{
$headers = imap_headerinfo($mbox, $imap_idx); // http://www.php.net/manual/en/function.imap-headerinfo.php
$raw_headers = imap_fetchheader($mbox, $imap_idx); // http://www.php.net/manual/en/function.imap-fetchheader.php
$selected_headers = '';
$text_part = '';
$html_part = '';
$original_message = imap_body($mbox, $imap_idx); // save the copy of the entire thing, attachments and all

// build selected headers string
for ($ii = 0; $ii < count($headers->from); $ii++)
$selected_headers .= 'From: ' . $headers->from[$ii]->mailbox . '@' . $headers->from[$ii]->host . "\n";
for ($ii = 0; $ii < count($headers->to); $ii++)
$selected_headers .= 'To: ' . $headers->to[$ii]->mailbox . '@' . $headers->to[$ii]->host . "\n";
for ($ii = 0; $ii < count($headers->cc); $ii++)
$selected_headers .= 'Cc: ' . $headers->cc[$ii]->mailbox . '@' . $headers->cc[$ii]->host . "\n";
for ($ii = 0; $ii < count($headers->bcc); $ii++)
$selected_headers .= 'Bcc: ' . $headers->bcc[$ii]->mailbox . '@' . $headers->bcc[$ii]->host . "\n";
if (!empty($headers->date))
$selected_headers .= 'Date: ' . $headers->date . "\n";
if (!empty($headers->subject))
$selected_headers .= 'Subject: ' . $headers->subject . "\n";



// see below; getMsg uses global variables
getMsg($mbox, $imap_idx);

$text_part = $plainmsg; // text portion of the email
$html_part = $htmlmsg; // html portion of the email

// check for text portion first
$msg_text = trim(strip_tags($plainmsg

最佳答案

试试这个代码来阅读电子邮件。

$username="yourusername@yourmailhost.com";
$password="yourPassword123!";
$hostname="{imap.hostinger.com:993/imap/ssl}INBOX";

$imap=imap_open($hostname,$username,$password) or die('Cannot connect: '.imap_last_error());
$message_count = imap_num_msg($imap);
echo "<b>$message_count messages</b><br>";

for ($i = 1; $i <= $message_count; ++$i){
$header = imap_header($imap, $i);
$body = imap_fetchbody($imap, $i, '2');
$prettydate = date("jS F Y", $header->udate);

if(isset($header->from[0]->personal)){
$personal = $header->from[0]->personal;
}else{
$personal = $header->from[0]->mailbox;
}

$subject=$header->Subject;
$email = "$personal <{$header->from[0]->mailbox}@{$header->from[0]->host}>";
echo "On $prettydate, $email said \"$body\".\n";
echo '<br><br>';
}
print_r(imap_errors());
imap_close($imap);

关于php - 使用 PHP IMAP 打开电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8672055/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com