问题描述
我正在使用IMAP来检索gmail消息,实际上,当我尝试获取电子邮件正文时,该消息是在连续1小时的时间间隔后从数据记录器发出的,它显示的是已编码的正文,例如 VVNSOlNpdGUwMV82LDAsNDksVHJ5Q291bnQ9MSxGVFBTdWNjZXNzPS0x原始文本为 USR:Site01_6,0,49,TryCount = 1,FTPSuccess = -1,
I am using IMAP to retrieve the gmail message , actually the messages are coming from a data-logger after a continuous time of 1 hour interval when i am trying to fetching the email body it is showing the encoded body like "VVNSOlNpdGUwMV82LDAsNDksVHJ5Q291bnQ9MSxGVFBTdWNjZXNzPS0x " and the orignal text is "USR:Site01_6,0,49,TryCount=1,FTPSuccess=-1",
如果我手动复制文本并从
if i copy the text manualy and sent an email from my other email then i can fetching this as same as orignal , can't understanding where is the problem ,, here is the code i am using for this ,
public function fetch_gmail_inbox()
{
$res=array();
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = '[email protected]';
$password = 'Solarisgr8';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'UNSEEN');
/* if emails are returned, cycle through each... */
if($emails) {
/* put the newest emails on top */
rsort($emails);
/* for every email... */
foreach($emails as $email_number) {
/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
$message =imap_fetchbody($inbox,$email_number,1);
$structure = imap_fetchstructure($inbox,$email_number);
if($structure->parts[0]->encoding == 3 ||$structure->encoding == 3 )
{
$message=imap_base64($message);
}
if($structure->parts[0]->encoding == 4 ||$structure->encoding == 4)
{
$message = imap_qprint($message);
}
$message2= quoted_printable_decode(imap_fetchbody($inbox,$email_number,0));
$date=explode(':',$message2);
$date2= date('d-m-Y h:i:s',strtotime($date[8].':00:00'));
$sub=$string = preg_replace('/\s+/', '', $overview[0]->subject);
$tomatch=preg_replace('/\s+/', '', "USR:Site01_Comms Complete");
if(strcmp($sub,$tomatch)==0)
{
$res['date']=$date2;
$res['body']=$message;
}
}
return $res;
}
/* close the connection */
imap_close($inbox);
}
任何帮助都会感激
推荐答案
尝试使用
function fetch_gmail_inbox($check='UNSEEN')
{
$res=array();
/* connect to gmail */
$hostname = '{mail.enlighten-energy.net:143/imap/novalidate-cert}INBOX';
$username = Yii::app()->getModule('user')->get_config('datalogger_email');
$password = Yii::app()->getModule('user')->get_config('datalogger_email_pwd');
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,$check);
/* if emails are returned, cycle through each... */
if($emails) {
/* put the newest emails on top */
rsort($emails);
/* for every email... */
$data3=array();
foreach($emails as $email_number)
{
//print_r($email_number); echo "#####";
/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
//echo "<pre>";print_r($overview);
$message = quoted_printable_decode(imap_fetchbody($inbox,$email_number,1));
$structure = imap_fetchstructure($inbox,$email_number);
$attachments = array();
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']);
}
}
}
}
$data2=array();
foreach($attachments as $attachment)
{
if($attachment['is_attachment'] == 1)
{
$filename = $attachment['name'];
if(empty($filename)) $filename = $attachment['filename'];
if(empty($filename)) $filename = time() . ".dat";
$filename=str_replace('/','',$filename);
$filename="uploads/gmail_attachment/".$email_number . "-" . $filename;
$fp = fopen($filename, "w+");
if(fwrite($fp, $attachment['attachment']))
{
$data=self::fetch_xls_data($email_number,$filename);
}
fclose($fp);
}
$data2[$email_number]=$data;
unlink($filename);
}
$data3[$email_number]=$data2[$email_number];
}
return $data3;
}
/* close the connection */
imap_close($inbox);
//return $data;
}
这篇关于如何使用php和imap获取gmail邮件正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!