macos下使用mail发送邮件

有一些定时脚本在执行,但是每天需要我到了公司之后去查看一下我的脚本的执行结果,就想着怎么能让bash执行完之后,发送短信到我的手机,但是我又没有什么短信接口,为了这个极少的使用去买个服务也不值得,就想到了使用移动运营商的sms网关,向其发送邮件,同时运营商会想你的手机发送短信,我是移动,移动的sms网关是@139.com,联通电信需要自己查网关

mail这个命令比较简单,你可以指定你的发件箱,当然你如果不指定的也会使用你本地默认的一个邮箱,首先安装msmtp

sudo brew install msmtp

我是mac,使用brew安装,会自动给你安装依赖,linux请用yum,ubuntu使用apt-get

安装完成之后要进行设置,看一下msmtp的手册,下面直接贴上了

# Example for a user configuration file ~/.msmtprc
       #
       #  This file focusses on TLS and authentication. Features not used here
       include
       # logging, timeouts, SOCKS proxies,  TLS  parameters,  Delivery  Status
       Notification
       # (DSN) settings, and more.

       # Set default values for all following accounts.
       defaults

       # Use the mail submission port 587 instead of the SMTP port 25.
       port 587

       # Always use TLS.
       tls on

       #  Set a list of trusted CAs for TLS. The default is to use system set-
       tings, but
       # you can select your own file.
       #tls_trust_file /etc/ssl/certs/ca-certificates.crt

       # If you select your own file, you should  also  use  the  tls_crl_file
       command to
       #  check for revoked certificates, but unfortunately getting revocation
       lists and
       # keeping them up to date is not straightforward.
       #tls_crl_file ~/.tls-crls

       # A freemail service
       account freemail

       # Host name of the SMTP server
       host smtp.freemail.example

       #  As  an  alternative  to  tls_trust_file/tls_crl_file,  you  can  use
       tls_fingerprint
       #  to pin a single certificate. You have to update the fingerprint when
       the
       # server certificate changes, but an attacker  cannot  trick  you  into
       accepting
       # a fraudulent certificate. Get the fingerprint with
       #      $      msmtp      --serverinfo     --tls     --tls-certcheck=off
       --host=smtp.freemail.example
       #tls_fingerprint  00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF:00:11
       :22:33

       # Envelope-from address
       from joe_smith@freemail.example

       #  Authentication. The password is given using one of five methods, see
       below.
       auth on
       user joe.smith

       # Password method 1: Add the password to the system  keyring,  and  let
       msmtp get
       #  it  automatically.  To set the keyring password using Gnome's libse-
       cret:
       # $ secret-tool store --label=msmtp \
       #   host smtp.freemail.example \
       #   service smtp \
       #   user joe.smith

       # Password method 2: Store the password in an encrypted file, and  tell
msmtp
       #  which command to use to decrypt it. This is usually used with GnuPG,
       as in
       # this example. Usually gpg-agent will  ask  once  for  the  decryption
       password.
       passwordeval gpg2 --no-tty -q -d ~/.msmtp-password.gpg

       #  Password method 3: Store the password directly in this file. Usually
       it is not
       # a good idea to store passwords in plain text files. If you do it any-
       way, at
       # least make sure that this file can only be read by yourself.
       #password secret123

       #  Password  method  4:  Store the password in ~/.netrc. This method is
       probably not
       # relevant anymore.

       # Password method 5: Do not specify a password. Msmtp will then  prompt
       you for
       # it. This means you need to be able to type into a terminal when msmtp
       runs.

       # A second mail address at the same freemail service
       account freemail2 : freemail
       from joey@freemail.example

       # The SMTP server of your ISP
       account isp
       host mail.isp.example
       from smithjoe@isp.example
       auth on
       user 12345

       # Set a default account
       account default : freemail

在用户的home下创建.msmtprc文件,照着上面的配置,将邮件名称及密码换成自己的,选项不是所有都需要填写的,例如,我就没有使用tls_trust_file,这个看你的邮箱。

配置好之后使用命令

msmtp -s

看一下是否设置好了,下面贴上我的设置好的一部分

SMTP server at smtp..com ([xx.xx.xx.xx]), port 587:
    smtp.xx.com xx Server
TLS session parameters:
    (TLS1.2)-(RSA)-(AES-256-CBC)-(SHA1)
TLS certificate information:
    Owner:
        Common Name: pop.xx.com
        Country: CN
    Issuer:
        Common Name: GeoTrust RSA CA 2018
        Organization: DigiCert Inc
        Organizational unit: www.xx.com
        Country: US

类似这种的就是设置正确了

如果你报错

msmtprc: contains secrets and therefore must have no more than user read/write permissions

说明你权限过大了,执行

sudo chmod 0600 ~/.msmtprc

再次执行mstmp -s确认成功后,在你的home目录下创建.mailrc,文件内容如下:

#替换成你的mstmp的路径
 set sendmail=/usr/local/bin/msmtp

让mail使用我们指定的邮箱去发送邮件,如果不这么指定的话,会使用mail默认的本地邮箱发送,有可能会发送失败,例如给139邮箱发送邮件的时候会报:

I'm sorry to have to inform you that your message could not be delivered to one or more recipients. It's attached below. For further assistance, please send mail to postmaster. If you do so, please include this problem report. You can delete your own text from the attached returned message.

这时候使用mail命令就可以给你的移动手机号码发送短信了(ps:实际上是向邮箱发送邮件)

mail -s '邮件title' 188xxxxxxxx@139.com

回车后,默认下一行你输入的是正文,如果是这种方式的,你在键入正文之后要使用ctrl+D结束,也可以使用管道

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值