【发布时间】:2013-02-08 01:51:27
【问题描述】:
当我在 php 中使用 imap 获取电子邮件时,我得到了电子邮件正文内容,但我无法提取粘贴且未附加在电子邮件正文中的内联图像。
【问题讨论】:
-
您应该提供用于获取电子邮件的相应代码(不包括用户名和密码)。否则,无法为您提供帮助,因为不知道您使用什么功能/库。
当我在 php 中使用 imap 获取电子邮件时,我得到了电子邮件正文内容,但我无法提取粘贴且未附加在电子邮件正文中的内联图像。
【问题讨论】:
在电子邮件内容的正文中搜索图像。
假设您的电子邮件正文是$body,那么您可以使用 preg_match 获取图像 url。
使用此 preg_match 表达式获取图像源。
preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $body, $matches);
【讨论】:
嘿,我想我有办法了
<?php
$hostname = '{myserver/pop3/novalidate-cert}INBOX';
$username = 'username';
$password = 'password';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Tiriyo: ' . imap_last_error());
$msgno = 0; //message id
$no_of_occurences = 0;
$intStatic = 2;//to initialize the mail body section
$decode = imap_fetchbody($mbox, $msgno , "");
$no_of_occurences = substr_count($decode,"Content-Transfer-Encoding: base64");//to get the no of images
if($no_of_occurences > 0){
for($i = 0; $i < $no_of_occurences; $i++){
$strChange = strval($intStatic+$i);
$decode = imap_fetchbody($mbox, $msgno , $strChange);//to get the base64 encoded string for the image
$data = base64_decode($decode);
$fName = time()."_".$strChange . '.gif';
$file = $fName;
$success = file_put_contents($file, $data); //creates the physical image
}
}
imap_close($inbox);
?>
【讨论】: