[英]Send mail with attachment by Mailgun API using Python's httpx library
我正在尝试使用 Mailgun API 发送带有附件的电子邮件。 我可以使用 curl 轻松实现这一点:
curl -V -s --user 'XXX' \
https://api.eu.mailgun.net/v3/XXX/messages \
-F from='Excited User <YOU@YOUR_DOMAIN_NAME>' \
-F to='XXXX' \
-F subject='Hello' \
-F text='Testing some Mailgun awesomness!' \
--form-string html='<html>HTML version of the body</html>' \
-F attachment=@files/example.csv
同样使用 python 的请求库。 但是当我尝试使用 httpx 库做同样的事情时:
build_request_kwargs: dict = {
"url": "/messages",
"method": "POST",
"data": {
"template": "template_name",
"from": from_,
"to": to,
"subject": subject,
"h:X-Mailgun-Variables": json.dumps(template_data),
},
"files": [("file", ("file", io.BytesIO(b"<file content>")))]
}
client = httpx.AsyncClient(base_url=base_url, auth=("api", api_key))
async with client:
request = client.build_request(**build_request_kwargs)
response = await client.send(request)
response.raise_for_status()
电子邮件已发送,但没有任何附件。 我试图将每个文件编码设置为application/octet-stream
但它也不起作用。 我不确定 curl 或 requests multipart/form-data
request 和 httpx 之间有什么区别。
我刚刚意识到我需要将每个文件发布为“附件”表单名称。 这个例子应该有效:
build_request_kwargs: dict = {
"url": "/messages",
"method": "POST",
"data": {
"template": "template_name",
"from": from_,
"to": to,
"subject": subject,
"h:X-Mailgun-Variables": json.dumps(template_data),
},
"files": [("attachment", ("file.txt", io.BytesIO(b"<file content>")))]
}
client = httpx.AsyncClient(base_url=base_url, auth=("api", api_key))
async with client:
request = client.build_request(**build_request_kwargs)
response = await client.send(request)
response.raise_for_status()
这对我有用。 我已经在附件数组中传递了附件文件 URL。 现在我可以在电子邮件中发送多个附件。
$attachment = [ 0 => 'https://www.crm.truerater.com/public/assets/client_upload_images/1634327873.png', 1 => 'https://www.crm.truerater.com/public/assets/client_upload_images/1634327873.png' ];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/truerater.com/messages');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$post = array(
'from' => $mailfromname .'<'.$mailfrom.'>',
'to' => $toname.'<'.$to.'>',
'cc' => '',
'bcc' => '',
'subject' => $subject,
'html'=>$html,
'text'=>$text,
'o:tracking'=>'yes',
'o:tracking-clicks'=>'yes',
'o:tracking-opens'=>'yes',
'o:tag'=>$tag,
'h:Reply-To'=>$replyto,
);
if(sizeOf($attachment) > 0){
$i=0;
foreach ($attachment as $attach){
$attachPath = substr($attach, strrpos($attach, '/public/') + 1);
$post['attachment['.$i.']'] = curl_file_create($attach, '', substr($attachPath, strrpos($attachPath, '/') + 1));
$i=$i+1;
}
}
$headers_arr = array("Content-Type:multipart/form-data");
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_USERPWD, 'api' . ':' . $key);
curl_setopt($ch, CURLOPT_HEADER, $headers_arr);
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if (curl_errno($ch)) {
$result = curl_error($ch);
\Log::info($result);
}
else{
$result = json_decode($result,true);
}
curl_close($ch);
\Log::info($result);
return $result;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.