使用阿里云直接邮件发送单个邮件?

时间:2018-07-28 09:22:57

标签: alibaba-cloud

我正在使用REST API调用来触发来自我的应用程序的邮件。请求如下

https://dm.aliyuncs.com/?Action=SingleSendMail
&AccountName=test@example.com
&ReplyToAddress=true
&AddressType=1   
&ToAddress=test1@example.com
&Subject=Subject
&HtmlBody=body
&<Public request parameter>

在示例中,我假设它是一个GET调用,如何将附件和请求一起上传?

2 个答案:

答案 0 :(得分:0)

我看到这是一个GET通话。我认为您指的是以下链接

https://www.alibabacloud.com/help/doc-detail/29444.htm

我认为您无法使用此REST接口上传附件。另一种选择是创建第三方API,该API进而使用SDK的(JAVA / Node)来方便添加附件。

答案 1 :(得分:0)

电子邮件附件是电子邮件正文的一部分。

大多数人使用MIME编码库来处理电子邮件的正文以包含附件。这也意味着您必须使用HTML邮件。

注意:由于您使用的是REST API,因此我写了一篇文章,介绍如何在Python中使用阿里巴巴DirectMail REST API和实际工作代码:DirectMail REST API

以下是带有附件的电子邮件的示例:

From: John Doe <example@example.com>
MIME-Version: 1.0
Content-Type: multipart/mixed;
        boundary="XXXXboundary text"

This is a multipart message in MIME format.

--XXXXboundary text 
Content-Type: text/plain

this is the body text

--XXXXboundary text 
Content-Type: text/plain;
Content-Disposition: attachment;
        filename="test.txt"

this is the attachment text

--XXXXboundary text--

以下是电子邮件附件的示例Python代码:Python Examples

# Import smtplib for the actual sending function
import smtplib

# Here are the email package modules we'll need
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

COMMASPACE = ', '

# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = 'Our family reunion'
# me == the sender's email address
# family = the list of all recipients' email addresses
msg['From'] = me
msg['To'] = COMMASPACE.join(family)
msg.preamble = 'Our family reunion'

# Assume we know that the image files are all in PNG format
for file in pngfiles:
    # Open the files in binary mode.  Let the MIMEImage class automatically
    # guess the specific image type.
    fp = open(file, 'rb')
    img = MIMEImage(fp.read())
    fp.close()
    msg.attach(img)

# Send the email via our own SMTP server.
s = smtplib.SMTP('localhost')
s.sendmail(me, family, msg.as_string())
s.quit()