Linux Shell script 使用 mutt 發送信件

 Mutt是一個在終端下運行的文本模式電子郵件客戶端,它支援POP3、IMAP和SMTP協議,允許用戶通過命令行界面管理和發送電子郵件。

以下是Mutt的一些主要特點和常見用法:

文本模式界面: Mutt是一個基於文本的電子郵件客戶端,它在終端中運行,沒有圖形用戶界面。這使得它適用於伺服器環境或喜歡使用純文本界面的用戶。

支援多個郵箱: Mutt允許你同時管理多個郵箱帳戶,包括POP3和IMAP帳戶。你可以在一個會話中訪問多個郵箱。

強大的配置選項: Mutt提供了豐富的配置選項,你可以自定義設置以滿足你的需求。這使得Mutt非常靈活,可以適應各種用戶需求。

郵件過濾和搜索: Mutt支持郵件過濾和搜索功能,可以幫助你快速查找和整理郵件。

自定義快捷鍵: 你可以自定義快捷鍵來執行特定的操作,使得Mutt更符合你的工作流程。

總之,Mutt是一個強大的電子郵件客戶端,特別適合那些喜歡在終端中處理郵件的用戶,並且需要高度自定義和配置的功能。

mutt 命令行参数和選項的列

-a: 附加一個或多個文件到郵件中。
-b: 密件副本郵件給指定的郵件地址,多個地址之間用逗號分隔。
-c: 副本郵件給指定的郵件地址,多個地址之間用逗號分隔。
-d: 指定郵箱文件的目錄。
-e: 啟用郵件編輯模式,允許你在發送郵件之前編輯郵件內容。
-F: 指定郵件的發件人地址。
-i : 使用指定文件中的內容作為郵件正文。
-q: 靜音模式,發送郵件時不顯示確認提示。
-H:使用 HTML 格式發送郵件。
-s: 指定郵件主題。
-p: 指定郵箱的密碼。
-S: 指定郵件伺服器的地址。
-P: 指定郵件伺服器的端口。
-x: 啟用郵件的加密選項。
-X:使用指定的代理伺服器。
-y :使用指定的配置文件。
-r: 指定郵件的回覆地址。
-L:指定日誌文件的路徑。
-v: 啟用詳細的日誌輸出。
-B:指定郵件的字符編碼。
-R:指定接收郵件的郵箱文件。
-T:使用指定的模板文件創建新郵件。
-o:設置配置選項,例如 -o my_hdr='From: My Name <myemail@example.com>'。

===

在 Ubuntu 或 Debian 上安装 Mutt
sudo apt update
sudo apt install mutt

在 CentOS 或 RHEL 上安装 Mutt
sudo yum install mutt

在 macOS 上安装 Mutt
brew install mutt

===

Mutt 的設定文件 

~/.muttrc
如果沒有需要自己建立
以下設定依照自己需求擷取

===

# 基本設置
set imap_user = "your_email@example.com"
set imap_pass = "your_password"
set smtp_url = "smtps://your_email@example.com@smtp.example.com"
set smtp_pass = "your_smtp_password"
set from = "Your Name <your_email@example.com>"
set realname = "Your Name"

# 郵件伺服器連接設置
set ssl_starttls = yes
set ssl_force_tls = yes
set imap_check_subscribed = yes

# 郵件伺服器和端口
set smtp_url = "smtps://smtp.example.com:465"
set imap_url = "imaps://imap.example.com"

# 身份驗證方式
set smtp_authenticators = "login"

# 自動收信間隔
set mail_check = 180

# 保存郵件的文件夾
set postponed = "+Drafts"

# 簽名文件
set signature = "~/.mutt/signature.txt"

# 快捷鍵
macro index <F2> ":toggle trash\n" "切換垃圾郵件文件夾"

# 顏色主題
color normal white black
color attachment brightred black

# 郵件文件夾
mailboxes +INBOX +Sent +Drafts +Junk +Archive

# 启用自动查找外部信任的PGP密钥
set pgp_autosign
set pgp_autoinline

# 設定預設編碼
set charset = "utf-8"

# 設定編輯器
set editor = "vim"

# 配置多語言支持
set charset="utf-8"
set locale="zh_TW.utf8"
set send_charset="utf-8"

# 預設瀏覽模式
set pager_index_lines = 10
set pager_context = 3

# 預設排序
set sort = threads
set sort_aux = reverse-last-date-received

