import smtplib
import os.path as op
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
from email import encoders
def send_mail(send_from, send_to, subject, message, files=[],
server="localhost", port=587, username='', password='',
use_tls=True):
"""Compose and send email with provided info and attachments.
Args:
send_from (str): from name
send_to (str): to name
subject (str): message title
message (str): message body
files (list[str]): list of file paths to be attached to email
server (str): mail server host name
port (int): port number
username (str): server auth username
password (str): server auth password
use_tls (bool): use TLS mode
"""
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach(MIMEText(message))
for path in files:
part = MIMEBase('application', "octet-stream")
with open(path, 'rb') as file:
part.set_payload(file.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',
'attachment; filename="{}"'.format(op.basename(path)))
msg.attach(part)
smtp = smtplib.SMTP(server, port)
if use_tls:
smtp.starttls()
smtp.login(username, password)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.quit()
323
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
import smtplib
msg = MIMEMultipart()
msg.attach(MIMEText(file("text.txt").read()))
msg.attach(MIMEImage(file("image.png").read()))
# to send
mailer = smtplib.SMTP()
mailer.connect()
mailer.sendmail(from_, to, msg.as_string())
mailer.close()
import os.path
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
def send_email(subject, message, from_email, to_email=[], attachment=[]):
"""
:param subject: email subject
:param message: Body content of the email (string), can be HTML/CSS or plain text
:param from_email: Email address from where the email is sent
:param to_email: List of email recipients, example: ["a@a.com", "b@b.com"]
:param attachment: List of attachments, exmaple: ["file1.txt", "file2.txt"]
"""
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = from_email
msg['To'] = ", ".join(to_email)
msg.attach(MIMEText(message, 'html'))
for f in attachment:
with open(f, 'rb') as a_file:
basename = os.path.basename(f)
part = MIMEApplication(a_file.read(), Name=basename)
part['Content-Disposition'] = 'attachment; filename="%s"' % basename
msg.attach(part)
email = smtplib.SMTP('your-smtp-host-name.com')
email.sendmail(from_email, to_email, msg.as_string())
10 回答
这是另一个:
它与第一个例子大致相同......但它应该更容易投入 .
这是我最终使用的代码:
代码与Oli的帖子大致相同 . 谢谢大家
代码来自Binary file email attachment problem post .
这是来自
Oli
for python 3的修改版本改编自here .
我能得到的最简单的代码是:
它是基于官方Django documentation
Gmail版本,使用Python 3.6(请注意,您需要更改Gmail设置才能通过smtp发送电子邮件:
用法:
要与任何其他电子邮件提供商一起使用,只需更改smtp配置即可 .
python 3的另一种方式(如果有人正在搜索):
确保在您的Gmail帐户中允许“less secure apps”
其他答案非常好,但我还是想分享一种不同的方法,以防有人在寻找替代方案 .
这里的主要区别是使用这种方法,您可以使用HTML / CSS格式化您的消息,这样您就可以获得创意并为您的电子邮件添加一些样式 . 虽然您没有强制使用HTML,但您仍然可以仅使用纯文本 .
请注意,此功能接受将电子邮件发送给多个收件人,并允许附加多个文件 .
我只在Python 2上尝试过这个,但我认为它在3上也可以正常工作:
I hope this helps! :-)
下面是我发布的Here和以下链接的组合,这使我更容易附加xlsx文件 . Found Here
有关说明,您可以使用此链接正确解释https://medium.com/@sdoshi579/to-send-an-email-along-with-attachment-using-smtp-7852e77623