linux指令碼實現自動發送和收取郵件的設定方法

來源:互聯網
上載者:User

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則刪除

# configuration file ends here
編輯好了之後在終端運行:
getmail --rcfile=getmailrc.buddha

getmail會自動收取郵件,下載的郵件會儲存在~/.getmail/maildir/new/下。

2.3 用munpack從郵件中抽取附件

郵件內文和附件是作為一個整體檔案被儲存的,附件以MIME格式附著在整體檔案最後,必須用程式抽取出來。
在Ubuntu下可以用新立得軟體包管理器搜尋安裝mpack,或者 sudo apt-get install mpack
在終端中運行:munpack mail_file
程式會自動識別附件並抽取出來。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.