# 使用 GPG 來加密和解密郵件
set crypt_use_gpgme = yes
set crypt_opportunistic_encrypt = yes
set crypt_verify_sig = yes

# 忽略郵件大小限制
set ignore_size = yes

# 忽略郵件主題中缺少主題的郵件
set nosubject = "unignored"

# 設定郵件伺服器超時時間:
set timeout = 60

# 設定外部瀏覽器:
set browser = "firefox %s"

# 設定自動回覆地址:
set reply_to = "your_reply_address@example.com"

# 設定外部郵件查看器:
set pager = "less"

# 自動查詢郵件:
set spoolfile = "+INBOX"
set mail_check = 180

# 設定摘要視圖:
set pager_index_lines = 5
set pager_context = 3

# 設定郵件列表標籤:
set status_format = "─ [%r] %f %S ─ %U"

# 設定郵件列表排序:
set sort = "threads"
set sort_aux = "last-date-received"

# 啟用垃圾郵件過濾:
set pipe_decode = yes
set pipe_spam = yes
set pipe_trust_msgid = yes
set spoolfile = "|/usr/bin/spamc"

# 設定 Mutt 主題:
color header brightcyan black
color quoted green black
color quoted1 green black

# 使用 NNTP(新聞組):
set nntp_servers = "news.example.com"
set nntp_username = "your_username"
set nntp_pass = "your_password"

# 設定附件保存路徑:
set attach_save = "~/Downloads"

# 設定郵件列表顯示內容:
index_format="%4C %Z %{%b %d} %-15.15L (%4l) %?M?(+%M)&(+%-M)? %-5.5s %3S %3T %s"

# 自定義主題篩選器:
macro index / "<change-folder>=[Gmail]/Starred<enter>" "查看已標記的郵件"

# 自定義簽名文件
send-hook . 'set signature="~/.mutt/signature-general.txt"'
send-hook 'your_email@example.com' 'set signature="~/.mutt/signature-work.txt"'

===

Gmail 設定


===

# Gmail IMAP 設定
set imap_user = "your_email@gmail.com"
set imap_pass = "your_password"
set folder = "imaps://imap.gmail.com"
set spoolfile = "+INBOX"
set postponed = "+[Gmail]/Drafts"
set record = "+[Gmail]/Sent Mail"
set trash = "+[Gmail]/Trash"

# Gmail SMTP 設定
set smtp_url = "smtps://your_email@gmail.com@smtp.gmail.com:465"
set smtp_pass = "your_smtp_password"

# 預設寄件人信息
set from = "Your Name <your_email@gmail.com>"
set realname = "Your Name"

# 使用 SSL/TLS 連接
set ssl_starttls = yes
set ssl_force_tls = yes

# 設定簽名文件
set signature = "~/.mutt/signature.txt"

# 啟用顏色主題
color normal white black
color attachment brightred black

# 默認排序方式
set sort = "threads"
set sort_aux = "last-date-received"

# 自動查詢新郵件
set mail_check = 180

# 自動選擇郵件伺服器
set imap_check_subscribed = yes

# 設定編碼和語言
set charset = "utf-8"
set locale = "en_US.utf8"

# 启用 GPG 加密支持
set pgp_use_gpgme = yes
set pgp_autoinline = yes
set pgp_verify_sig = yes

# 忽略郵件大小限制
set ignore_size = yes

# 忽略主題中缺少主題的郵件
set nosubject = "unignored"

# 郵件文件夾
mailboxes +INBOX +[Gmail]/Sent Mail +[Gmail]/Drafts +[Gmail]/Trash

===

自訂郵件伺服器


===

# 自定義郵件伺服器 IMAP 設定
set imap_user = "your_email@example.com"
set imap_pass = "your_password"
set folder = "imaps://imap.example.com"
set spoolfile = "+INBOX"
set postponed = "+Drafts"
set record = "+Sent"
set trash = "+Trash"

# 自定義郵件伺服器 SMTP 設定
set smtp_url = "smtps://your_email@example.com@smtp.example.com:465"
set smtp_pass = "your_smtp_password"

# 預設寄件人信息
set from = "Your Name <your_email@example.com>"
set realname = "Your Name"

# 使用 SSL/TLS 連接
set ssl_starttls = yes
set ssl_force_tls = yes

# 設定簽名文件
set signature = "~/.mutt/signature.txt"

