如何在 python 中发送邮件
How to send mails in python
我有这个 python 脚本,它使用 smtplib 模块发送电子邮件。
#! /usr/bin/python
import smtplib
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from1 = "root"
to = "ankur.kulshrestha@ericsson.com"
subject = "test mail"
msg = MIMEMultipart()
msg['From'] = from1
msg['To'] = to
msg['Subject'] = subject
text = """Hi,
This is test messgae"""
html = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.
</p>
</body>
</html>
"""
fi = open("dragon.jpg", 'rb')
img = MIMEImage(fi.read())
fi.close()
msg.attach(img)
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
smtp_obj = smtplib.SMTP('rinacac-test.egi.ericsson.com')
smtp_obj.sendmail(from1, to, msg.as_string())
smtp_obj.quit()
脚本正在运行,但我的 html 内容 'html' 和图像 'dragon.jpg' 显示为附件。我希望它们显示为我的邮件内容而不是附件。请帮助
更新:
我仍然无法理解 Multipart/alternative 在这里扮演什么角色。此外,在您之前分享的 link 中,邮件中未显示行。
msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)
其次,包含 html 字符串的对象 'msgText' 附加了 'msgAlnernative'
msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>Nifty!', 'html')
msgAlnernative.attach(msgText)
但是读取图像的对象 'msgImage' 附加了 'msgRoot'。为什么会这样..
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)
我找不到任何关于 MIMEMultipart/related 和备选方案的帮助文档。请帮助
我想这会解决你的问题
How to display base64 images in html
如果您的邮件提供商支持电子邮件中的 html 标签
抱歉,因为这是一个特定的 python 问题,link 中的示例似乎是 php 这里的 python 解决方案
import base64
img = 'test.jpg'
with open(img, 'rb') as imgfile:
img64 = base64.encodestring(imgfile.read())
###
html = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.
</p>
<img width="160" height="160" alt="tick" src="data:image/jpg;base64,{0}">
</body>
</html>
""".format(img64)
###
我有这个 python 脚本,它使用 smtplib 模块发送电子邮件。
#! /usr/bin/python
import smtplib
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from1 = "root"
to = "ankur.kulshrestha@ericsson.com"
subject = "test mail"
msg = MIMEMultipart()
msg['From'] = from1
msg['To'] = to
msg['Subject'] = subject
text = """Hi,
This is test messgae"""
html = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.
</p>
</body>
</html>
"""
fi = open("dragon.jpg", 'rb')
img = MIMEImage(fi.read())
fi.close()
msg.attach(img)
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
smtp_obj = smtplib.SMTP('rinacac-test.egi.ericsson.com')
smtp_obj.sendmail(from1, to, msg.as_string())
smtp_obj.quit()
脚本正在运行,但我的 html 内容 'html' 和图像 'dragon.jpg' 显示为附件。我希望它们显示为我的邮件内容而不是附件。请帮助
更新: 我仍然无法理解 Multipart/alternative 在这里扮演什么角色。此外,在您之前分享的 link 中,邮件中未显示行。
msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)
其次,包含 html 字符串的对象 'msgText' 附加了 'msgAlnernative'
msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>Nifty!', 'html')
msgAlnernative.attach(msgText)
但是读取图像的对象 'msgImage' 附加了 'msgRoot'。为什么会这样..
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)
我找不到任何关于 MIMEMultipart/related 和备选方案的帮助文档。请帮助
我想这会解决你的问题 How to display base64 images in html
如果您的邮件提供商支持电子邮件中的 html 标签
抱歉,因为这是一个特定的 python 问题,link 中的示例似乎是 php 这里的 python 解决方案
import base64
img = 'test.jpg'
with open(img, 'rb') as imgfile:
img64 = base64.encodestring(imgfile.read())
###
html = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.
</p>
<img width="160" height="160" alt="tick" src="data:image/jpg;base64,{0}">
</body>
</html>
""".format(img64)
###