如何使用Python 3.6发送电子邮件附件 [英] How to send email attachments with Python 3.6

查看:117
本文介绍了如何使用Python 3.6发送电子邮件附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请介意帮助我! 我使用此页面上的所有代码如何使用Python发送电子邮件附件

would you mind helping me, please! I use all code's from this page How to send email attachments with Python

但是没用=(

这是我使用的最后一个版本

This is last version which i used

import smtplib
from smtplib import SMTP_SSL
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
import os

filepath = 'D:/files/1.jpg'
fromaddr = "name@gmail.com"
toaddr = "name2@gmail.com"
password = '********'
mail_adr = 'smtp.gmail.com'
mail_port = 587

# Compose attachment
part = MIMEBase('application', "octet-stream")
part.set_payload(open(filepath, "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % os.path.basename(filepath))

# Compose message
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg.attach(part)

# Send mail
smtp = SMTP_SSL()
smtp.set_debuglevel(1)
smtp.connect(mail_adr, mail_port)
smtp.login(fromaddr, password)
smtp.sendmail(fromaddr, toaddr, msg.as_string())
smtp.quit()

这是我跌倒的错误

connect: ('smtp.gmail.com', 587)
connect: ('smtp.gmail.com', 587)
Traceback (most recent call last):
  File "C:/Users/Oleg/Desktop/444.py", line 31, in <module>
    smtp.connect(mail_adr, mail_port)
  File "C:\Users\Oleg\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py", line 335, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Users\Oleg\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py", line 1037, in _get_socket
    server_hostname=self._host)
  File "C:\Users\Oleg\AppData\Local\Programs\Python\Python36-32\lib\ssl.py", line 401, in wrap_socket
    _context=self, _session=session)
  File "C:\Users\Oleg\AppData\Local\Programs\Python\Python36-32\lib\ssl.py", line 808, in __init__
    self.do_handshake()
  File "C:\Users\Oleg\AppData\Local\Programs\Python\Python36-32\lib\ssl.py", line 1061, in do_handshake
    self._sslobj.do_handshake()
  File "C:\Users\Oleg\AppData\Local\Programs\Python\Python36-32\lib\ssl.py", line 683, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:749)

推荐答案

您是否尝试过 emailpy ? (我是作者)

Have you tried emailpy? (I am the author)

它支持任何附件格式,除非您的电子邮件提供商对此加以限制.

It supports any attachment format unless your email provider restricts it.

示例:

import emailpy
emailManager = emailpy.EmailManager('yourcooladdress@domain.com', 'youremailpassword') # this may take a few seconds to generate
emailManager.send(['sendsomething@gmail.com', 'anotheremail@hotmail.com'],  \
subject = 'Subject here', body = 'body text here', html = 'some html here', \
attachments = ['file1.png', 'file2.txt', 'file3.py'], \
nofileattach = {'file.txt': 'hi, this is some data'}) 
# send email to sendsomething@gmail.com and anotheremail@hotmail.com with subject "Subject here"
#, body "body text here", html "some html here", and some attachments: 
# "file1.png", "file2.txt", "file3.py". Also, it adds a file called file.txt
# that has the contents "hi, this is some data"

emailpy文档可以在其PyPI页面上下载,也可以从

The emailpy docs can either be downloaded on its PyPI page, or from here

所有电子邮件服务器均已通过emailpy成功测试,如果在python 3.x上使用emailpy,则emailpy不会出现任何错误(Python 2不支持emailpy,主要是因为语法)

All email servers have been successfully tested with emailpy, and there will be no errors with emailpy if you use it on python 3.x (emailpy is not supported in python 2, mainly because of the syntax)

这篇关于如何使用Python 3.6发送电子邮件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