# 啟用顏色主題
color normal white black
color attachment brightred black

# 默認排序方式
set sort = "threads"
set sort_aux = "last-date-received"

# 自動查詢新郵件
set mail_check = 180

# 自動選擇郵件伺服器
set imap_check_subscribed = yes

# 設定編碼和語言
set charset = "utf-8"
set locale = "en_US.utf8"

# 启用 GPG 加密支持(如果需要的話)
set pgp_use_gpgme = yes
set pgp_autoinline = yes
set pgp_verify_sig = yes

# 忽略郵件大小限制
set ignore_size = yes

# 忽略主題中缺少主題的郵件
set nosubject = "unignored"

# 郵件文件夾
mailboxes +INBOX +Sent +Drafts +Trash

===

mutt 發送測試郵件:收件人


===

#!/bin/bash

# 郵件接收者的郵件地址
recipient="recipient@example.com"

# 郵件主題
subject="測試郵件"

# 郵件內容
message="這是一封測試郵件,用於測試郵件發送。"

# 使用 mutt 命令發送郵件
echo "$message" | mutt -s "$subject" "$recipient"

===

發送測試郵件:

mutt 主要收件人、副本


===
#!/bin/bash

# 主要收件人的郵件地址
recipient="recipient@example.com"

# 副本收件人的郵件地址
cc_recipient="cc_recipient@example.com"

# 郵件主題
subject="測試郵件"

# 郵件內容
message="這是一封測試郵件,用於測試郵件發送。"

# 使用 mutt 命令發送郵件,將主要收件人和副本收件人指定為參數
echo "$message" | mutt -s "$subject" -b "$cc_recipient" -- "$recipient"

===

發送測試信件:

mutt 主要收件人、副本、密件副本


===
#!/bin/bash

# 主要收件人的郵件地址
recipient="recipient@example.com"

# 副本收件人的郵件地址
cc_recipient="cc_recipient@example.com"

# 密件副本收件人的郵件地址
bcc_recipient="bcc_recipient@example.com"

# 郵件主題
subject="測試郵件"

# 郵件內容
message="這是一封測試郵件,用於測試郵件發送。"

# 使用 mutt 命令發送郵件,將主要收件人、副本收件人和密件副本收件人指定為參數
echo "$message" | mutt -s "$subject" -b "$cc_recipient" -b "$bcc_recipient" -- "$recipient"

===

發送測試信件:

mutt 主要收件人、副本、密件副本,加上收件人姓名


===
#!/bin/bash

# 主要收件人的姓名和郵件地址
recipient="Recipient Name <recipient@example.com>"

# 副本收件人的姓名和郵件地址
cc_recipient="CC Recipient <cc_recipient@example.com>"

# 密件副本收件人的姓名和郵件地址
bcc_recipient="BCC Recipient <bcc_recipient@example.com>"

# 郵件主題
subject="測試郵件"

# 郵件內容
message="這是一封測試郵件,用於測試郵件發送。"

# 使用 mutt 命令發送郵件,將姓名和郵件地址指定為參數
echo "$message" | mutt -s "$subject" -b "$cc_recipient" -b "$bcc_recipient" -- "$recipient"

===

mutt 測試夾帶檔案


===

#!/bin/bash

# 主要收件人的姓名和郵件地址
recipient="Recipient Name <recipient@example.com>"

# 郵件主題
subject="測試郵件"

# 郵件內容
message="這是一封包含附件的測試郵件。"

# 附件文件的路徑
attachment="/path/to/your/attachment.pdf"

# 使用 mutt 命令發送郵件,並附加附件
mutt -s "$subject" -a "$attachment" -- "$recipient" <<< "$message"

===

測試發信,mutt 發信給多個使用者


===

#!/bin/bash

# 主要收件人的郵件地址
recipient="recipient1@example.com, recipient2@example.com"

# 郵件主題
subject="測試郵件"

# 郵件內容
message="這是一封測試郵件,用於測試郵件發送給多個收件人。"

# 附件文件的路徑
attachment="/path/to/your/attachment.pdf"

# 使用 mutt 命令發送郵件,並附加附件
mutt -s "$subject" -a "$attachment" -- "$recipient" <<< "$message"
===

留言

這個網誌中的熱門文章

Routeros中TX與RX的定義

RouterOS 啟用 (NAT)IP地址轉換

Windows 7 輸入法不見