我一直在使用python中的电子邮件模块,但我希望能够知道如何嵌入包含在html中的图像.
所以,例如,如果身体是这样的
<img src="../path/image.png"></img>
Run Code Online (Sandbox Code Playgroud)
我想将image.png嵌入到电子邮件中,该src属性应该替换为content-id.有人知道怎么做这个吗?
在我的Web应用程序中,我偶尔使用可重用的邮件程序应用程序发送电子邮件,如下所示:
user - self.user
subject = ("My subject")
from = "me@mydomain.com"
message = render_to_string("welcomeEmail/welcome.eml", {
"user" : user,
})
send_mail(subject, message, from, [email], priority="high" )
Run Code Online (Sandbox Code Playgroud)
我想发送一封包含嵌入图像的电子邮件,所以我尝试在邮件客户端发送邮件,查看源代码,并将其放入我的模板(welcome.eml),但我一直无法将其渲染到正确地在邮件客户端发送时.
有没有人知道我有一个简单的方法来创建具有内联图像的mime编码邮件模板,当我发送它们时它们会正确呈现?
我目前有一个程序,它会从列表中随机选择引号并通过电子邮件发送给它们.我现在正试图在电子邮件中嵌入图像.我遇到了一个问题,我可以附上电子邮件,但我的报价不再有效.我在网上研究过,解决方案对我不起作用.请注意,我使用的是Python 3.2.2.
任何指导将不胜感激.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
attachment = 'bob.jpg'
msg = MIMEMultipart()
msg["To"] = to_addr
msg["From"] = from_addr
msg["Subject"] = subject_header
#msgText = MIMEText(<b>%s</b><br><img src="cid:bob.jpg"><br>, 'html') % body
fp = open(attachment, 'rb')
img = MIMEImage(fp.read())
fp.close()
msg.attach(img)
#email_message = '%s\n%s\n%s' % (subject_header, body, img)
email_message = '%s\n%s' % (subject_header, body)
emailRezi = smtplib.SMTP(mail_server, mail_server_port)
emailRezi.set_debuglevel(1)
emailRezi.login(mail_username, mail_password)
emailRezi.sendmail(from_addr, to_addr, email_message)
#emailRezi.sendmail(from_addr, to_addr, msg.as_string())
emailRezi.quit()
Run Code Online (Sandbox Code Playgroud)
从上面的代码可以看出,我尝试了不同的方法(参考#)
我使用 python 发送电子邮件。我需要将图片插入到正文电子邮件中,如下所示:
\n\n\n\n但我的输出不显示这样的图像:
\n\n\n\n我尝试了很多解决方案来修复它,但效果不佳。\n这是我的代码如下:
\n\nimport smtplib\nfrom email.mime.image import MIMEImage\nfrom email.mime.multipart import MIMEMultipart\nfrom email.mime.text import MIMEText\nfrom email.header import Header\nimport pandas as pd\nimport win32com.client as win32\nimport openpyxl\nimport sys\nfrom PIL import ImageGrab\nfrom pathlib import Path\ndf = pd.read_excel("xxxxx", sheet_name = "sample",nrows = 2, usecols = "A:W")\ndf1 = pd.read_excel("xxxx", sheet_name = "sample")\nexcel_path = ("sample")\nexcel = win32.gencache.EnsureDispatch(\'Excel.Application\')\nexcel.Visible = False\nexcel.DisplayAlerts = False\nwb = excel.Workbooks.Open(excel_path)\nws = wb.Worksheets(1)\nwin32c = win32.constants\nws.Range("A1:H33").CopyPicture(Format=win32c.xlBitmap)\nimg = ImageGrab.grabclipboard()\nimage_path = str("path" + \'te.png\')\nimg.save(image_path)\noutlook = win32.gencache.EnsureDispatch(\'Outlook.Application\')\nnew_mail = outlook.CreateItem(0)\nuser = df1.loc[34,"Unnamed: 4"]\napprover …Run Code Online (Sandbox Code Playgroud) email ×4
python ×4
mime ×2
attachment ×1
django ×1
html ×1
multipart ×1
outlook ×1
python-3.x ×1