电子邮件中的Base64编码图像

时间:2016-07-13 20:04:44

标签: python html email mime gmail-api

我正在尝试使用gmail api和python发送嵌入了base64编码图像的html电子邮件。

这是我想以电子邮件形式发送的html文件

unidecode(u"anu...

这是我的代码

<h1>This is a test message</h1>
<p>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAMAAABOo35HAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAZdEVYdFNvZnR3YXJlAFBhaW50Lk5FVCB2My41LjFO5zj5AAAA5FBMVEUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWwvIjAAAATHRSTlMAAgMEBQYICgsMDQ4QERMUFRYXGBkaGxwdHh8hIyUnKCkrLS4vMTI0Nzg5Ojs8PkJDRUhMTlFSVFhaW19iY2hrbnF0dnd6f4CEiIyPu8o/fwAAB+RJREFUeNrt3WFb00gUhuFDXCoWCpQVFgvNAksFSoEVRRR1S3HBkvn//2c/zDTJJE3SklDX6TNfQMnpJHcmk0l8uRSxWm1ltUUzbX3lpWS2lfbJOc1qp/76wjiq5SNsxrWTRopqYQeWrOa/YFg9dXAtA5LfNmLXIOOqaKKvhVjMV4XtkItwirbFRTh5O9ML1AYSk7RdERHxLcDDPZ5zdPMPrQeaIxGRhdPY3xwsCi1jpV4TkVdxK4Dsth3DWbenrDPGVbK9te+Hr6M/7oGTbLELsSUireiPbXByrsMEVgucZG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ca43pX3k5/ja02veDYIEFFlhggQUWWGCBBRZYYIEFFlhggQUWWGCBBdazY+2AU4DleCK5ZEsEmh1PJJdsiUCz24nkki0ZaHY6kVzWKhVodjeR/AyB5nlJJFcSaJ6XRHI1gWaG1jSB5kMsJg8017gQpwg0cyFOE2jeZmxNEWh2OpFceaDZ4UTycwSa3UwkP1+g+RXPOHmB5v8A5Tz/tpGY9IQAAAAASUVORK5CYII=">
</p>

但是,我在电子邮件内容中看不到我的图片。当我检查原文时,我可以看到我的HTML格式错误;

"""Send an email message from the user's account.
"""

import base64
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import mimetypes
import os

from apiclient import errors



def send_message(service, user_id, message):
  """Send an email message.

  Args:
    service: Authorized Gmail API service instance.
    user_id: User's email address. The special value "me"
    can be used to indicate the authenticated user.
    message: Message to be sent.

  Returns:
    Sent Message.
  """
  try:
    message = (service.users().messages().send(userId=user_id, body=message)
               .execute())
    print 'Message Id: %s' % message['id']
    return message
  except errors.HttpError, error:
    print 'An error occurred: %s' % error


def create_message(sender, to, subject, message_text):
  """Create a message for an email.

  Args:
    sender: Email address of the sender.
    to: Email address of the receiver.
    subject: The subject of the email message.
    message_text: The text of the email message.

  Returns:
    An object containing a base64url encoded email object.
  """
  message = MIMEText(message_text,'html')
  message['to'] = to
  message['from'] = sender
  message['subject'] = subject
  return {'raw': base64.urlsafe_b64encode(message.as_string())}

if __name__ == "__main__":
    from login import service

    with open("test.html","rb") as f:
        message_body = f.read()

    m = create_message("xxx@gmail.com","xxx@gmail.com","test message",message_body)

    send_message(service, "me", m)

如您所见,Content-Type: multipart/alternative; boundary=001a114442124f125f053789c367 --001a114442124f125f053789c367 Content-Type: text/plain; charset=UTF-8 This is a test message --001a114442124f125f053789c367 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <h1>This is a test message</h1> <p> <img src=3D"data:image/png;base64,iVBOR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KhVodjeR/AyB5nlJJFcSaJ6XRHI1gWaG1jSB5kMsJg8017gQpwg0cyFOE2jeZmxNEWh2Op= FceaDZ4UTycwSa3UwkP1+g+RXPOHmB5v8A5Tz/tpGY9IQAAAAASUVORK5CYII=3D"> </p> --001a114442124f125f053789c367-- 部分中插入了3D

使用gmail api在电子邮件中嵌入图片的正确方法是什么?

加成

经过进一步调查后,我意识到如果我将电子邮件中的<img src=3D"data:image/png;base64 ...转换回=3D,图像仍然会被破坏。

我认为问题可能与我的base64编码数据在电子邮件源代码中被分成多行有关。

2 个答案:

答案 0 :(得分:3)

我决定将图像添加为附件,而不是使用嵌入式base64编码。这是我的新test.html文件;

<h1>This is a test message</h1>
    <p>
    <img src="cid:asdfgh">
    </p>

这是我的代码

"""Send an email message from the user's account.
"""

import base64
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import mimetypes
import os

from apiclient import errors


def send_message(service, user_id, message):
  """Send an email message.

  Args:
    service: Authorized Gmail API service instance.
    user_id: User's email address. The special value "me"
    can be used to indicate the authenticated user.
    message: Message to be sent.

  Returns:
    Sent Message.
  """
  try:
    message = (service.users().messages().send(userId=user_id, body=message)
               .execute())
    print 'Message Id: %s' % message['id']
    return message
  except errors.HttpError, error:
    print 'An error occurred: %s' % error


def create_message(sender, to, subject, message_text):
  """Create a message for an email.

  Args:
    sender: Email address of the sender.
    to: Email address of the receiver.
    subject: The subject of the email message.
    message_text: The text of the email message.

  Returns:
    An object containing a base64url encoded email object.
  """
  message = MIMEText(message_text,'html')
  message['to'] = to
  message['from'] = sender
  message['subject'] = subject
  return {'raw': base64.urlsafe_b64encode(message.as_string())}

def create_multipart_message(sender, to, subject, message_text):

      message = MIMEMultipart()
      message['to'] = to
      message['from'] = sender
      message['subject'] = subject

      msg = MIMEText(message_text,'html')
      message.attach(msg)

      return message

def attach_file_to_multipart_message(message, file, content_id=None):

  content_type, encoding = mimetypes.guess_type(file)

  if content_type is None or encoding is not None:
    content_type = 'application/octet-stream'

  main_type, sub_type = content_type.split('/', 1)
  if main_type == 'text':
    fp = open(file, 'rb')
    msg = MIMEText(fp.read(), _subtype=sub_type)
    fp.close()
  elif main_type == 'image':
    fp = open(file, 'rb')
    msg = MIMEImage(fp.read(), _subtype=sub_type)
    fp.close()
  elif main_type == 'audio':
    fp = open(file, 'rb')
    msg = MIMEAudio(fp.read(), _subtype=sub_type)
    fp.close()
  else:
    fp = open(file, 'rb')
    msg = MIMEBase(main_type, sub_type)
    msg.set_payload(fp.read())
    fp.close()
  filename = os.path.basename(file)
  msg.add_header('Content-Disposition', 'attachment', filename=filename)
  if content_id:
    msg.add_header('Content-ID', '<%s>' % content_id)

  message.attach(msg)

def finalize_message(message):
    return {"raw": base64.urlsafe_b64encode(message.as_string())}

if __name__ == "__main__":
    from login import service

    m = create_multipart_message("xxxx@gmail","xxxx@gmail.com","test message", open("test.html").read())
    attach_file_to_multipart_message(m, "placeholder.png", "asdfgh")

    m_final = finalize_message(m)

    send_message(service, "me", m_final)

使用这种方法,我能够在我的html电子邮件中显示图像。

答案 1 :(得分:0)

&#34; =&#34;使用Quoted-Printable HTML时,character是一个转义字符,因此= 3D只是一个转义等于。

电子邮件可能还有其他问题。我不认为= 3D就是它。

尝试将您的消息提供给消息lint实用程序,看看它是否有任何抱怨。

https://tools.ietf.org/tools/msglint/