【问题标题】:Sending an image via an email attachment, which is stored in a string通过电子邮件附件发送图像,该附件存储在字符串中
【发布时间】:2011-08-04 22:39:06
【问题描述】:
如果我有一个存储了这个 text/html 的字符串:
Hello. This is a test article. <img src="http://hellotxt.com/image/jpFd.n.jpg" />
我希望将图像上传/保存在临时文件夹中,然后通过电子邮件作为附件发送。然后最好删除临时文件夹。这可能吗?
我知道如何发送带有附件的电子邮件(简单的部分),所以没问题。这只是将图像上传到文件夹并在字符串中查找图像。
【问题讨论】:
标签:
php
image
email
attachment
uploading
【解决方案1】:
好的,所以这基本上是一个两部分的问题:
- 如何从字符串中提取文件名/源
- 如何将上述文件上传到服务器
- 检查 preg_match 函数(如果同一字符串中有多个文件,请使用 preg_match_all)
$matches = array();
$numFound = preg_match( "/src[\s]?=[\s]?[\" | \'](^[\" | \'])*[\" | \']/", $yourInputString, $matches );
echo $matches[1]; //this will print out the source (the part in parens in the regex)
我对正则表达式不是很好,所以我提供的那个可能是错误的,但我认为它应该可以工作。
-
好的,现在上传部分......假设这是直接的 PHP(没有 HTML,表单可用),那么我认为你最好的选择是使用 cURL 并模仿表单提交。您将需要一个 PHP 脚本来接受上传的文件并将其移动到服务器上的某个位置(这应该会有所帮助)。实际的上传将像这样完成:
$data = array('file' => '@' . $fileSourceFromPart1); //the '@' is VITAL!
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'path/to/upload/script.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
希望这能起到作用,或者至少能让你朝着正确的方向前进!