前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python mail 收发

python mail 收发

作者头像
用户5760343
发布2022-05-13 09:58:44
7330
发布2022-05-13 09:58:44
举报
文章被收录于专栏:sktj

------------------------mailconfig.py popservername = 'pop.secureserver.net' popusername = 'PP4E@learning-python.com'

smtpservername = 'smtpout.secureserver.net'

myaddress = 'PP4E@learning-python.com' mysignature = ('Thanks,\n' '--Mark Lutz (http://learning-python.com, http://rmi.net/~lutz)')

smtpuser = None # per your ISP smtppasswdfile = '' # set to '' to be asked

poppasswdfile = r'c:\temp\pymailgui.txt' # set to '' to be asked

sentmailfile = r'.\sentmail.txt' # . means in current working dir

savemailfile = r'c:\temp\savemail.txt' # not used in PyMailGUI: dialog

fetchEncoding = 'utf8' # 4E: how to decode and store message text (or latin1?) headersEncodeTo = None # 4E: how to encode non-ASCII headers sent (None=utf8)

fetchlimit = 25 # 4E: maximum number headers/emails to fetch on loads

--------------------------------------------------popmail.py

!/usr/local/bin/python

import poplib, getpass, sys, mailconfig

mailserver = mailconfig.popservername # ex: 'pop.rmi.net' mailuser = mailconfig.popusername # ex: 'lutz' mailpasswd = getpass.getpass('Password for %s?' % mailserver)

print('Connecting...') server = poplib.POP3(mailserver) server.user(mailuser) # connect, log in to mail server server.pass_(mailpasswd) # pass is a reserved word

try: print(server.getwelcome()) # print returned greeting message msgCount, msgBytes = server.stat() print('There are', msgCount, 'mail messages in', msgBytes, 'bytes') print(server.list()) print('-' * 80) input('[Press Enter key]')

代码语言:javascript
复制
for i in range(msgCount):
    hdr, message, octets = server.retr(i+1)    # octets is byte count
    for line in message: print(line.decode())  # retrieve, print all mail
    print('-' * 80)                            # mail text is bytes in 3.x
    if i < msgCount - 1:
       input('[Press Enter key]')              # mail box locked till quit

finally: # make sure we unlock mbox server.quit() # else locked till timeout print('Bye.')

---------------------------------------------smtpmail.py

!/usr/local/bin/python

import smtplib, sys, email.utils, mailconfig mailserver = mailconfig.smtpservername # ex: smtp.rmi.net

From = input('From? ').strip() # or import from mailconfig To = input('To? ').strip() # ex: python-list@python.org Tos = To.split(';') # allow a list of recipients Subj = input('Subj? ').strip() Date = email.utils.formatdate() # curr datetime, rfc2822

standard headers, followed by blank line, followed by text

text = ('From: %s\nTo: %s\nDate: %s\nSubject: %s\n\n' % (From, To, Date, Subj))

print('Type message text, end with line=[Ctrl+d (Unix), Ctrl+z (Windows)]') while True: line = sys.stdin.readline() if not line: break # exit on ctrl-d/z

if line[:4] == 'From':

line = '>' + line # servers may escape

代码语言:javascript
复制
text += line

print('Connecting...') server = smtplib.SMTP(mailserver) # connect, no log-in step failed = server.sendmail(From, Tos, text) server.quit() if failed: # smtplib may raise exceptions print('Failed recipients:', failed) # too, but let them pass here else: print('No errors.') print('Bye.')

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • !/usr/local/bin/python
  • !/usr/local/bin/python
  • standard headers, followed by blank line, followed by text
  • if line[:4] == 'From':
  • line = '>' + line # servers may escape
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档