【问题标题】:sending single mail using Alibaba Cloud Direct Mail?使用阿里云直邮发送单封邮件?
【发布时间】:2018-07-29 01:01:56
【问题描述】:

我正在使用 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 调用,我如何将附件与请求一起上传?

【问题讨论】:

    标签: alibaba-cloud


    【解决方案1】:

    我看到这是一个 GET 调用。我假设您指的是以下链接

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

    我认为您不能使用此 REST 接口上传附件。另一种选择是创建第三方 API,该 API 反过来会使用 SDK(JAVA/Node)来促进附件添加。

    【讨论】:

      【解决方案2】:

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

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

      注意:由于您使用的是REST API,我写了一篇关于如何使用阿里直邮REST API的文章以及Python中的实际工作代码: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()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-09
        • 2020-07-28
        • 2018-12-03
        • 2016-01-30
        • 2015-03-03
        • 2016-02-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多