Linux Shell发邮件笔记

企鹅博客
企鹅博客
企鹅博客
25193
文章
0
评论
2020年10月4日17:52:14 评论 11 views 8237字阅读27分27秒

  ①安装所需软件包 RPM式系统(RedHat、CentOS): yum install -y sendmail yum install -y sharutils wget ftp://ftp.gnu.org/gnu/mailutils/mailutils-2.2.tar.gz tar -xzf mailutils-2.2.tar.gz cd mailutils-2.2 ; ls ./configure make && make install DEB式系统(Debian、Ubuntu): apt-get install sendmail apt-get install mailutils apt-get install sharutils   ②简单说明 DEB系统的apt提供了完整的源,可以直接简单安装;RPM系统中“mailutils”包需要手动下载源码编译安装。“sendmail”提供本地邮件配置和服务管理,“mailutils”提供各种格式和通信协议的邮件支持,“sharutils”提供附件传输。 Ubuntu安装完成后邮件服务(“sendmail”)默认已经启动了,CentOS则可能需要手动启动服务。可以通过查看进程列表确认是否需要手动启动服务。 ps aux | grep sendmail service sendmail start “sendmail”默认配置是支持本地邮件服务,要打开外网通信,需要修改一下配置文件“/etc/mail/sendmail.mc”,找到“127.0.0.1”,修改为“0.0.0.0”。   ③发件示例 普通邮件 echo 邮件正文 | mail -s 邮件主题 -t 收件人 例:echo -e "我是一棵葱\n你是一颗蒜" | mail -s Hello -t …………@…….com 邮件内容来自文件 mail -s subject -t …………@…….com < file 例:mail -s Hello -t …………@…….com < 葱与蒜.txt 附件邮件 uuencode 原文件名 文件附件名 | mail -s subject -t …………@…….com 例:uuencode 葱与蒜.txt 葱与蒜.txt | mail -s subject -t …………@…….com   一个完整例子: (echo -e "我是一棵葱\n" ; echo "你是一颗蒜" ; uuencode 葱.txt 葱.txt ; uuencode 蒜.txt 蒜.txt) | \ mail -s "Hello Gay" -t …………@163.com, …………@qq.com   作为脚本语言,Python也可以非常方便的进行发邮件[附2]。   附1:shell发邮件命令之mail 目前最常用的就是mail命令了。 (sudo apt-get install mailutils) 也可手动下载安装:http://mailutils.org/download.html  简单一行命令实现发送邮件 echo 'content test' | mail -s "title test" -t  [email protected],[email protected],[email protected] From:[email protected]  内容由文件导入 mail -s "sh mail test"  < file1.txt 带上附件  (uuencode file1.txt file1.txt; uuencode file2.txt file2.txt) | mail -s "title test" -t  [email protected]  内容 + 附件 (echo '1111111'; uuencode file1.txt file1.txt; uuencode file2.txt file2.txt) | mail -s "title test" -t  [email protected]  多个内容 +多个 附件 (echo '1111111'; echo '22222222'; uuencode file1.txt file1.txt; uuencode file2.txt file2.txt) | mail -s "title test" -t  [email protected]  Cent OS 系统上:  下载安装mailutils wget ftp://ftp.gnu.org/gnu/mailutils/mailutils-2.2.tar.gz 解压进入 ./configure make && make install yum install -y sendmail yum install sharutils   # 使用带附件功能 service sendmail start  查看sendmail启动没:   ps aux |grep sendmail 测试:echo 'content test' | mail -s "title test"  -t [email protected] 此时发觉只能给公司内部发邮件, 如果需要还能给外面的邮箱(比如我的qq邮箱)发邮件则还需要 vi  /etc/mail/sendmail.mc 找到包含Addr的这一行: DAEMON_OPTIONS(`Family=inet,  Name=MTA-v4, Port=smtp, Addr=127.0.0.1')dnl 修改Addr=0.0.0.0  ,表明可以连接到任何服务器 /etc/mail/sendmail.mc divert(-1)dnl   #-----------------------------------------------------------------------------   # $Sendmail: debproto.mc,v 8.14.4 2014-02-11 13:02:08 cowboy Exp $   #   # Copyright (c) 1998-2010 Richard Nelson.  All Rights Reserved.   #   # cf/debian/sendmail.mc.  Generated from sendmail.mc.in by configure.   #   # sendmail.mc prototype config file for building Sendmail 8.14.4   #   # Note: the .in file supports 8.7.6 - 9.0.0, but the generated   #   file is customized to the version noted above.   #   # This file is used to configure Sendmail for use with Debian systems.   #   # If you modify this file, you will have to regenerate /etc/mail/sendmail.cf   # by running this file through the m4 preprocessor via one of the following:   #   * make   (or make -C /etc/mail)   #   * sendmailconfig   #   * m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf   # The first two options are preferred as they will also update other files   # that depend upon the contents of this file.   #   # The best documentation for this .mc file is:   # /usr/share/doc/sendmail-doc/cf.README.gz   #   #-----------------------------------------------------------------------------   divert(0)dnl   #   #   Copyright (c) 1998-2005 Richard Nelson.  All Rights Reserved.   #   #  This file is used to configure Sendmail for use with Debian systems.   #   define(`_USE_ETC_MAIL_')dnl   include(`/usr/share/sendmail/cf/m4/cf.m4')dnl   VERSIONID(`$Id: sendmail.mc, v 8.14.4-4.1ubuntu1 2014-02-11 13:02:08 cowboy Exp $')   OSTYPE(`debian')dnl   DOMAIN(`debian-mta')dnl   dnl # Items controlled by /etc/mail/sendmail.conf - DO NOT TOUCH HERE   undefine(`confHOST_STATUS_DIRECTORY')dnl#DAEMON_HOSTSTATS=   dnl # Items controlled by /etc/mail/sendmail.conf - DO NOT TOUCH HERE   dnl #   dnl # General defines   dnl #   dnl # SAFE_FILE_ENV: [undefined] If set, sendmail will do a chroot()   dnl #   into this directory before writing files.   dnl #   If *all* your user accounts are under /home then use that   dnl #   instead - it will prevent any writes outside of /home !   dnl #   define(`confSAFE_FILE_ENV', `')dnl   dnl #   dnl # Daemon options - restrict to servicing LOCALHOST ONLY !!!   dnl # Remove `, Addr=' clauses to receive from any interface   dnl # If you want to support IPv6, switch the commented/uncommentd lines   dnl #   FEATURE(`no_default_msa')dnl   dnl DAEMON_OPTIONS(`Family=inet6, Name=MTA-v6, Port=smtp, Addr=::1')dnl   DAEMON_OPTIONS(`Family=inet,  Name=MTA-v4, Port=smtp, Addr=0.0.0.0')dnl   dnl DAEMON_OPTIONS(`Family=inet6, Name=MSP-v6, Port=submission, M=Ea, Addr=::1')dnl   DAEMON_OPTIONS(`Family=inet,  Name=MSP-v4, Port=submission, M=Ea, Addr=127.0.0.1')dnl   dnl #   dnl # Be somewhat anal in what we allow   define(`confPRIVACY_FLAGS',dnl  
