我目前有一个程序,它会从列表中随机选择引号并通过电子邮件发送给它们.我现在正试图在电子邮件中嵌入图像.我遇到了一个问题,我可以附上电子邮件,但我的报价不再有效.我在网上研究过,解决方案对我不起作用.请注意,我使用的是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)
从上面的代码可以看出,我尝试了不同的方法(参考#)
我已经能够使用Java将图像作为附件发送到电子邮件中.我现在正试图在电子邮件正文中发送相同的图像,如下所示:
public static void main(String[] args) throws NoSuchProviderException, MessagingException {
System.out.println("Sending mail...");
Properties props = new Properties();
props.setProperty("mail.smtp.starttls.enable", "true");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.setProperty("mail.smtp.port", "587");
props.setProperty("mail.smtp.user", "mysusername");
props.setProperty("mail.smtp.password", "mypassword");
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("HTML mail with images");
message.setFrom(new InternetAddress("myaddress@gmail.com"));
message.setContent
("<h1>This is a test</h1>"
+ "<img src=\"C:/Users/pc/Desktop/Photos/Shammah.PNG\">",
"text/html");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("receiver@simbatech.biz"));
transport.connect();//This is line 46
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
Run Code Online (Sandbox Code Playgroud)
我得到这个输出:
Sending mail...
DEBUG: setDebug: JavaMail version 1.4ea …Run Code Online (Sandbox Code Playgroud) 我看过其他几篇文章,包括:
创建带有图像的MIME电子邮件模板以使用python / django发送
这些以及smtplib和电子邮件的python文档使我离我很近。我正在使用下面的代码来创建嵌入了简单jpg的电子邮件。如果我将电子邮件发送给gmail,它将很好地显示嵌入的图像,但Outlook 2013无法显示。
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
From = ''
To = ''
msg = MIMEMultipart()
msg['Subject'] = 'image test message'
msg['From'] = From
msg['To'] = To
text = 'This is sample text from me'
html = '''
<html>
<head>
<title> this is a test title </title>
</head>
<body>
<p> Test me <br>
Another line <br>
This is the image you were looking for <img …Run Code Online (Sandbox Code Playgroud) 我不能发送python中的电子邮件与身体作为多部分电子邮件.我尝试过的所有内容都将所有内容都作为附件,我无法将文本或html显示在正文中.
msg = MIMEMultipart()
if msg_mime_type == 'text' or not msg_mime_type:
new_body = MIMEText(body, 'text')
elif msg_mime_type == 'image':
new_body = MIMEImage(body)
elif msg_mime_type == 'html':
new_body = MIMEText(body, 'html')
new_body.add_header('Content-Disposition', 'inline', filename='body')
msg.set_payload(new_body) #also tried msg.attach(new_body)
Run Code Online (Sandbox Code Playgroud)
我需要使用一个,Multipart所以我也可以添加附件,但为了简单起见我保留了代码.
我有以下脚本可以成功发送带有附加到hotmail的图像的邮件。问题是,如果我将相同的邮件发送到GMAIL,则图像将附加在邮件中,而不嵌入HTML中。为什么会这样?如何解决?例如,HTML内的图像以以下方式显示:
这是当前代码
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import base64
import email
import smtplib
import os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.header import Header
import codecs
from bs4 import BeautifulSoup
import mimetypes
import requests
import time
SMTP_SERVER = "xxx"
SMTP_PORT = 587
SMTP_USERNAME = "xxx"
SMTP_PASSWORD = "xxx"
SMTP_USE_TLS = False
FROM_EMAIL = "xxx@xxx.com"
lista_mails = ['my_mail@hotmail.com']
lista_apodos =['user']
subject='Test Mail'
def get_image(img_src):
if img_src.startswith('http://') or img_src.startswith('https://'):
try:
resp = requests.get(img_src) …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) python ×5
email ×4
html ×2
outlook ×2
gmail ×1
jakarta-mail ×1
java ×1
mail-sender ×1
mime ×1
python-3.x ×1
smtp ×1
smtplib ×1