pythonメール、グループメール
2571 ワード
import smtplib
from email.mime.text import MIMEText
#
from email.mime.image import MIMEImage
#
from email.mime.multipart import MIMEMultipart
#
from email.header import Header
#
''' ctrl mime, init.py , email “ ”。 , “ ” “ ”。
“ ” “ ” , (module) , (package) , , “ ” 。
, email ? Python init.py 。
init.py 。 , , 。 import email 。
'''
text='''Python , :
Web Internet
'''
msg=MIMEText(text,'plain','utf-8')
# msg: ,
# type: , plain( )
# chartset: , “utf-8”
from_addr='[email protected]'
to_addr='[email protected]'
msg['From'] = Header(from_addr)#
msg['To'] = Header(to_addr)#
msg['Subject'] = Header('python test')#
#
'''
to_addrs = ['[email protected]','[email protected]']#
msg['to'] = Header(",".join(to_addrs))
header , , join , ,
while
to_addrs = []
while True:
a=input(' :')
#
to_addrs.append(a)
#
b=input(' ,n , :')
#
if b == 'n':
break
msg['to'] = Header(",".join(to_addrs))
, while
CSV
import csv
to_addrs=[]
with open(('C:\\Users\\Administrator\\Desktop\\text\\xxx.csv','r') as f):
a=csv.read(f)
for line in a:
to_addrs.append(line)
msg['to'] = Header(",".join(to_addrs))
'''
username = '[email protected]'
password = 'tdwkfcienqltjhjc'
#username:
#password: /
server = smtplib.SMTP_SSL('smtp.qq.com')
# server = smtplib.SMTP_SSL()
#ValueError: server_hostname cannot be an empty string or start with a leading dot.
#python 3.7 ssl.py smtplib.SMTP_SSL ,
# SSL , 。 server
#server.starttls() ,
server.connect('smtp.qq.com', 465)
# UnicodeDecodeError, :server.connect('smtp.qq.com', 465,'utf-8')
# QQ , 。 SMTP , , 【 】 。
#
server.login(username, password)
server.sendmail(from_addr, to_addr, msg.as_string())
#from_addr: , username
#to_addr:
#msg.as_string():
server.quit()
# , SMTP