`needmailhelo,needexpnhelo,needvrfyhelo,restrictqrun,restrictexpand,nobodyreturn,authwarnings')dnl dnl #   dnl # Define connection throttling and window length   define(`confCONNECTION_RATE_THROTTLE', `15')dnl   define(`confCONNECTION_RATE_WINDOW_SIZE',`10m')dnl   dnl #   dnl # Features   dnl #   dnl # use /etc/mail/local-host-names   FEATURE(`use_cw_file')dnl   dnl #   dnl # The access db is the basis for most of sendmail's checking   FEATURE(`access_db', , `skip')dnl   dnl #   dnl # The greet_pause feature stops some automail bots - but check the   dnl # provided access db for details on excluding localhosts...   FEATURE(`greet_pause', `1000')dnl 1 seconds   dnl #   dnl # Delay_checks allows sender<->recipient checking   FEATURE(`delay_checks', `friend', `n')dnl   dnl #   dnl # If we get too many bad recipients, slow things down...   define(`confBAD_RCPT_THROTTLE',`3')dnl   dnl #   dnl # Stop connections that overflow our concurrent and time connection rates   FEATURE(`conncontrol', `nodelay', `terminate')dnl   FEATURE(`ratecontrol', `nodelay', `terminate')dnl   dnl #   dnl # If you're on a dialup link, you should enable this - so sendmail   dnl # will not bring up the link (it will queue mail for later)   dnl define(`confCON_EXPENSIVE',`True')dnl   dnl #   dnl # Dialup/LAN connection overrides   dnl #   include(`/etc/mail/m4/dialup.m4')dnl   include(`/etc/mail/m4/provider.m4')dnl   dnl #   dnl # Default Mailer setup   MAILER_DEFINITIONS   MAILER(`local')dnl  MAILER(`smtp')dnl   附2:【Python】一键式发邮件小脚本 工作中涉及到给客户定时发邮件,这里提供一个Python小脚本,单独放进一个py小文件,每次用时import一下非��简单方便。 # !/usr/bin/python # -*- coding: utf-8 -*- # # # create_author: 蛙鳜鸡鹳狸猿 # create_time  : 2016-10-10 # NOTICE       : *_*mail config*_* # # import logging import smtplib from email import encoders from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart # # # class obj class Mail(): def __init__(self, msg_from, msg_to, msg_subject, text, path_file, host, port, user, password): self.msg_from    = msg_from         # 发件箱——————"" self.msg_to      = msg_to           # 收件箱——————["",""] self.msg_subject = msg_subject      # 邮件主题————"" self.text        = text             # 邮件正文————"" self.path_file   = path_file        # 邮件附件————[{"path" : "", "name" : ""}, {"path" : "", "name" : ""}] self.host        = host             # 服务器——————"smtp.163.com" self.port        = port             # 端口号——————25 self.user        = user             # 发件箱用户———"" self.password    = password         # 发件箱密码————"" # def __str__(self): tranceback = "An e-mail from \"%s\" to \"%s\" under subject of \"%s\"…………" % (self.msg_from, self.msg_to, self.msg_subject) return tranceback # def csmail(self): msg = MIMEMultipart() try: msg['from']    = self.msg_from msg['to']      = ','.join(self.msg_to)  # because of a BUG of smtplib  msg['subject'] = self.msg_subject msg.attach(MIMEText(self.text)) for i in self.path_file: msg_attach  = MIMEText(open(i['path'], "rb").read(), "base64", "utf-8") attach_name = i['name'] msg_attach.add_header("Content-Disposition", "attachment", filename=attach_name) msg_attach["Content-Type"] = "application/octet-stream" msg.attach(msg_attach) except Exception , e: logging.error("create email under subject \"%s\" failed! Detail: %s" % (self.msg_subject , e)) exit() ser = smtplib.SMTP() try: ser.connect(host=self.host, port=self.port) ser.login(user=self.user, password=self.password) ser.sendmail(msg['from'], self.msg_to, msg.as_string()) ser.quit() except Exception, e: logging.error("send email under subject \"%s\" failed! Detail: %s" % (self.msg_subject , e)) exit() 以下是一个小示例。 MSG = {"msg_from"    : "……@163.com", "msg_to"      : ["……@qq.com", "……@…….com"], "msg_subject" : "Hello收件人", "text"        : "测试邮件(generate by Python2.7.12)", "path_file"   : [{"path" : moudle_base.rount() + "附件1" + moudle_base.curtime() + ".xlsx", "name" : "附件1" + moudle_base.curtime() + ".xlsx"}, {"path" : moudle_base.rount() + "附件2" + moudle_base.curtime() + ".xlsx", "name" : "附件2" + moudle_base.curtime() + ".xlsx"}], "host"        : "smtp.163.com", "port"        : 25, "user"        : "……", "password"    : "……" } mail = Mail(msg_from=MSG["msg_from"], msg_to=MSG["msg_to"], msg_subject=MSG["msg_subject"], text=MSG["text"], path_file=MSG["path_file"], host=MSG["host"], port=MSG["port"], user=MSG["user"], password=MSG["password"]) print(mail) mail.csmail() 也就是把所有的参数放进一个大字典,直接取用就可以了,非常一目了然。

继续阅读
Git 分支的创建、合并、管理和删除 Linux教程

Git 分支的创建、合并、管理和删除

几乎每一种版本控制系统都以某种形式支持分支。使用分支意味着你可以从开发主线上分离开来,然后在不影响主线的同时继续工作。在很多版本控制系统中,这是个昂贵的过程,常常需要创建一个源代码目录的完整副本,对大...
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: