从Mailgun表单PHP中检索附件

时间:2014-07-07 07:39:07

标签: php forms post attachment mailgun

如何通过Mailgun POST的表单帖子检索并保存发送给我的附件

以下一些参数

attachment-1    
{:filename=>"crabby.gif", :type=>"image/gif", :name=>"attachment-1", :tempfile=>#<Tempfile:/tmp/RackMultipart20140707-2-slsrkh>, :head=>"Content-Disposition: form-data; name=\"attachment-1\"; filename=\"crabby.gif\"\r\nContent-Type: image/gif\r\nContent-Length: 2785\r\n"}

attachment-2    
{:filename=>"attached_файл.txt", :type=>"text/plain", :name=>"attachment-2", :tempfile=>#<Tempfile:/tmp/RackMultipart20140707-2-sudxuf>, :head=>"Content-Disposition: form-data; name=\"attachment-2\"; filename=\"attached_файл.txt\"\r\nContent-Type: text/plain\r\nContent-Length: 32\r\n"}

Content-Type    
multipart/mixed; boundary="------------020601070403020003080006"

2 个答案:

答案 0 :(得分:8)

所以我知道这是迟了一年,但我遇到了同样的问题,并想出了如何下载附件。帖子中的文件存储在环境变量$_FILES中。每个文件的信息如下所示:

Array
(
    [attachment-1] => Array
        (
            [name] => ATextFile.txt
            [type] => text/plain
            [tmp_name] => /tmp/php8zhmlU
            [error] => 0
            [size] => 70144
        )
)

文件的路径存储在tmp_name中,因此在这种情况下,/tmp/php8zhmlU是文件的完整路径。 move_uploaded_file将覆盖任何现有文件!要从POST下载所有附件,我写了一个函数:

function download_attachments($pathToDownloadDirectory)
{
    foreach($_FILES as $file)
    {
        if($file['error'] == "0")
        {
            if(!(move_uploaded_file($file['tmp_name'], $pathToDownloadDirectory . $file['name'])))
            {
                return 0;
            }
        }
        else
        {
            return 0;
        }    
    }
    return 1;
}

download_attachments("/Full/Path/To/Some/Dir/");

此顶部的文档可以是found here.

答案 1 :(得分:4)

  1. 使用路线会话中的商店操作以获取信息 关于你的电子邮件(retrieving stored message
  2. json_decode&#34;附件&#34;财产以获取信息 关于你的附件
  3. 获取api密钥并使用php库以使用get api。
  4. 如果你的json解码附件列表是$ files,你可以做

    $mgClient = new Mailgun('yourApiKey');
    
    foreach ($files as $file){
        file_put_contents($file->name,$mgClient->get($file->url)->http_response_body);    
    }
    

    下载每个附件