email — Как получить тело сообщения gmail с помощью php и imap

Я использую IMAP для извлечения сообщения gmail, на самом деле сообщения поступают из регистратора данных через непрерывный интервал времени в 1 час, когда я пытаюсь извлечь тело письма, оно показывает закодированное тело, такое как «VVNSOlNpdGUwMV82LDAsNDksVHJ5Q291bnQ9MSxGVFBTdWNjxXN или orsps или текст «USR: Site01_6,0,49, TryCount = 1, FTPSuccess = -1»,

если я скопирую текст вручную и отправлю электронное письмо с другого моего письма, тогда я смогу получить его так же, как и оригинал, не могу понять, в чем проблема, вот код, который я использую для этого,

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);}

любая помощь будет оценена

0

Решение

попробуйте использовать это

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;

}
0

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]