Heim  >  Artikel  >  Backend-Entwicklung  >  E-Mails automatisch senden-2017-7-5

E-Mails automatisch senden-2017-7-5

PHP中文网
PHP中文网Original
2017-07-05 18:13:221186Durchsuche
<span style="color: #577999; font-style: italic">#coding=utf-8<br><span style="color: #161b99; font-style: italic">'''<br><span style="color: #161b99; font-style: italic">    Created on 2017-7-5<br><span style="color: #161b99; font-style: italic">    @auther:Qigege<br><span style="color: #161b99; font-style: italic">    Project:<span style='color: #161b99; font-style: italic; font-family: "宋体"'>发送邮箱测试<span style="color: #161b99; font-style: italic"><br><span style="color: #161b99; font-style: italic"><br><span style="color: #161b99; font-style: italic">'''<br><span style="color: #4038e3">import smtplib<br><span style="color: #4038e3">from email.mime.text <span style="color: #4038e3">import MIMEText<br><br><span style="color: #577999; font-style: italic">#SMTP<span style='color: #577999; font-style: italic; font-family: "宋体"'>服务器<br>mail_host=<span style="color: #ef3142">'smtp.163.com'<br>mail_user=<span style="color: #ef3142">'******@163.com'<br><span style="color: #577999; font-style: italic">#<span style='color: #577999; font-style: italic; font-family: "宋体"'>网易邮箱为网页版,需要用到客户端密码,进入网页版邮箱中设置授权码,即客户端密码<br>mail_password=<span style="color: #ef3142">'******'<br><span style="color: #ef3142"><br><span style="color: #577999; font-style: italic">#<span style='color: #577999; font-style: italic; font-family: "宋体"'>发件人邮箱<br>sender=<span style="color: #ef3142">'******@163.com'<br><span style="color: #577999; font-style: italic">#<span style='color: #577999; font-style: italic; font-family: "宋体"'>接收人邮箱<br>receiver=[<span style="color: #ef3142">'******@qq.com',<span style="color: #ef3142">'******@qq.com']<br><br>content=<span style="color: #008080; font-weight: bold">u'<span style='color: #008080; font-weight: bold; font-family: "宋体"'>邮箱测试<span style="color: #008080; font-weight: bold">......' <span style="color: #577999; font-style: italic">#<span style='color: #577999; font-style: italic; font-family: "宋体"'>内容<br>title=<span style="color: #ef3142">'Python SMTP Mail Test' <span style="color: #577999; font-style: italic">#<span style='color: #577999; font-style: italic; font-family: "宋体"'>主题<br>message=MIMEText(content,<span style="color: #ef3142">'plain',<span style="color: #ef3142">'utf-8')<br><span style="color: #577999; font-style: italic">#<span style='color: #577999; font-style: italic; font-family: "宋体"'>邮箱发送者地址<br><span style="color: #577999; font-style: italic">#<span style='color: #577999; font-style: italic; font-family: "宋体"'>格式化字符串<span style="color: #577999; font-style: italic">format(),{}==%<span style='color: #577999; font-style: italic; font-family: "宋体"'>,例:<span style="color: #577999; font-style: italic">‘{1}<span style='color: #577999; font-style: italic; font-family: "宋体"'>,<span style="color: #577999; font-style: italic">{0}<span style='color: #577999; font-style: italic; font-family: "宋体"'>,<span style="color: #577999; font-style: italic">{1}’.format('abc',12)-->'12,abc,12'<br>message[<span style="color: #ef3142">'From']=<span style="color: #ef3142">'{}'.format(sender)<br><span style="color: #577999; font-style: italic">#<span style='color: #577999; font-style: italic; font-family: "宋体"'>邮件接受者地址,字符串列表<span style="color: #577999; font-style: italic">[‘<span style='color: #577999; font-style: italic; font-family: "宋体"'>接受地址<span style="color: #577999; font-style: italic">1’<span style='color: #577999; font-style: italic; font-family: "宋体"'>,<span style="color: #577999; font-style: italic">‘<span style='color: #577999; font-style: italic; font-family: "宋体"'>接受地址<span style="color: #577999; font-style: italic">2’<span style='color: #577999; font-style: italic; font-family: "宋体"'>,<span style="color: #577999; font-style: italic">......]<span style='color: #577999; font-style: italic; font-family: "宋体"'>或<span style="color: #577999; font-style: italic">‘<span style='color: #577999; font-style: italic; font-family: "宋体"'>接受地址<span style="color: #577999; font-style: italic">’<br>message[<span style="color: #ef3142">'To']=<span style="color: #ef3142">','.join(receiver)  <span style="color: #577999; font-style: italic">#type(message['To'])<span style='color: #577999; font-style: italic; font-family: "宋体"'>为<span style="color: #577999; font-style: italic">str<br>message[<span style="color: #ef3142">'Subject']=title<br><br><span style="color: #4038e3">try:<br><span style="color: #577999; font-style: italic">#<span style='color: #577999; font-style: italic; font-family: "宋体"'>启用<span style="color: #577999; font-style: italic">SSL<span style='color: #577999; font-style: italic; font-family: "宋体"'>,端口一般是<span style="color: #577999; font-style: italic">465<br><span style="color: #577999; font-style: italic">    smtpObj=smtplib.SMTP_SSL(mail_host,<span style="color: #549937">465)<br><span style="color: #577999; font-style: italic">#<span style='color: #577999; font-style: italic; font-family: "宋体"'>登录验证<br><span style='color: #577999; font-style: italic; font-family: "宋体"'>    smtpObj.login(mail_user,mail_password)<br><span style="color: #577999; font-style: italic">#<span style='color: #577999; font-style: italic; font-family: "宋体"'>发送<br><span style='color: #577999; font-style: italic; font-family: "宋体"'>    smtpObj.sendmail(sender,receiver,message.as_string())<br><span style="color: #577999; font-style: italic">#as_string()<span style='color: #577999; font-style: italic; font-family: "宋体"'>将<span style="color: #577999; font-style: italic">MIMEText<span style='color: #577999; font-style: italic; font-family: "宋体"'>或<span style="color: #577999; font-style: italic">MIMEMultipart<span style='color: #577999; font-style: italic; font-family: "宋体"'>对象转换为<span style="color: #577999; font-style: italic">str<br><span style="color: #577999; font-style: italic">    <span style="color: #4038e3">print <span style="color: #ef3142">'mail has been send successfully.'<br><span style="color: #4038e3">except smtplib.SMTPException <span style="color: #4038e3">as e:<br><span style="color: #4038e3">print e</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

Das obige ist der detaillierte Inhalt vonE-Mails automatisch senden-2017-7-5. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn