我无法解决如何使用请求库从Python应用程序使用Mailgun api发送多个内联消息的问题.目前我有(使用jinja2作为模板和烧瓶作为web框架,托管在Heroku上):
def EmailFunction(UserEmail):
Sender = 'testing@test.co.uk'
Subject = 'Hello World'
Text = ''
name = re.sub('@.*','',UserEmail)
html = render_template('GenericEmail.html', name=name)
images = []
imageloc = os.path.join(dirname, 'static')
images.append(open(os.path.join(imageloc,'img1.jpg')))
images.append(open(os.path.join(imageloc,'img2.jpg')))
send_mail(UserEmail,Sender,Subject,Text,html,images)
return html
def send_mail(to_address, from_address, subject, plaintext, html, images):
r = requests.\
post("https://api.mailgun.net/v2/%s/messages" % app.config['MAILGUN_DOMAIN'],
auth=("api", app.config['MAILGUN_KEY']),
data={
"from": from_address,
"to": to_address,
"subject": subject,
"text": plaintext,
"html": html,
"inline": images
}
)
return r
Run Code Online (Sandbox Code Playgroud)
因此,电子邮件发送正常,但最后没有图像在电子邮件中.当我点击下载它们时,它们不显示.根据mailgun api在HTML中引用图像(当然简化了!);
<img src="cid:img1.jpg"/>
<img src="cid:img2.jpg"/>
etc ...
Run Code Online (Sandbox Code Playgroud)
显然我做错了,但是我尝试使用requests.files对象附加这些,它甚至没有发送电子邮件并且没有给出任何错误,因此我认为这根本不是正确的方法.
可悲的是,关于此的文档相当稀少.
让HTML直接指向服务器端图像会更好吗?然而,这并不理想,因为服务器端图像通常不会是静态的(有些会,有些则不会).