Web Email Interfaces อย่าง Gmail บ่อยครั้ง Inefficient และ Insecure ทั้งหมด Require คุณ Online ตลอด และไม่ Support Features อย่าง Mass Tagging และ Filtering อย่างไรก็ตาม หากคุณใช้ Emacs Edit Text อยู่แล้ว คุณสามารถใช้มัน Manage Email ได้
Modern Email Clients และ Emacs
แม้มี Offline Email Clients อย่าง Thunderbird และ Evolution โปรแกรมเหล่านี้บ่อยครั้ง Large, ไม่มีทุกอย่างที่คุณต้องการ หรือไม่ Combine Well กับ Applications อื่นๆ
ตัวอย่าง Evolution Accept Email และ Calendar ของคุณ แตไม่ Deal กับ Document Processing, Newsgroups, และ To-do Lists สร้าง Disjointed User Experience ที่อาจ Lead ไป Issues มากขึ้นในอนาคต
ด้านอื่น Emacs Operate ใน Notion ของ Interoperability ภายใน Emacs ทุกอย่าง Accessed และ Manipulated ผ่าน Design Language เดียวกัน Keybindings สำหรับ Basic Actions เหมือนกัน อีกทั้ง Packages ก็ Strictly Create และ Output Text ที่ใช้เป็น Input ใน Packages อื่นได้
ตัวอย่าง หากคุณใช้ gnus อ่าน Newsgroups และ Org Mode จัดการ Notes คุณสามารถ Direct Newsgroup Article ไป Org Buffer เพื่อ Annotate หรือ Save เป็น Inspiration
เพิ่มเติม หากคุณใช้ EMMS จัดการ Music มัน Output Title ของ Current Song เป็น Text คุณสามารถ Link ไป Blog Post เพื่อ Highlight ว่าคุณกำลังฟังเพลงอะไรขณะเขียน
Emacs เป็น Email Client
Emacs ให้คุณ Integrate และ Save Email ใน Org Buffer เพื่อ Annotate หรือ Save เป็น To-do Item ใน Calendar
คุณยังทำตรงข้ามได้: เขียน Article ใน Org Buffer และ Send ภายใน Emacs เป็น Email ให้ใครบางคน
สิ่งนี้ Possible ด้วย 2 Programs: Offlineimap และ notmuch.
- Offlineimap เป็น Helper Program ที่ Fetch Email จาก Remote Server และ Save บน Disk Server อาจเป็น Self-hosted หรือ Email Service อย่าง Gmail หรือ Protonmail
- Notmuch อีกด้านหนึ่ง เป็น Extremely Simple Application ที่ Tag และ Display Mail มัน Maintain Database ของ Mail ทั้งหมดที่ได้รับ และ Tag ตามที่คุณตั้ง แล้ว Display Emails ใน Emacs ตาม Tags นั้น
Installing Offlineimap และ notmuch
ติดตั้ง 2 Applications นี้ใน Debian และ Ubuntu:
sudo apt install offlineimap notmuch
ใน Arch Linux:
sudo pacman -Syu offlineimap notmuch
ใน Fedora:
sudo dnf install offlineimap notmuch
หลังจากนั้น ติดตั้ง Notmuch Package ที่มาด้วยใน Emacs ปัจจุบัน Available ใน MELPA Repository คุณอาจต้อง Enable มันก่อนใน Emacs Configuration
เพื่อทำนั้น ให้เพิ่ม Lisp Lines ต่อไปนี้ใน init.el file:
(require 'package) (add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/")) (package-initialize)
เพื่อ Install Notmuch Package กด Alt + X และพิมพ์ package-install Emacs จะถามชื่อ Package ที่ต้องการ Install พิมพ์ "notmuch"
ติดตั้งแล้ว Package นี้จะเป็น Front End ของ Notmuch Tagging Program
Setting up Offlineimap
เมื่อทำเสร็จ คุณสามารถ Configure Offlineimap ได้ นี่คือ Simple Process ที่เกี่ยวข้องกับ Single File หลัก: ".offlineimaprc"
โดย Default Config File นี้อ่านจาก Home Directory เพื่อสร้างมัน ให้รันคำสั่ง:
touch /home/$USER/.offlineimaprc
เสร็จแล้ว เปิดไฟล์นี้ด้วย Text Editor โปรด
Configuration สำหรับ Offlineimap เขียนใน INI Format ตัวอย่าง .offlineimaprc สำหรับ Single IMAP Account อาจดูแบบนี้:
[general] accounts = imapaccount [Account imapaccount] localrepository = thismachine remoterepository = thatimap [Repository thismachine] type = Maildir localfolders = /home/$USER/mail/youremail@domain.com [Repository thatimap] type = IMAP remotehost = imap.domain.com remoteuser = youremail@domain.com remotepass = your_password_goes_here ssl = yes sslcacertfile = /your/ca/cert/path/here
- Category general บอก Offlineimap ว่าคุณตั้ง Single Account สำหรับ Installation นี้
- Category Account ระบุ Sources ที่ Account นั้นได้ Email รวม Scripts ที่อยาก Run เมื่อ Offlineimap Fetch Mail
- Category Repository จะบอก Offlineimap Configuration สำหรับ Account ที่ตั้ง Settings ที่นี่ Up To Whether หรือไม่คุณใช้ Gmail
Email Repositories
Offlineimap ทำงานโดย Save Structure ของ Remote Email Directory ใน Local Computer นี้ Allow Program Imitate Remote IMAP Server และ Allow Email Clients อย่าง Notmuch อ่าน Mail โดยไม่ต้อง Online
Repository Category ต่างกันว่าคุณ Setup Local หรือ Remote Mailbox เพื่อสร้าง Local Mailbox คุณแค่ต้อง Set Type เป็น "Maildir" จะบอก Offlineimap ว่า Repository นี้จะ Point ไป Location ใน Local Machine
[Repository thismachine] type = Maildir localfolders = /home/$USER/mail/youremail@domain.com
ในทางตรงข้าม Setup Remote Repository อาจ Involved เล็กน้อย แต่เมื่อเข้าใจแต่ละ Value ใน Config File หมายถึงอะไร Remote Setup ควร Very Simple
มาดู Repository Block Example อีกครั้ง:
[Repository thatimap] type = IMAP remotehost = imap.domain.com remoteuser = youremail@domain.com remotepass = your_password_goes_here ssl = yes sslcacertfile = /your/ca/cert/path/here
- Setting type ระบุ Kind ของ Server ที่อยากให้ Offlineimap Connect ในที่นี้มี 2 Options: Gmail สำหรับ Google Mail และ IMAP สำหรับ Non-Google Accounts
- Setting remotehost ตั้ง Address ของ IMAP Server ที่ Offlineimap Connect
- remoteuser และ remotepass คือที่ให้ User Credentials
- Setting ssl เป็น "yes" จะบอก Offlineimap ว่าอยาก Establish Encrypted Connection ไป IMAP Server
- Option sslcacertfile ระบุ Local SSL Certificate ของคุณ Offlineimap จะใช้ Verify Connection กับ IMAP Server
Connecting to Gmail
เหมือนอธิบายข้างบน Offlineimap ยัง Provide Special Type สำหรับ Connect Gmail Accounts เนื่องจาก Google Require Additional Information เมื่อ Connect Mail Service
เพื่อ Connect Gmail ด้วย Offlineimap คุณแค่ต้องเปลี่ยน Type ของ Local และ Remote Repositories ตั้ง Local Repository เป็น "GmailMaildir" และ Remote เป็น "Gmail"
ตัวอย่าง .offlineimaprc File ที่ Connect Gmail Service อาจดูแบบนี้:
[general] accounts = gmailaccount [Account gmailaccount] localrepository = localgmail remoterepository = remotegmail [Repository localgmail] type = GmailMaildir localfolders = /home/$USER/mail/youremail@gmail.com [Repository remotegmail] type = Gmail maxconnections=1 remotehost = imap.gmail.com remoteuser = youremail@gmail.com remotepass = your_password_goes_here ssl = yes sslcacertfile = /your/ca/cert/path/here
Establishing an SSL Connection
สิ่งต่อไปคือ Configure Offlineimap ให้ Connect ผ่าน SSL ทำได้โดย Indicate Path ไป System-wide SSL Certificates
Location ของ SSL Certificates ขึ้นกับ System ที่รัน อย่างไรก็ตาม ใน Debian และ Ubuntu อยู่ที่ Path นี้:
ls /etc/ssl/certs/ca-certificates.crt
คุณสามารถ Run Offlineimap ใน Terminal เพื่อ Get All Mail จาก Remote IMAP Server ได้แล้ว
Setting up notmuch
จากนั้น ดำเนินไป Notmuch Setup ตามชื่อ ไม่มีอะไรมากที่ Config
เมื่อคุณมี IMAP Directory แล้ว คุณสามารถ Run Notmuch ใน Command Line นี่จะ Start Configuration Script ที่ถามเกี่ยว Specific Email Setup
ด้วยนั้น Notmuch จะสร้าง Config File ใน Home Directory คุณ Check ได้โดยรันคำสั่ง:
less /home/$USER/.notmuch-config
ตั้ง Number ของ Options ที่บอก Notmuch หา New Mail ที่ไหน คุณยัง Set Tags ที่ไม่อยาก Indexed เมื่อ Notmuch Search ผ่าน Database
ตัวอย่าง คุณตั้งให้ Notmuch ไม่ Search Mail ที่ Tagged ว่า Junk:
[search] exclude_tags = junk
Email Tagging ด้วย notmuch
เหมือนที่สังเกต คุณไม่ Set Email Tags ภายใน Notmuch Config File เพราะ Notmuch Tag Email โดยตรงจาก Command Line
นี่ Allow คุณ Flexible กับการใช้ Notmuch ตัวอย่าง คุณ Include Notmuch Tagging Command ใน Script ที่ Periodically Run เป็น Cronjob
เพื่อเริ่มต้น แต่คุณต้อง Initialize Notmuch's Database โดยรันคำสั่ง:
notmuch new
จะอ่าน Config File และสร้าง Database ตาม Sensible Defaults จากนั้นคุณใช้ Notmuch's Tagging Commands Tag Incoming และ Existing Email ได้
Notmuch's Tagging Syntax
Tagging Syntax ใน Notmuch Highly Intuitive General Form ดูแบบนี้:
notmuch tag [+|-]label header:header-property (tag:current-tag)
- Command tag บอก Notmuch ว่า Arguments ต่อไปจะใช้สร้าง Tagging Rule ใน Mailbox
- Option label ระบุว่าคุณจะ Add Label (+) หรือ Remove Existing (-) ตัวอย่าง Incoming Mail ทั้งหมด Tagged "unread" โดย Default คุณ Set Command เป็น -unread เพื่อ Remove Unread Tag
- Option header บอก Notmuch ให้ Apply Label Based On Email Header ที่ตั้ง ตัวอย่าง ใช้ Header "From:" Filter Mail Based On Who They Are From
- Option tag เป็น Optional Argument ที่ Limit Application ของ Tagging Rule ไป Specific Tag ตัวอย่าง Set ให้ Notmuch Apply Label ก็ต่อเมื่อ Email นั้นมี Unread Tag
Viewing Your Email ใน Emacs
คุณสามารถ View Emails ภายใน Emacs ได้ ทำได้โดยกด Alt + X และพิมพ์ notmuch จะ Load Front-end Package สำหรับ Notmuch ใน Emacs
Landing Screen จะ Display Standard Tags บางตัวโดย Default อย่างไรก็ตาม คุณ View Custom Tags ได้โดยคลิก Button "[show]" ถัดจาก "All Tags" คุณยัง Access Tags ได้โดยกด S และพิมพ์ "is:tag_name" ใน Command Buffer
Standard Emacs Movement และ Editing Keys ทำงานใน Notmuch ได้ อีกทั้งมี Added Features เยอะสำหรับ Filter Email ดีกว่า
ตัวอย่าง Trigger "Tree-style" View โดยกด Shift + Z ขณะ View Specific Tag นี้ Useful เมื่อ Subscribe Mailing List เพราะ Display Emails ใน Threads ที่ Follow ได้ง่าย
Setting up Emacs เพื่อ Send Email
ตอนนี้คุณมี Working Email Directory อ่านใน Emacs ได้ แตคุณยังต้อง Setup Means ส่ง Mail โชคดี มัน Surprisingly Easy ภายใน Emacs
เพื่อ Enable Email Support คุณเพิ่ม Lisp Lines นี้ใน init.el file:
(setq mail-user-agent 'message-user-agent) (setq message-send-mail-function 'smtpmail-send-it smtpmail-stream-type 'starttls smtpmail-smtp-server "mail.domain.com" smtpmail-smtp-service 587)
- mail-user-agent Set User-agent Variable ใช้ Emacs' message-user-agent Identify Outgoing Emails มาจาก Emacs Allow Communicate กับ Mail Servers อื่น
- message-send-mail-function บอก Emacs ใช้ Built-in
smtpmailPackage ส่ง Emails ให้ Proper - ในนั้น smtpmail-stream-type ระบุ Kind ของ Connection ที่ Emacs ใช้ Connect Remote Mail Server Mail Servers ส่วนใหญ่ใช้ SSL/TLS หรือ STARTTLS
- smtpmail-smtp-server ตั้ง Address ของ SMTP Server ที่อยาก Connect
- smtpmail-smtp-service ตั้ง Port ที่ Emacs จะใช้ส่ง SMTP Packets ถ้า SSL/TLS ใส่ 465 ถ้า STARTTLS ใส่ 587
Setting up Email Authentication Using .authinfo
คุณต้อง Allow Emacs ส่ง Email ผ่าน Mail Account ทำได้โดย Append Email Credentials ใน .authinfo
.authinfo File คือ Hidden File ที่ Store User Credentials เมื่อ Login Remote Services ส่วนใหญ่ ไม่มา Default ใน Linux Installation แตคุณสร้างไฟล์นี้ได้โดยรัน:
touch /home/$USER/.authinfo
One Important Thing: ไฟล์นี้มี Sensitive Information อย่าง Username และ Password ดังนั้นคุณต้อง Secure Access Permissions ให้แค่คุณ Read/Write ได้ ทำได้โดยรัน:
chmod 600 /home/$USER/.authinfo
จากนั้น Edit .authinfo ให้มี Mail Account Credentials General Syntax ของ .authinfo ดูแบบนี้:
machine mail.domain.com login username@domain.com port 587 password mypasswordis123
- Variable machine บอก Emacs ว่าคุณ Connect Different Machine หรือ Server
- Domain Name บอกว่า นี่คือ Address ของ Machine ที่อยาก Connect
- Field login คือที่ตั้ง Email Address
- Option port ตั้ง Specific Port ที่ Emacs Connect ควรเหมือน Port Number ใน init.el
- Variable password มี Password ของ Email Account
Reload Emacs เพื่อ Apply New Settings
Sending Your First Email ใน Emacs
ด้วยนั้น ส่ง Email จาก Emacs ง่ายมาก ทำได้โดยกด Ctrl + X, M หรือ กด M ขณะใน Notmuch Buffer
Keybindings นี้ Execute composemail Command สร้าง Message Buffer ด้วย Empty Heading ให้คุณ Type Email
เสร็จแล้ว กด Ctrl + C, Ctrl + C ส่ง Email แรกจาก Emacs
ขอแสดงความยินดี! คุณ Setup Emacs เป็น Email Client สำเร็จ อีกทั้งตอนนี้คุณมี Basic Understanding ของ IMAP และ SMTP Servers ทำงานยังไง รวมถึง Basics ของ Email Tagging System Setup
ถ้าพูดคุยทั้งนี้ทำให้คุณสนใจอะไรอีกที่ทำได้กับ Emacs Check Out 5 Useful Packages สำหรับ Extensible Text Editor นี้
Frequently Asked Questions
1. สามารถ Auto-tag Emails ได้ไหมหลัง Run Offlineimap?
ทำได้โดยสร้าง Notmuch Post-sync Hook ใน Offlineimap Insert Variable ใต้ Account Category ชื่อ "postsynchook":
... [Account imapaccount] localrepository = thismachine remoterepository = thatimap postsynchook = /path/to/your/script.sh ...
Variable นี้ควรมี File Path ไป Executable Script ที่มี Notmuch Commands ตั้ง Tags
ตัวอย่าง Simple Post-sync Script Remove Unread Tag จาก Incoming Mailing List Emails และ Tag อย่างเหมาะสม:
#!/bin/sh notmuch new notmuch tag -inbox -unread +mailing-list from:mailing-list or to:mailing-list@domain.com tag:inbox notmuch tag -inbox -unread +mailinglist-cmd from:mailing-list-cmd@domain.com tag:inbox
2. เขียน Email แตตัดสินใจไม่ส่ง Discard Email ใน Emacs ยังไง?
Discard Email ใน Emacs ได้โดยกด Ctrl + C, Ctrl + D ขณะใน Message Buffer จะ Tag Email ว่า Discarded Draft และ Emacs Delete หลัง Period Of Time
3. สามารถ Modify Email Tags ภายใน Notmuch ใน Emacs ได้ไหม?
ได้! ทำได้โดยกด Shift + = ขณะใน Notmuch Buffer นี่ Open Small Command Buffer ให้คุณ Indicate Tags ที่อยาก Add หรือ Remove จาก Email เฉพาะ