我正在开发一个网站,用户可以在其中邮寄票证并将任何类型的文件附加到特定的邮件 ID。我需要将邮件主题、内容和附件添加到数据库中。我正在使用 cron 来执行此操作。除了附件之外,一切都很完美。我看过一些创建下载链接的帖子。由于我使用的是 cron,因此无法手动执行。
$hostname = '{xxxx.net:143/novalidate-cert}INBOX';
$username = '[email protected]';
$password = 'zzzz';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to : ' . imap_last_error());
$emails = imap_search($inbox,'ALL');
if($emails) {
$output = '';
rsort($emails);
foreach($emails as $email_number) {
$structure = imap_fetchstructure($inbox, $email_number);
$name = $structure->parts[1]->dparameters[0]->value; // name of the file
$type = $structure->parts[1]->type; //type of the file
}}
我能够获取文件的类型和名称,但不知道如何进一步进行
任何人请帮助我。谢谢...
要将附件另存为文件,您需要解析邮件的结构并取出附件本身的所有部分(内容处置)。您应该将其包装到自己的类中,以便您可以轻松访问并且随着时间的推移可以更轻松地处理错误,电子邮件解析可能很脆弱:
$savedir = __DIR__ . '/imap-dump/';
$inbox = new IMAPMailbox($hostname, $username, $password);
$emails = $inbox->search('ALL');
if ($emails) {
rsort($emails);
foreach ($emails as $email) {
foreach ($email->getAttachments() as $attachment) {
$savepath = $savedir . $attachment->getFilename();
file_put_contents($savepath, $attachment);
}
}
}
这些类的代码或多或少地包装了imap_...
函数,但对于附件类,它也会进行结构解析。你可以在github上找到代码。希望这有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)