linux腳本實現自動發送和收取郵件的設置方法
2019-10-26 18:36:59
供稿:網友
1. 命令行模式下的發送郵件
1.1 安裝sendemail
2.2 使用sendemail和舉例
2. 命令行模式下的收取郵件
2.1 安裝getmail4
2.2 配置getmail4和簡單舉例
2.3 用munpack從郵件中抽取附件
1. 命令行模式下的發送郵件
1.1 安裝sendemail
在Ubuntu下可以用新立得軟件包管理器搜索安裝,或者在終端運行:
sudo apt-get install sendemail
建議在安裝前先安裝另外兩個包:libio-socket-ssl-perl, libnet-ssleay-perl
2.2 使用sendemail和舉例
如果你想用你的郵箱christ@gmail.com發送郵件到buddha@qq.com,在終端輸入:
sendemail -s smtp.gmail.com -f christ@gmail.com -t buddha@qq.com -u hello -m "A hello from Christans to buddhists via gmail" -xu christ -xp password -o tls=auto
解釋:
-s smtp.gmail.com 指定服務器域名,郵件發送一般通過SMTP協議實現,其域名一般為smtp.***.com,比如qq郵箱的服務器為smtp.qq.com,163郵箱則為smtp.163.com
-f christ@gmail.com指定發送郵箱地址
-t buddha@qq.com 指定目的郵箱地址
-u hello 郵件標題
-m "A hello from Christans to buddhists via gmail" 郵件正文,較長的正文可以先存在文本文件中,不妨命名為mail.txt,然后換用-o message-file=mail.txt
-xu christ 指定郵箱用戶名,即郵箱地址@之前的部分
-xp password 指定發送郵箱的密碼
-o tls=auto 加密方式在none, tls, ssl中自動選擇
如果想在郵件中粘帖附件
-a attachment_file1 attachment_file2 attachment_file3
2. 命令行模式下的收取郵件
通過郵件客戶端收取email郵件主要有兩種方式:POP3和IMAP,郵件客戶端通過POP3下載服務器上的郵件,但是IMAP默認只下載郵件的主題。基于命令行方式自動化的意義,采用POP3更合適。
2.1 安裝getmail4
在Ubuntu下可以用新立得軟件包管理器搜索安裝,或者
sudo apt-get install getmail4
2.2 配置getmail4和簡單舉例
安裝完畢后在終端執行一下命令
代碼如下:
cd ~
mkdir .getmail
cd .getmail
mkdir maildir
cd maildir
mkdir new cur tmp
后在 .getmail/下建立一個配置文件,如果這個配置文件是為收取buddha@qq.com這個郵箱配置的,不妨命名為getmailrc.buddha,然后編輯該文件,#后是注釋:
代碼如下:
# This is a configuration file for buddha@qq.com
[retriever]
type = SimplePOP3Retriever
server = pop.qq.com #如果是gmail則改為pop.gmail.com
username = budda
password = password
[destination]
type = Maildir
path = ~/.getmail/maildir/ #就是剛才在~/.getmail/建立的目錄,注意該目錄下一定要有new,cur,tmp這三個子目錄
[options]
read_all = False #只接受以前沒有收取的郵件,如果改成True則收取郵箱中所有郵件
delete = False #下載郵件后不在服務器上刪除該郵件,如果改成True則刪除