zoukankan      html  css  js  c++  java
  • Python 使用 Postfix 发送邮件

    最近在做一个监控程序,需要用邮件发送告警。以前是使用注册的免费邮来发送,但是这样不免有很多限制,而且有时还会当作恶意登录,帐号异常等,还不让登录邮箱了。利用Postfix提供邮件SMTP服务,可以很自由的发送邮件,任意定义发送者的邮箱地址。感觉都可以用来恶作剧,用别人的邮箱来发邮件呢。

    安装 Postfix

    如果是 Ubuntu,请使用

    sudo apt-get install postfix
    

    如果是 Centos,请使用

    yum install postfix
    

    接下来需要进行一些配置

    选择Internet Site
    这里选择Internet Site

    设置FQDN
    然后让设置FQDN,但是使用默认的机器名即可,这里不是很确定。

    使用 Python 发送邮件

    使用这里给出的完美例子

    #! /usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import smtplib
    from email.mime.multipart import MIMEMultipart
    from email.mime.base import MIMEBase
    from email.mime.text import MIMEText
    from email.utils import COMMASPACE, formatdate
    from email import Encoders
    import os
    
    def send_mail(mail_to, mail_from, subject, text, files, server="localhost"):
        assert type(mail_to) == list
        assert type(files) == list
    
        msg = MIMEMultipart()
        msg['From'] = mail_from
        msg['To'] = COMMASPACE.join(mail_to)
        msg['Date'] = formatdate(localtime=True)
        msg['Subject'] = subject
    
        # 如果 text 是html,则需要设置 _subtype='html'
        # 默认情况下 _subtype='plain',即纯文本
        # msg.attach(MIMEText(text, _subtype='html', _charset='utf-8'))
        msg.attach(MIMEText(text, _charset='utf-8'))
    
        for f in files:
            part = MIMEBase('application', "octet-stream")
            part.set_payload(open(f, "rb").read())
            Encoders.encode_base64(part)
            part.add_header('Content-Disposition', 'attachment; filename="%s"'
                            % os.path.basename(f))
            msg.attach(part)
    
        smtp = smtplib.SMTP(server)
        smtp.sendmail(mail_from, mail_to, msg.as_string())
        smtp.close()
    
    
    if __name__ == '__main__':
        # Example:
        # 这里可以任意定制发送者的邮箱地址
        send_mail(['your_name <your_name@gmail.com>'], 'MonitorBase <notify@monitor.base>', 'Hello Python!', 'Say hello to Python! :)', [])
    

    最后把使用 SMTP 登录邮箱发送邮件的方法也附上

    #! /usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import smtplib
    import time, traceback, sys, os
    from email.mime.text import MIMEText
    
    def send_mail(mail_from, password, mail_to, subject, content):
    
        handle = smtplib.SMTP('smtp.163.com', 25)
        handle.login(mail_from, password)
        time_str = time.strftime('%Y-%m-%d %X', time.localtime(time.time()))
        msg = '<html><body>' + content + "<br><br><span style='color:#999;font-size:"
                            + "10px;font-family:Verdana;'>" 
                            + time_str + " by " + mail_from + "</span>"+'</body></html>'
        send_msg = MIMEText(msg, 'html', 'utf-8')
        send_msg['Subject'] = subject
        handle.sendmail(mail_from, mail_to, send_msg.as_string())
        handle.close()
    
    if __name__ == '__main__':
        send_mail('your_name@163.com', 'your_password', 'Hello Python!', 'Say hello to Python! :)')
    
  • 相关阅读:
    python网上开发执行环境
    bt5全称是Back Track five,是继BT3,BT4之后的最新版,这是一个linux环境的便携系统,可以放到U盘或者硬盘中启动,对本身硬盘没有影响,无需在本地安装。
    ubuntu安装mysql的步骤和配置总结
    Django 安装MySQLdb模块
    OpenCV3编程入门笔记(一)
    论文笔记---Deblurring Shaken and Partially Saturated Images
    win7 64位操作系统 电脑桌面出现this computer is being attacked的窗口
    论文笔记(一)---翻译 Rich feature hierarchies for accurate object detection and semantic segmentation
    OpenCV3计算机视觉Python语言实现笔记(五)
    OpenCV3计算机视觉Python语言实现笔记(四)
  • 原文地址:https://www.cnblogs.com/restran/p/4288912.html
Copyright © 2011-2022 走看看