php Mailgun 发送带有附件的邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14229656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mailgun Sent mail With attachment
提问by TechCare99
I am facing issue when mail having attachment sent using mailgun. If anyone has done this thing please reply. This is my code...
我在使用 mailgun 发送带有附件的邮件时遇到问题。如果有人做过这件事,请回复。这是我的代码...
$mg_api = 'key-3ax6xnjp29jd6fds4gc373sgvjxteol0';
$mg_version = 'api.mailgun.net/v2/';
$mg_domain = "samples.mailgun.org";
$mg_from_email = "[email protected]";
$mg_reply_to_email = "[email protected]";
$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 <' . '[email protected]' . '>',
'to' => '[email protected]',
'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);
(I have used my mailgun settings)
(我已经使用了我的 mailgun 设置)
I receive the email without the attachment. If I use the URL path it displays the URL instead of the attachment.
我收到没有附件的电子邮件。如果我使用 URL 路径,它会显示 URL 而不是附件。
采纳答案by user1964269
You need to change the last parameter in the following way:
attachment[1]' => '@aaa.rar
您需要通过以下方式更改最后一个参数:
attachment[1]' => '@aaa.rar
We have some samples in our documentation for this use case. Just click PHP in the top bar to switch the language. http://documentation.mailgun.net/user_manual.html#examples-sending-messages-via-http
我们的文档中有一些用于此用例的示例。只需单击顶部栏中的 PHP 即可切换语言。 http://documentation.mailgun.net/user_manual.html#examples-sending-messages-via-http
Please don't hesitate to send us an email with any questions to [email protected]. We are always happy to help.
如有任何问题,请随时向我们发送电子邮件至 [email protected]。我们总是乐于提供帮助。
回答by nzmuzzer
I realise this is old but I've just run into this problem and this page is pretty much the only info I can find on the net. So I hope this will assist someone else. After talking to MailGun support I've found the following works with the current API.
我意识到这是旧的,但我刚刚遇到了这个问题,这个页面几乎是我在网上能找到的唯一信息。所以我希望这会对其他人有所帮助。在与 MailGun 支持人员交谈后,我发现以下内容适用于当前的 API。
Cycles through an array of zero to n attachment files:
循环遍历零到 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);
回答by JeremyS
The documentation doesn't say it explicitly, but the attachment itself is bundled into the request as multipart/form-data.
文档没有明确说明,但附件本身作为 multipart/form-data 捆绑到请求中。
The best way to debug what's going on is use Fiddler to watch the request. Make sure you accept Fiddler's root certificate, or the request won't issue due to the SSL error.
调试正在发生的事情的最好方法是使用 Fiddler 来观察请求。确保您接受 Fiddler 的根证书,否则请求将不会因 SSL 错误而发出。
What you should see in Fiddler is for Headers:
您应该在 Fiddler 中看到的是标题:
Cookies / Login
Cookies / 登录
Authorization: Basic <snip>==
Entity
实体
Content-Type: multipart/form-data; boundary=<num>
And for TextView:
对于 TextView:
Content-Disposition: form-data; name="attachment"
@Test.pdf
Content-Disposition: form-data; name="attachment"; filename="Test.pdf"
Content-Type: application/pdf
<data>
Note that you POST the field 'attachment=@<filename>'. For form-data, the field name is also 'attachment', then has 'filename=<filename>' (without the '@') and finally the file contents.
请注意,您 POST 字段 'attachment=@<filename>'。对于表单数据,字段名称也是 'attachment',然后是 'filename=<filename>'(没有 '@'),最后是文件内容。
I think CURL is supposed to just do this all for you magically based on using the '@' syntax and specifying a path to a file on your local machine. But without knowing the magic behavior it's hard to grok what's really happening.
我认为 CURL 应该根据使用“@”语法并指定本地计算机上文件的路径神奇地为您完成这一切。但是如果不知道魔法行为就很难理解到底发生了什么。
For example, in C# it looks like this:
例如,在 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);
}
回答by Dan
I got stuck on this issue for a while and the answers here helped me, but there is something I came across which is not mentioned yet.
我在这个问题上停留了一段时间,这里的答案对我有所帮助,但是我遇到了一些尚未提及的问题。
I had been sending my POST parameters with a blank/NULL 'cc' value, such as: $post_data['cc'] = NULL;. This did not prevent me from sending text emails (no attachment), but it did cause problems when sending an email with an attachment. Removing the blank ccfrom my array resolved part of the issue.
我一直在发送带有空白/NULL 'cc' 值的 POST 参数,例如:$post_data['cc'] = NULL;. 这并没有阻止我发送文本电子邮件(无附件),但是在发送带有附件的电子邮件时确实会导致问题。cc从我的阵列中删除空白解决了部分问题。
Additionally I had been using http_build_querybefore posting my data via PHP curl and that prevented my email with attachment from being sent successfully.
此外,我在http_build_query通过 PHP curl 发布我的数据之前一直在使用,这阻止了我的带有附件的电子邮件被成功发送。
Removing the empty ccand http_build_queryresolved this for me. Maybe an uncommon case, but posting in case it is helpful for someone who has the same issue.
删除空的cc并http_build_query为我解决了这个问题。也许是一个不常见的案例,但发布以防万一它对遇到相同问题的人有所帮助。
回答by Evan Winograd
This worked for me...
这对我有用...
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 <[email protected]>',
'to' => 'To <[email protected]>',
'subject' => 'Your Subject',
'html' => '<h1>Hooray!</h1>',
);
$file_array = array('/var/www/scripts/test.csv');
echo json_encode($Email::send($array, $file_array));
回答by Dinesh Gurjar
This is working fine for me.
这对我来说很好用。
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' => '<[email protected]>',
'to' => '<[email protected]>',
'subject' => 'csv file',
'text' => 'csv test file'
),array('attachment' => [
['remoteName'=>'student.csv', 'filePath'=>'/form/stu.csv']
]) );
回答by Dilip Kheni
Add attachment file:
添加附件文件:
"attachment[1]" = "@$_FILES['careerDocument']['tmp_name'];filename=test.jpg".
($contentType ? ';type=' . $contentType : '' ) ;
curl_setopt($ch, CURLOPT_POSTFIELDS, ($message));
回答by simplecoder
Thsi worked for me:
Thsi为我工作:
<?php
$filePath='@Wealth_AC_AMF.pdf';
$curl_post_data=array(
'from' => 'Excited User <[email protected]>',
'to' => '[email protected]',
'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);
?>

