我正在尝试使用此代码,但我不明白我们如何使用发件人的电子邮件获取附件。这是我的代码,我用不同的代码尝试了很多次,但我没有得到任何解决方案。
public function myresume(&$response)
{
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'My gmail username';
$password = 'password';
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox,'ALL');
if($emails)
{
$output = '';
rsort($emails);
foreach($emails as $email_number) {
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,2);
$output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
$output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
$output.= '<span class="from">'.$overview[0]->from.'</span>';
$output.= '<span class="date">on '.$overview[0]->date.'</span>';
$output.= '</div>';
$output.= '<div class="body">'.$message.'</div>';
}
echo $output ;
}
imap_close($inbox);
}
答案 0 :(得分:0)
尝试这种方式来获取看不见的电子邮件并存储在任何文件夹中
$server = '{imap.gmail.com:993/imap/ssl}INBOX';
$login = 'username';
$password = 'password';
/* try to connect */
$connection = imap_open($server,$login,$password) or die('Cannot connect to your sever: ' . imap_last_error());
/*get only unseen mail from inbox*/
$result = imap_search($connection, 'UNSEEN');
$max_emails = 1;
/* if any emails found, iterate through each email */
if($emails) {
$count = 1;
/* put the newest emails on top */
rsort($emails);
$attachments = array();
/* for every email... */
foreach($emails as $email_number)
{
/* get mail structure */
$structure = imap_fetchstructure($inbox, $email_number);
if(isset($structure->parts) && count($structure->parts))
{
for($i = 0; $i < count($structure->parts); $i++)
{
$attachments[$i] = array(
'is_attachment' => false,
'filename' => '',
'name' => '',
'attachment' => ''
);
if($structure->parts[$i]->ifdparameters)
{
foreach($structure->parts[$i]->dparameters as $object)
{
if(strtolower($object->attribute) == 'filename')
{
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['filename'] = $object->value;
}
}
}
if($structure->parts[$i]->ifparameters)
{
foreach($structure->parts[$i]->parameters as $object)
{
if(strtolower($object->attribute) == 'name')
{
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['name'] = $object->value;
}
}
}
if($attachments[$i]['is_attachment'])
{
$attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);
/* 4 = QUOTED-PRINTABLE encoding */
if($structure->parts[$i]->encoding == 3)
{
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
/* 3 = BASE64 encoding */
elseif($structure->parts[$i]->encoding == 4)
{
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
}
}
}
}
imap_close($inbox);
/*store mail attachments to locally*/
foreach($attachments as $attachment)
{
if($attachment['is_attachment'] == 1)
{
$filename = $attachment['name'];
if(empty($filename))
$filename = $attachment['filename'];
if(empty($filename))
$filename = time() . ".dat";
$fp = fopen($path." ".$email_number . "-" . $filename, "w+");
$csvFileName=$path." ".$email_number . "-" . $filename;
fwrite($fp, $attachment['attachment']);
fclose($fp);
$array=$fields=array();
$i=0;
}
}
}
答案 1 :(得分:0)
您可以使用imap_fetchstructure来了解邮件结构并获取附件。以下是从http://www.php.net/manual/en/function.imap-fetchstructure.php复制的示例 (用于解析和解码所有类型的消息的代码,包括附件。)
<?php
function getmsg ($mbox, $mid)
{
// input $mbox = IMAP stream, $mid = message id
// output all the following:
global $charset, $htmlmsg, $plainmsg, $attachments;
$htmlmsg = $plainmsg = $charset = '';
$attachments = array();
// HEADER
$h = imap_header($mbox, $mid);
// add code here to get date, from, to, cc, subject...
// BODY
$s = imap_fetchstructure($mbox, $mid);
if (! $s->parts) // simple
getpart($mbox, $mid, $s, 0); // pass 0 as part-number
else { // multipart: cycle through each part
foreach ($s->parts as $partno0 => $p)
getpart($mbox, $mid, $p, $partno0 + 1);
}
}
function getpart ($mbox, $mid, $p, $partno)
{
// $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple
global $htmlmsg, $plainmsg, $charset, $attachments;
// DECODE DATA
$data = ($partno) ? imap_fetchbody($mbox, $mid, $partno) : // multipart
imap_body($mbox, $mid); // simple
// Any part may be encoded, even plain text messages,
// so check everything.
if ($p->encoding == 4)
$data = quoted_printable_decode($data);
elseif ($p->encoding == 3)
$data = base64_decode($data);
// PARAMETERS
// get all parameters, like charset, filenames of attachments, etc.
$params = array();
if ($p->parameters)
foreach ($p->parameters as $x)
$params[strtolower($x->attribute)] = $x->value;
if ($p->dparameters)
foreach ($p->dparameters as $x)
$params[strtolower($x->attribute)] = $x->value;
// ATTACHMENT
// Any part with a filename is an attachment,
// so an attached text file (type 0) is not mistaken as the message.
if ($params['filename'] || $params['name']) {
// filename may be given as 'Filename' or 'Name' or both
$filename = ($params['filename']) ? $params['filename'] : $params['name'];
// filename may be encoded, so see imap_mime_header_decode()
$attachments[$filename] = $data; // this is a problem if two files have
// same name
}
// TEXT
if ($p->type == 0 && $data) {
// Messages may be split in different parts because of inline attachments,
// so append parts together with blank row.
if (strtolower($p->subtype) == 'plain')
$plainmsg.
trim($data) . "\n\n";
else
$htmlmsg. =
$data . "<br><br>";
$charset = $params['charset']; // assume all parts are same charset
}
// EMBEDDED MESSAGE
// Many bounce notifications embed the original message as type 2,
// but AOL uses type 1 (multipart), which is not handled here.
// There are no PHP functions to parse embedded messages,
// so this just appends the raw source to the main message.
elseif ($p->type == 2 && $data) {
$plainmsg. =
$data . "\n\n";
}
// SUBPART RECURSION
if ($p->parts) {
foreach ($p->parts as $partno0 => $p2)
getpart($mbox, $mid, $p2, $partno . '.' . ($partno0 + 1)); // 1.2, 1.2.1, etc.
}
}
?>