希望你很好,
我的脚本有问题,如何从服务器获取电子邮件。
if ($emailObj->isMultipart()) {
$zendObj = new RecursiveIteratorIterator($emailObj);
try {
foreach ($zendObj as $part) {
try {
if (strtok($part->contentType, ';') == 'text/plain') {
$charset = '';
try {
$charset = $part->getHeaderField('contentType', 'charset');
} catch (Exception $excCS) {
$charset = '';
}
$bodyTXT = trim($part);
//if ($part->contentTransferEncoding == 'base64') $bodyTXT = base64_decode($bodyTXT);
if (base64_decode($bodyTXT, true)) $bodyTXT = base64_decode($bodyTXT);
if (strpos($part->contentType, 'utf-8') === FALSE) $bodyTXT = utf8_encode($bodyTXT);
if ($part->getHeader('contentTransferEncoding') == 'quoted-printable') $BODY = quoted_printable_decode($bodyTXT);
$BODY = $bodyTXT;
if ($charset != '' && $charset != 'utf-8') $BODY = mb_convert_encoding($BODY, 'UTF-8', $charset);
} elseif (trim(strtok($part->contentType, ';')) == 'text/html') {
$charset = '';
try {
$charset = $part->getHeaderField('contentType', 'charset');
} catch (Exception $excCS) {
$charset = '';
}
$bodyHTML = trim($part);
try {
$transferEncoding = $part->getHeader('contentTransferEncoding');
} catch (Exception $exCTE) {
$transferEncoding = false;
}
if ($transferEncoding == 'quoted-printable') $BODY = quoted_printable_decode($bodyHTML);
elseif ($transferEncoding == 'base64') $BODY = base64_decode($bodyHTML);
else $BODY = $bodyHTML;
if (strpos($part->contentType, 'utf-8') === FALSE) $bodyHTML = utf8_encode($bodyHTML);
if ($charset != '' && $charset != 'utf-8') $BODY = mb_convert_encoding($BODY, 'UTF-8', $charset);
} else {
$cTypes[] = $part->contentType;
$begin = strpos($part->contentType, 'name="');
$end = strpos($part->contentType, '"', $begin + 7);
$disposition = false;
try {
$disposition = explode(';', $part->contentDisposition);
} catch (Exception $excCD) {
$disposition = false;
}
$ctype = preg_split('/( |;)/', $part->contentType);
if ($disposition && count($disposition) > 1) {
$disposition[1] = trim($disposition[1]);
$rawFileName = substr($disposition[1], strpos($disposition[1], 'filename=') + 9);
$rawFileName = str_replace('"', '', str_replace('/', '_', $rawFileName));
} else {
$rawFileName = substr(end($ctype), strpos(end($ctype), 'name=') + 5);
$rawFileName = str_replace('"', '', str_replace('/', '_', $rawFileName));
}
if (preg_match("/=\?/", $rawFileName)) $rawFileName = iconv_mime_decode($rawFileName, 0, 'UTF-8');
$thisD = '';
try {
$thisD = $part->contentId;
$thisD = str_replace('<', '', str_replace('>', '', $thisD));
} catch (Exception $exContId) {
$thisD = '';
}
$fileNameAttach = urlencode(substr($part->contentType, $begin + 6, -1));
$attachment = base64_decode($part->getContent());
$fileNameAttachRandName = "att_" . $this->session->salesDatabaseConfig . "_" . $rand . "_" . $this->stripAccents(trim($rawFileName));
$fileNameAttachAllRandName[] = $fileNameAttachRandName;
$fileNameAttachAll[] = array('name' => $rawFileName, 'randName' => $fileNameAttachRandName, 'cid' => ($thisD ? str_replace('<', '', str_replace('>', '', $thisD)) : ''));
$fh = fopen(str_replace("'", "\\'", FCPATH . 'images/attachemail/' . $this->session->salesDatabaseConfig . "/" . $fileNameAttachRandName), 'w');
fwrite($fh, $attachment);
fclose($fh);
system("chmod 777 " . escapeshellarg(FCPATH . 'images/attachemail/' . $this->session->salesDatabaseConfig . "/" . $fileNameAttachRandName));
}
} catch (Zend_Mail_Exception $e) {
// ignore
print "ZEND MAIL EXCEPTION : " . $e . " \n";
}
}
} catch (Exception $exc) {
print $exc->getMessage() . PHP_EOL;
continue;
}
}
并且如果我的附件文件带有“é”,则catch会出现错误:检测到无效的头值。
这是电子邮件标题:
-0000000000001c308005a831cc78----0000000000001c308205a831cc7a内容类型:application / pdf; name =“ =?UTF-8?Q?Plan_de_d = C3 = A9partements = 2Epdf?=”内容处置:附件; filename =“ =?UTF-8?Q?Plan_de_d = C3 = A9partements = 2Epdf?=”内容传输编码:base64内容ID:X附件ID:f_kbhuaq9a0
正如您提到的那样,原始PHP脚本的唯一问题是它不接受法语字符,您可以尝试将unicode放入PHP。该站点可能有效:https://www.sitepoint.com/bringing-unicode-to-php-with-portable-utf8/#:~:text=PHP%20allows%20multibyte%20variable%20names,that%20PHP%20lacks%20Unicode%20support。
编辑:因此,为了澄清我的意思,您可以使用名为“便携式UTF-8”的库在php脚本中启用Unicode处理,因为它提供了诸如以下方法:
$title = utf8_clean($_POST['title']);
从UTF-8编码的字符串中删除无效的字节序列。有关更多信息,您可以查看文档:http://pageconfig.com/post/portable-utf8