zoukankan      html  css  js  c++  java
  • Linux終端一行命令发送邮件

    近期由于经常需要给别人发送邮件,每次都要打开QQ邮箱觉得非常麻烦。想到Linux终端可以自定义命令,加上python可以实现邮件发送功能,于是自己写了一个终端send + 文件地址的命令。

    首先贴上python发送邮件的代码。

    # An automatically send qq_email lib
    # python3
    # author: lunar_ubuntu
    # -*- coding: UTF-8 -*-
    
    import sys
    import smtplib
    from email.mime.text import MIMEText
    from email.mime.application import MIMEApplication
    from email.mime.multipart import MIMEMultipart
    from email.header import Header
    from xml.dom.minidom import parse
    import xml.dom.minidom
    
    dom_tree = xml.dom.minidom.parse("info.xml") # here you need to fill your xml file path
    elem = dom_tree.documentElement
    
    sender = elem.getElementsByTagName("sender")[0]
    sender_acount = sender.getElementsByTagName("sender_acount")[0].childNodes[0].data
    sender_pwd = sender.getElementsByTagName("sender_pwd")[0].childNodes[0].data
    
    recevs = elem.getElementsByTagName("receivers")[0]
    receivers = []
    for re in recevs.getElementsByTagName("receiver_acount"):
        receivers.append(re.childNodes[0].data)
    
    def mail(*file_paths):
        res = True
        message = MIMEMultipart()
        message['From'] = Header("ubuntu",'utf-8')
        message['TO'] = Header("windows", 'utf-8')
        subject = 'ubuntu send files'
        message['Subject'] = Header(subject,'utf-8')
        for path in file_paths[0]:
            #print("path: %s" % path)
            file_name = split_path(path)
            #print("file_name: %s" % file_name)
            attach = MIMEApplication(open(path,'rb').read())
            attach['Content-type'] = 'application/octet-stream'
            attach['Content-Disposition'] = 'attachment; filename="' + file_name + '"'
            message.attach(attach)
    
        server = smtplib.SMTP_SSL("smtp.qq.com", 465)
        server.login(sender_acount, sender_pwd)
        server.sendmail(sender_acount, receivers, message.as_string())
        server.quit()
        return res
    
    def split_path(path):
        return path.split('/')[-1]
    
    if __name__ == '__main__':
        res = mail(sys.argv[1:])
        if res:
            print("successfully send emails!")
        else:
            print("failed to send emails!")
    

    我是通过将邮箱信息写在xml文件里面来读取的,这样方便修改。不用每次都修改源代码。xml也贴一下。

    <info>
    <sender>
        <sender_acount>mail_address</sender_acount>
        <sender_pwd>put your password here</sender_pwd>
    </sender>
    <receivers>
        <receiver_acount>2100125584@qq.com</receiver_acount>
        <receiver_acount>2100125584@qq.com</receiver_acount>
    </receivers>
    </info>
    

    这里面的邮箱密码并不是你登录邮箱的那个密码,而是在邮箱里面的账号设置里获得的第三方登录码。具体可以看一下菜鸟教程的python发送邮件的那个教程。还不懂可以在评论区问我。

    最后修改~/.bashrc文件,加上一句alias send="python3 [文件地址]"。然后保存退出后输入命令sourcr ~/.bashrc是命令生效。然后就可以通过send命令来发送文件啦!

    我愿潇洒如鹰,远离地上宿命
  • 相关阅读:
    Maven依赖
    Maven坐标
    初识Maven POM
    Maven配置
    相似文本文档分析之SimHash算法
    Ubuntu14.10下JDK编译安装详细操作说明
    Ubuntu14.10下Tomcat编译安装 详细操作说明
    ubuntu 14.10 编译安装 Ruby
    ubuntu 14.10 编译安装 Python
    ubuntu 14.10 编译安装 Golang
  • 原文地址:https://www.cnblogs.com/lunar-ubuntu/p/12345855.html
Copyright © 2011-2022 走看看