电子邮件中的 Base64 编码图像
Base64 encoded image in email
我正在尝试使用 gmail api 和 python.
发送嵌入了 base64 编码图像的电子邮件
这是我要作为电子邮件发送的 html 文件
<h1>This is a test message</h1>
<p>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAMAAABOo35HAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAZdEVYdFNvZnR3YXJlAFBhaW50Lk5FVCB2My41LjFO5zj5AAAA5FBMVEUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWwvIjAAAATHRSTlMAAgMEBQYICgsMDQ4QERMUFRYXGBkaGxwdHh8hIyUnKCkrLS4vMTI0Nzg5Ojs8PkJDRUhMTlFSVFhaW19iY2hrbnF0dnd6f4CEiIyPu8o/fwAAB+RJREFUeNrt3WFb00gUhuFDXCoWCpQVFgvNAksFSoEVRRR1S3HBkvn//2c/zDTJJE3SklDX6TNfQMnpJHcmk0l8uRSxWm1ltUUzbX3lpWS2lfbJOc1qp/76wjiq5SNsxrWTRopqYQeWrOa/YFg9dXAtA5LfNmLXIOOqaKKvhVjMV4XtkItwirbFRTh5O9ML1AYSk7RdERHxLcDDPZ5zdPMPrQeaIxGRhdPY3xwsCi1jpV4TkVdxK4Dsth3DWbenrDPGVbK9te+Hr6M/7oGTbLELsSUireiPbXByrsMEVgucZGuBBRZYYIEFFlhggQUWWGCBBRZYYIH1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RZYYIEFFlhggQUWWGCBBdazY+2AU4DleCK5ZEsEmh1PJJdsiUCz24nkki0ZaHY6kVzWKhVodjeR/AyB5nlJJFcSaJ6XRHI1gWaG1jSB5kMsJg8017gQpwg0cyFOE2jeZmxNEWh2OpFceaDZ4UTycwSa3UwkP1+g+RXPOHmB5v8A5Tz/tpGY9IQAAAAASUVORK5CYII=">
</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())}
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)
但是,我没有在电子邮件内容中看到我的图像。当我检查原件时,我可以看到我的 html 像这样畸形;
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,iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAMAA=
ABOo35HAAAAAXNSR0IArs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cSaJ6XRHI1gWaG1jSB5kMsJg8017gQpwg0cyFOE2jeZmxNEWh2Op=
FceaDZ4UTycwSa3UwkP1+g+RXPOHmB5v8A5Tz/tpGY9IQAAAAASUVORK5CYII=3D">
</p>
--001a114442124f125f053789c367--
如你所见,在<img src=3D"data:image/png;base64 ...
部分插入了一个3D
。
使用 gmail 在电子邮件中嵌入图像的正确方法是什么 api?
加法
经过进一步调查,我发现如果我将电子邮件中的 =3D
转换回 =
,图像仍然损坏。
我认为问题可能与我的 base64 编码数据在电子邮件源代码中分成多行有关。
当使用 Quoted-Printable HTML 时,“=”字符是一个转义字符,所以 =3D 只是一个转义的等号。
电子邮件可能还有其他问题。我不认为 =3D 是它。
尝试将您的消息提供给消息 lint 实用程序,看看它是否抱怨任何事情。
我决定添加图像作为附件,而不是使用嵌入的 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 电子邮件中显示图像。
我正在尝试使用 gmail api 和 python.
发送嵌入了 base64 编码图像的电子邮件这是我要作为电子邮件发送的 html 文件
<h1>This is a test message</h1>
<p>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAMAAABOo35HAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAZdEVYdFNvZnR3YXJlAFBhaW50Lk5FVCB2My41LjFO5zj5AAAA5FBMVEUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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RjzpaKWzWE2zHpV4dPtC4nm3sTHJT+FQS0Ur3cXyw18z8qI5yaSx2/kxSTOr9Sq8Gf7PsXrhr0eYfN5ulMZey49WXmbEJN3Fim6GJgraDdPYgeTHJPsZMUlXsWp/3odLzAvtcmWNl7yYZJARk3QUqzN6qvuuXS717e9cj5cjKYxWjo1JuokVJhyDpojcq7slEc+8gFDqH+9pMUlHsb4bqq8NEak9vv9NRJr61zNMPjI3WpkRk+RNKW9KwQILLLDAAgsssMACCyywwAILLLDAAgsssMACCyywwAILLLDAAgsssMACCyywwAILLLDAAgsssMACCyywwAILLLDAAgsssMACCyywwAILLLDAAgsssMACCyywwAILLLDAAgsssMpi7YBVULEV/cx3EGu7UqzV6GdHDmLtRYe3XR5r+bz4437d1ogd3UZ5rBdn0Q/PNlyzOo1hrZTHkoPY550ftBxqW+34oZ1KBVivz+ej7VWBtXg2H1irVWDNydDal0qw5HAOrE4WK8KqzcGF2JCKsObgQvSlMizZdnxs+QsVYsnykcNUp41pnrUnePRe2HHW6mBRKsYSWfZPHJQ621+f8i3OpC/4XrVca43pX3k5/ja02veDYIEFFlhggQUWWGCBBRZYYIEFFlhggQUWWGCBBdazY+2AU4DleCK5ZEsEmh1PJJdsiUCz24nkki0ZaHY6kVzWKhVodjeR/AyB5nlJJFcSaJ6XRHI1gWaG1jSB5kMsJg8017gQpwg0cyFOE2jeZmxNEWh2OpFceaDZ4UTycwSa3UwkP1+g+RXPOHmB5v8A5Tz/tpGY9IQAAAAASUVORK5CYII=">
</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())}
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)
但是,我没有在电子邮件内容中看到我的图像。当我检查原件时,我可以看到我的 html 像这样畸形;
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,iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAMAA=
ABOo35HAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQA=
AAAZdEVYdFNvZnR3YXJlAFBhaW50Lk5FVCB2My41LjFO5zj5AAAA5FBMVEUAAAAAAAAAAAAAAAA=
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWwvIjAAAATHR=
STlMAAg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
GJgraDdPYgeTHJPsZMUlXsWp/3odLzAvtcmWNl7yYZJARk3QUqzN6qvuuXS717e9cj5cjKYxWjo=
1JuokVJhyDpojcq7slEc+8gFDqH+9pMUlHsb4bqq8NEak9vv9NRJr61zNMPjI3WpkRk+RNKW9Kw=
QILLLDAAgsssMACCyywwAILLLDAAgsssMACCyywwAILLLDAAgsssMACCyywwAILLLDAAgsssMAC=
CyywwAILLLDAAgsssMACCyywwAILLLDAAgsssMACCyywwAILLLDAAgsssMpi7YBVULEV/cx3EGu=
7UqzV6GdHDmLtRYe3XR5r+bz4437d1ogd3UZ5rBdn0Q/PNlyzOo1hrZTHkoPY550ftBxqW+34oZ=
1KBVivz+ej7VWBtXg2H1irVWDNydDal0qw5HAOrE4WK8KqzcGF2JCKsObgQvSlMizZdnxs+QsVY=
snykcNUp41pnrUnePRe2HHW6mBRKsYSWfZPHJQ621+f8i3OpC/4XrVca43pX3k5/ja02veDYIEF=
FlhggQUWWGCBBRZYYIEFFlhggQUWWGCBBdazY+2AU4DleCK5ZEsEmh1PJJdsiUCz24nkki0ZaHY=
6kVzWKhVodjeR/AyB5nlJJFcSaJ6XRHI1gWaG1jSB5kMsJg8017gQpwg0cyFOE2jeZmxNEWh2Op=
FceaDZ4UTycwSa3UwkP1+g+RXPOHmB5v8A5Tz/tpGY9IQAAAAASUVORK5CYII=3D">
</p>
--001a114442124f125f053789c367--
如你所见,在<img src=3D"data:image/png;base64 ...
部分插入了一个3D
。
使用 gmail 在电子邮件中嵌入图像的正确方法是什么 api?
加法
经过进一步调查,我发现如果我将电子邮件中的 =3D
转换回 =
,图像仍然损坏。
我认为问题可能与我的 base64 编码数据在电子邮件源代码中分成多行有关。
当使用 Quoted-Printable HTML 时,“=”字符是一个转义字符,所以 =3D 只是一个转义的等号。
电子邮件可能还有其他问题。我不认为 =3D 是它。
尝试将您的消息提供给消息 lint 实用程序,看看它是否抱怨任何事情。
我决定添加图像作为附件,而不是使用嵌入的 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 电子邮件中显示图像。