zoukankan      html  css  js  c++  java
  • python 之发送邮件服务[原著] 海瑞博客

    Python 发送邮件

    使用默认的django的发送邮件,只适用于单邮箱。

    作者:海瑞博客 http://www.hairuinet.com/

    setting中配置

    # send e-mail

    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'  #email后端

    EMAIL_USE_TLS = False   #是否使用TLS安全传输协议

    EMAIL_USE_SSL = True    #是否使用SSL加密,qq企业邮箱要求使用

    EMAIL_HOST = 'smtp.exmail.qq.com'   #发送邮件的邮箱 的 SMTP服务器,这里用了qq企业邮箱

    EMAIL_PORT = 465     #发件箱的SMTP服务器端口

    EMAIL_HOST_USER = 'hairui@hairuinet.com'    #发送邮件的邮箱地址

    EMAIL_HOST_PASSWORD = 'hairui2017HAIRUI'         #发送邮件的邮箱密码


    在视图中使用  

    from django.core.mail import send_mail  

    # send_mail的参数分别是  邮件标题邮件内容发件箱(settings.py中设置过的那个),收件箱列表(可以发送给多个人),失败静默(若发送失败,报错提示我们)

    send_mail('python django 测试邮件', '你好朋友!人生苦短,我爱python!.', 'hairui@hairuinet.com',

        ['574601625@qq.com'], fail_silently=False)


    以上有个问题就是,你配置好了,不能修改。。。网络大部分是这样写的?http://www.hairuinet.com/

    如何写成动态的呢?

    我们可以写一个类,初始化的时候可以将信息保存起来,通过判断是否加密验证去做处理。就可以啦!


    我已经封装好了!直接拿去用吧

     1 #!/usr/bin/env python
     2 # -*- coding=utf-8 -*-
     3 from smtplib import SMTP_SSL,SMTP
     4 from email.header import Header
     5 from email.mime.text import MIMEText
     6 import smtplib
     7 
     8 
     9 class SendMail:
    10 
    11     def __init__(self,template_id,sendname, toname, title, content):
    12         self.mail_host = 'smtp.qq.com'  # 设置服务器
    13         self.mail_user = 'hairui@hairuinet.com'  # 用户名
    14         self.mail_pass = 'hairuinet.com'  # 口令
    15         self.mail_port = 25      # 端口
    16         self.mail_ssh = False     # 是否是加密端口
    17         self.content = content # 邮件内容
    18         self.sendname = sendname
    19         self.toname = toname
    20         self.title = title # 邮件标题
    21         self.encoding = "utf-8" # 邮件编码格式
    22         self.sender = 'member@kylinclub.org' #发件地址
    23         self.receivers = '574601624@qq.com' # 收件箱
    24 
    25     def start(self):
    26         self.msg()
    27         if self.mail_ssh:
    28             self.ssh()
    29         else:
    30             self.pt()
    31 
    32     def msg(self):
    33         self.msg = MIMEText(self.content, "plain", self.encoding)
    34         self.msg["Subject"] = Header(self.title, self.encoding)
    35         self.msg["from"] = self.mail_user
    36         self.msg["to"] = self.toname
    37 
    38     def ssh(self):
    39         smtp = SMTP_SSL(self.mail_host)
    40         smtp.set_debuglevel(0)
    41         smtp.ehlo(self.mail_user)
    42         smtp.login(self.mail_user,self.mail_pass)
    43         smtp.sendmail(self.mail_user, self.receivers, self.msg.as_string())
    44         smtp.quit()
    45 
    46     def pt(self):
    47         server = smtplib.SMTP(self.mail_host, self.mail_port)
    48         server.set_debuglevel(1)
    49         server.login(self.mail_user, self.mail_pass)
    50         server.sendmail(self.mail_user, [self.receivers,], self.msg.as_string())
    51         server.quit()
    52 
    53 if __name__ == '__main__':
    54     obj = SendMail(1,'海瑞网络','海瑞','密码找回','内容:这个海瑞网络发送的测试邮件',)
    55     obj.start()

    本文作者为原著,转载请保留本文链接!http://www.hairuinet.com/

    From Hairui 转载请注明出处!谢谢
  • 相关阅读:
    4K
    4J
    4C
    I2C总线的仲裁机制
    Linux C中strcpy , strncpy , strlcpy 的区别
    Linux下的USB总线驱动(一)
    C/C++ 语言中的表达式求值
    const变量通过指针修改问题
    关于协议栈XDATA,内存溢出的小结
    Ubuntu安装ssh,及失败解决方案
  • 原文地址:https://www.cnblogs.com/hairuipython/p/7205858.html
Copyright © 2011-2022 走看看