首页 文章

Mailgun已发送邮件附件

提问于
浏览
10

当邮件有使用mailgun发送的附件时,我正面临问题 . 如果有人做过这件事,请回复 . 这是我的代码......

$mg_api = 'key-3ax6xnjp29jd6fds4gc373sgvjxteol0';
$mg_version = 'api.mailgun.net/v2/';
$mg_domain = "samples.mailgun.org";
$mg_from_email = "info@samples.com";
$mg_reply_to_email = "info@samples.org";

$mg_message_url = "https://".$mg_version.$mg_domain."/messages";


$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

curl_setopt ($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_VERBOSE, 0);
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($ch, CURLOPT_USERPWD, 'api:' . $mg_api);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_POST, true); 
//curl_setopt($curl, CURLOPT_POSTFIELDS, $params); 
curl_setopt($ch, CURLOPT_HEADER, false); 

//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, $mg_message_url);
curl_setopt($ch, CURLOPT_POSTFIELDS,                
        array(  'from'      => 'aaaa <' . 'aaa@aaa.com' . '>',
                'to'        => 'test.client91@gmail.com',
                'h:Reply-To'=>  ' <' . $mg_reply_to_email . '>',
                'subject'   => 'aaaaa'.time(),
                'html'      => 'aaaaaa',
                'attachment'[1] => 'aaa.rar'
            ));
$result = curl_exec($ch);
curl_close($ch);
$res = json_decode($result,TRUE);
print_r($res);

(我使用过我的mailgun设置)

我收到没有附件的电子邮件 . 如果我使用URL路径,则显示URL而不是附件 .

8 回答

  • 1

    您需要以下列方式更改最后一个参数: attachment[1]' => '@aaa.rar

    我们在这个用例的文档中有一些示例 . 只需单击顶部栏中的PHP即可切换语言 . http://documentation.mailgun.net/user_manual.html#examples-sending-messages-via-http

    如有任何问题,请随时向我们发送电子邮件至support@mailgunhq.com . 我们总是很乐意提供帮助 .

  • 0

    我意识到这已经老了,但我刚遇到这个问题,这个页面几乎是我能在网上找到的唯一信息 . 所以我希望这会帮助别人 . 在与MailGun支持人员交谈之后,我发现以下适用于当前的API .

    循环遍历零到n个附件文件的数组:

    $attachmentsArray = array('file1.txt', 'file2.txt');
        $x = 1;
        foreach( $attachmentsArray as $att )
        {
            $msgArray["attachment[$x]"] = curl_file_create( $att );
            $x ++;
        }
    
        // relevant cURL parameter, $msgArray also contains to, from, subject  parameters etc.
        curl_setopt($ch, CURLOPT_POSTFIELDS,$msgArray);
    
  • 0

    文档没有明确说明,但附件本身作为multipart / form-data捆绑到请求中 .

    调试正在进行的操作的最佳方法是使用Fiddler来查看请求 . 确保您接受Fiddler的根证书,否则由于SSL错误,请求将不会发出 .

    你应该在Fiddler看到的是Headers:

    Cookies /登录

    Authorization: Basic <snip>==
    

    实体

    Content-Type: multipart/form-data; boundary=<num>
    

    对于TextView:

    Content-Disposition: form-data; name="attachment"
    @Test.pdf
    
    Content-Disposition: form-data; name="attachment"; filename="Test.pdf"
    Content-Type: application/pdf
    <data>
    

    请注意,您发布字段'attachment = @ <filename>' . 对于表单数据,字段名称也是“附件”,然后是“filename = <filename>”(不带“@”),最后是文件内容 .

    我认为CURL应该基于使用'@'语法并指定本地机器上文件的路径来神奇地为你做这一切 . 但是,如果不了解魔法行为,就很难理解真正发生的事情 .

    例如,在C#中它看起来像这样:

    public static void SendMail(MailMessage message) {
        RestClient client = new RestClient();
        client.BaseUrl = apiUrl;
        client.Authenticator = new HttpBasicAuthenticator("api", apiKey);
    
        RestRequest request = new RestRequest();
        request.AddParameter("domain", domain, ParameterType.UrlSegment);
        request.Resource = "{domain}/messages";
        request.AddParameter("from", message.From.ToString());
        request.AddParameter("to", message.To[0].Address);
        request.AddParameter("subject", message.Subject);
        request.AddParameter("html", message.Body);
    
        foreach (Attachment attach in message.Attachments)
        {
            request.AddParameter("attachment", "@" + attach.Name);
            request.AddFile("attachment",
                attach.ContentStream.WriteTo,
                attach.Name,
                attach.ContentType.MediaType);
        }
    
        ...
        request.Method = Method.POST;
        IRestResponse response = client.Execute(request);
    }
    
  • 0

    我在这个问题上停留了一段时间,这里的答案对我有帮助,但是我遇到的东西还没有提到 .

    我一直在发送带有空白/ NULL 'cc'值的POST参数,例如: $post_data['cc'] = NULL; . 这并不妨碍我发送文本电子邮件(没有附件),但在发送带附件的电子邮件时确实会出现问题 . 从我的阵列中删除空白 cc 解决了部分问题 .

    另外,在通过PHP curl发布我的数据之前,我一直在使用 http_build_query ,这阻止了我的电子邮件附件被成功发送 .

    删除空 cchttp_build_query 为我解决了这个问题 . 也许是一个不常见的案例,但如果对有相同问题的人有帮助,则张贴 .

  • 7

    添加附件文件:

    "attachment[1]" = "@$_FILES['careerDocument']['tmp_name'];filename=test.jpg".
    ($contentType ? ';type=' . $contentType : '' ) ;
    curl_setopt($ch, CURLOPT_POSTFIELDS, ($message));
    
  • 0

    Thsi为我工作:

    <?php
    
    $filePath='@Wealth_AC_AMF.pdf';
    
    $curl_post_data=array(
        'from'    => 'Excited User <noreply@test.com>',
        'to'      => 'test@gmail.com',
        'subject' => 'Hello',
        'text'    => 'test',
    'attachment[1]' => $filePath
    );
    
    $service_url = 'https://api.mailgun.net/v3/test.com/messages';
    $curl = curl_init($service_url);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_USERPWD, "api:key-test"); 
    
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
    
    
    $curl_response = curl_exec($curl);  
    $response = json_decode($curl_response);
    curl_close($curl);
    
    var_dump($response);
    
    
    
     ?>
    
  • 3

    这对我有用......

    class Email {
    
        function send($array, $file_array) {
            $mgClient = new Mailgun('YOUR_MAILGUN_API_KEY');
            $domain = "mg.YOUR_DOMAIN.com";
            $result = $mgClient->sendMessage($domain, $array, array('attachment' => $file_array));
            return $result;
        }   
    }
    
    $array = array(
        'from'    => 'From <from@from.com>',
        'to'      => 'To <to@to.com>',
        'subject' => 'Your Subject',
        'html'    => '<h1>Hooray!</h1>',
    );
    
    $file_array = array('/var/www/scripts/test.csv');
    echo json_encode($Email::send($array, $file_array));
    
  • 5

    这对我来说很好 .

    require '../../vendor/autoload.php';
    use Mailgun\Mailgun;
    
    # Instantiate the client.
    $mgClient = new Mailgun('key-b0e955');
    $domain = "domain.com";
    
    # Make the call to the client.
    $result = $mgClient->sendMessage("$domain",
      array('from'    => '<postmaster@domain.com>',
            'to'      => '<test@gmail.com>',
            'subject' => 'csv file',
            'text' => 'csv test file'
           ),array('attachment' => [
        ['remoteName'=>'student.csv', 'filePath'=>'/form/stu.csv']
      ]) );
    

相关问题