[英]Opening e-mail with PHP IMAP
w3schools 論壇上的一位用戶幫助我編寫了一些有關使用 IMAP 函數檢查私人服務器上的郵件收件箱的代碼,並使用它做我喜歡的事情,我創建了自己的一組函數,用於將電子郵件內容發布到 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);
我相信這個鏈接會幫助你,因為我自己使用過這個鏈接,這對我來說很有效。
在那里你可以注冊和下載代碼,使用它很簡單。
或者如果你只想獲取 header 的信息,你可以做的是:
$mbox = imap_open("{xyz@abc.com:995/pop3/ssl/novalidate-cert}INBOX", 'abc@xyz.com', 'pass')
or die("can't connect: " . imap_last_error());
$MC = imap_check($mbox);
$result = imap_fetch_overview($mbox,"1:{$MC->Nmsgs}",0);
foreach ($result as $overview) {
echo "#{$overview->msgno} ({$overview->date}) - From: {$overview->from}
{$overview->subject}\n";
echo "<br>";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.