├── .gitignore ├── LICENSE ├── README.md ├── emailer.py └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 wangweicheng7 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # python-emailer 2 | 3 | Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件 4 | 5 | ### 配置 6 | 7 | ``` 8 | def __init__(self, addresses): 9 | self.address = 'wangweicheng@xxx.com' # 发送的邮箱地址 10 | auth_info['server'] = 'mail.xxx.com' # 邮箱服务器 11 | auth_info['password'] = 'xxxxxx' # 邮箱密码 12 | 13 | ``` 14 | 15 | ### 使用 16 | 17 | ``` 18 | // 发送的邮箱数组,邮件标题,邮件内容 19 | EMailer(['80990366@qq.com']).send_mail("Python 邮件测试", "这是一封Python自动发送的邮件") 20 | 21 | ``` 22 | -------------------------------------------------------------------------------- /emailer.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | # by wangweicheng 4 | 5 | ''' 6 | Python SMTP 发送带附件电子邮件 7 | Author: weicheng wang 8 | Note: Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件 9 | ''' 10 | from email.mime.multipart import MIMEMultipart 11 | from email.mime.text import MIMEText 12 | from email.header import Header 13 | import smtplib 14 | from email import utils 15 | 16 | class EMailer(object): 17 | '''邮件操作类 18 | ''' 19 | def __init__(self, addresses): 20 | self.address = 'wangweicheng@xxx.com' # 发送的邮箱地址 21 | auth_info = {} 22 | auth_info['server'] = 'mail.xxx.com' # 邮箱服务器 23 | auth_info['user'] = self.address 24 | auth_info['password'] = 'xxxxxx' # 邮箱密码 25 | self.user_info = auth_info 26 | self.contacts = addresses # 接受邮件的邮箱 27 | print self.contacts 28 | 29 | def send_mail(self, subject, content): 30 | '''发送邮件私有方法 31 | ''' 32 | str_to = '; '.join(self.contacts) 33 | 34 | server = self.user_info.get('server') 35 | smtp_port = 25 # smtp_port 端口 36 | user = self.user_info.get('user') 37 | passwd = self.user_info.get('password') 38 | 39 | if not (server and user and passwd): 40 | print 'incomplete login info, exit now' 41 | return 42 | 43 | # 设定root信息 44 | msg_root = MIMEMultipart('related') 45 | msg_root['Subject'] = subject 46 | msg_root['From'] = '%s<%s>' % (Header(subject, 'utf-8'), self.address) 47 | msg_root['To'] = str_to 48 | 49 | msg_alternative = MIMEMultipart('alternative') 50 | msg_root.attach(msg_alternative) 51 | 52 | # 构造MIMEMultipart对象做为根容器 53 | main_msg = MIMEMultipart() 54 | 55 | html_msg = MIMEText( 56 | '

测试邮件

\ 57 |
'+ content + '
\ 58 |

* 请勿直接回复此邮件

', 59 | 'html', 60 | 'utf-8' 61 | ) 62 | 63 | main_msg.attach(html_msg) 64 | # 设置根容器属性 65 | main_msg['From'] = '%s<%s>' % (Header('Python 研发组', 'utf-8'), 'developer@python.com') # 如果你想隐藏发送邮件邮箱,此处可以伪装 66 | main_msg['To'] = str_to # 显示收件人地址 67 | main_msg['Subject'] = subject 68 | main_msg['Date'] = utils.formatdate() 69 | 70 | # 得到格式化后的完整文本 71 | full_text = main_msg.as_string() 72 | 73 | try: 74 | #发送邮件 75 | smtp = smtplib.SMTP(server, smtp_port) 76 | smtp.ehlo() 77 | smtp.starttls() 78 | smtp.ehlo() 79 | smtp.login(user, passwd) 80 | smtp.sendmail(self.address, self.contacts, full_text) 81 | smtp.quit() 82 | print "邮件发送成功!" 83 | except Exception, e: 84 | print "失败:" + str(e) 85 | 86 | 87 | if __name__ == "__main__": 88 | EMailer(['809405366@qq.com']).send_mail("Python 邮件测试", "这是一封Python自动发送的邮件") 89 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | # by wangweicheng 4 | 5 | try: 6 | from setuptools import setup 7 | except ImportError: 8 | from distutils.core import setup 9 | 10 | DOC = \ 11 | """ 12 | Features: 13 | Python SMTP 发送带附件电子邮件 14 | Author: weicheng wang 15 | Note: Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件 16 | 17 | 支持 easy_install:: 18 | 19 | $ sudo pip install pyapns 20 | 21 | 22 | `. 23 | """ 24 | 25 | setup( 26 | name = "pyemailer", 27 | version = "0.1.0", 28 | description = "Python SMTP 发送带附件电子邮件", 29 | long_description = DOC, 30 | author = "wangweicheng", 31 | author_email="809405366@qq.com", 32 | license="MIT", 33 | url="http://github.com/samuraisam/pyapns/tree/master", 34 | download_url="https://github.com/wangweicheng7/python-emailer", 35 | classifiers = [ 36 | 'Development Status :: 4 - Beta', 37 | 'Environment :: Web Environment', 38 | 'Intended Audience :: Developers', 39 | 'License :: OSI Approved :: MIT License', 40 | 'Operating System :: MAC OS', 41 | 'Programming Language :: Python', 42 | 'Topic :: Software Development :: Libraries :: Python Modules :: email'], 43 | packages=['pyapns'], 44 | package_data={}, 45 | install_requires=[] 46 | ) 47 | --------------------------------------------------------------------------------