วันพฤหัสบดีที่ 10 มิถุนายน พ.ศ. 2564

การติดตั้ง Postfix ใน Ubuntu 20.04 ปี 2021

อีกครั้งกับการที่ต้องมา Setup ตัว Postfix ซึ่งไม่ได้ทำบ่อยแล้วไม่มีความรู้เรื่องนี้เอาสะเลย ครั้งนี้จะต้องขึ้น Server Mail ให้ระบบอื่น ๆ เข้ามาเชื่อมต่อเพื่อส่งเมลออกไปยังที่อื่น ๆ ต่อไป แม้จะเคยทำมาแล้วแต่ก็ลืมไปหมด ครั้งนี้ได้โอกาสเลยเอามาเขียยลง Blog ไว้จะถ้ามีโอกาสได้ทำอีก ก็จะได้กลับมาหาอ่านไปกันเร็ว ๆ ได้ ในบทความนี้เราจะมาเริ่มแบบไม่รู้อะไรค่อย ๆ กันแก้ Error ต่าง ๆ ไปด้วย ไปเริ่มทำกันเลย

OS ที่ใช้จะเป็น Ubuntu 20.04 เพื่อให้ใช้ตัวนี้กันไปยาว ๆ ให้เราทำการติดตั้งให้เรียบร้อยตั้ง Date, Local, Network และทำการ Upgrade Package ให้เรียบร้อย
จากนั้นทำการติดตั้ง postfix อีเมล Server และ mailutils เพื่อใช้ทดสอบส่งอีเมลออกจาก Server
# Install postfix and mailutils
sudo apt install postfix mailutils
เมื่อติดตั้งเรียบร้อยแล้วให้ทำการ configuration ตัว postfix โดยใช้คำสั่ง dpkg-reconfigure postfix เพื่อช่วยให้เรา Config ง่ายขึ้น และผมจะใช้ค่า Config Default ตั้งตนไปก่อน เดียวมาทำความเข้าใจแต่ละส่วนที่หลัง ค่าอยู่หลัง : นั้นใช้ Default ระบบมาให้แต่แรกเดียวเราจะกลับมาแก้ที่ละอัน และตั้งค่าที่ configuration แล้วก็ให้ทำการ restart postfix อีก 1 รอบ
# Configure postfix
sudo dpkg-reconfigure postfix

# Config Value
# General type of mail configuration: Internet Site
# System mail name: enoti
# Root and postmaster mail recipient: sarankon
# Other destinations to accept mail for (blank for none): enoti, enoti, localhost.localdomain, , localhost
# Force synchronous updates on mail queue?: <No>
# Local networks: 127.0.0.0/8 [::ffff:127.0.0.0]/24 [::1]/128
# Mailbox size limit (bytes): 0
# Local address extension character: +
# Internet protocols to use: all

sudo systemctl restart postfix
ลองส่งอีเมลเลยว่าได้ไหม โดยใช้ mailutils ในการส่ง
# Test Send Mail
echo "Postfix Test" | mail -s "Subject Test" youremail@gmail.com
ถ้าเจอ Error : mail: cannot send message: Process exited with a non-zero status
ให้ทำการ dpkg-reconfigure postfix แล้ว restart postfix ดู


รอสักครุ่หลังจากส่งเมลออกให้ทำการตัวสอบ Error ใน Mail Box ของเราโดยทำการพิมพ์ mail ลงใน terminal
# Check Error
mail
"/var/mail/sarankon": 1 message 1 new
>N   1 Mail Delivery Syst Fri Jun 11 09:43  80/2876  Undelivered Mail Returned to Sender
? # กด Enter เพื่อเข้าไปดู

Return-Path: <>
X-Original-To: sarankon@enoti
Delivered-To: sarankon@enoti
Received: by enoti (Postfix)
        id D5FAD121D9C; Fri, 11 Jun 2021 09:43:10 +0700 (+07)
Date: Fri, 11 Jun 2021 09:43:10 +0700 (+07)
From: MAILER-DAEMON@enoti (Mail Delivery System)
Subject: Undelivered Mail Returned to Sender
To: sarankon@enoti
Auto-Submitted: auto-replied
MIME-Version: 1.0
Content-Type: multipart/report; report-type=delivery-status;
        boundary="29167121D9B.1623379390/enoti"
Content-Transfer-Encoding: 8bit
Message-Id: >20210611024310.D5FAD121D9C@enoti<

This is a MIME-encapsulated message.

--29167121D9B.1623379390/enoti
Content-Description: Notification
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

This is the mail system at host enoti.

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.

                   The mail system

<youremail@gmail.com>: host gmail-smtp-in.l.google.com[2404:6800:4003:c04::1a]
    said: 550-5.7.1 [ ... ] Our system has
    detected that 550-5.7.1 this message does not meet IPv6 sending guidelines
    regarding PTR 550-5.7.1 records and authentication. Please review 550-5.7.1
    https://support.google.com/mail/?p=IPv6AuthError for more information 550
    5.7.1 . j8si12221366pjj.51 - gsmtp (in reply to end of DATA command)
ส่งไม่ผ่าน ให้เราทำการ review 550-5.7.1 ที่ Link: https://support.google.com/mail/?p=IPv6AuthError
Fix IPv6 authorization errors
An IPv6 authorization error could mean the PTR record for the sending server isn’t using IPv6.
If you use an email service provider, confirm they’re using an IPv6 PTR record.

Here's an example of an IPv6 authorization error:
550-5.7.1: Message does not meet IPv6 sending guidelines regarding PTR records and authentication.
ให้เราทำการ dpkg-reconfigure postfix อีกครั้งและในข้อสุดท้ายให้เปลี่ยนจาก all เป็น ipv4 ก็คือจะใช้เฉพาะ ipv4 ในการใช้งาน internet แล้วทำการ restart postfix แล้วส่งอีเมลออกอีกครั้ง

ลองส่งอีเมลอีกครั้ง โดยใช้ mailutils ในการส่ง
# Test Send Mail
echo "Postfix Test" | mail -s "Subject Test" youremail@gmail.com

ในครั้งนี้อีเมลจะส่งออกมาได้แล้ว แต่จะเข้าไปอยู่ใน Spam เราจะต้องไปแก้ไขเพื่อไม่ให้อยู่ใน Spam และก็เจอข้อสังเกตอีก 3 จุดคือ Name, Username และ Hostname ติดมาด้วยก็จะแก้ตรงนี้ไปด้วย

ลองส่งอีเมลอีกครั้ง แต่เปลี่ยนชื่อคนส่งดู โดยใช้ mailutils ในการส่ง
# Test Send Mail
echo "Postfix Test" | mail -s "Subject Test" youremail@gmail.com aFrom:youremail@gmail.com
เราจะสามารถส่งออกเป็นชื่อใครก็ได้ แต่จะยังอยู่ใน Spam



ตั้งชื่อ Hostname ให้ถูกต้อง ให้ทำการเปลี่ยน hostname ในไฟล์ /etc/hosts, /etc/hostname และใช้คำสั่ง dpkg-reconfigure postfix เพื่อเปลี่ยน แล้วทำการสั่ง reboot และส่งอีเมลอีกครั้งจะเห็นว่าเมลไม่เข้า Spam แล้วแต่จะมีเครื่องหมาย No encryption ขึ้นมา

แก้ไขโดยการใช้ Certbot แล้วทำการสร้างใบรับรองขึ้นมาใหม่แล้วเอาไปใส่ใน Postfix
# Install certbot
sudo apt install certbot

# Create certification
sudo certbot certonly --standalone --rsa-key-size 4096 --agree-tos --preferred-challenges http -d [yourdomain]
smtpd_tls_cert_file=/etc/letsencrypt/live/[yourdomain]/fullchain.pem
smtpd_tls_key_file=/etc/letsencrypt/live/[yourdomain]/privkey.pem
smtpd_tls_security_level=may

smtp_tls_CApath=/etc/ssl/certs
smtp_tls_security_level=may
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

เสร็จแล้วลองทดสอบส่งเมลออกอีกครั้งจะพบว่าได้แล้ว

อ่านรายละเอียดที่ การสร้างใบรับรอง อ่านเพิ่มเติม (Step 5)/
SPF Record อ่านเพิ่มเติม





การส่งเมลโดยใช้ TELNET จากเครื่องอื่น
# TELNET ไปที่เครื่อง Mail Server 192.168.1.105 Port 25
TELNET 192.168.1.105 25

Trying 192.168.0.110...
telnet: Unable to connect to remote host: Connection refused
sarankon@host:~$ telnet 192.168.0.110 25
Trying 192.168.0.110...
Connected to 192.168.0.110.
Escape character is '^]'.
220 email.localdomain ESMTP Postfix (Ubuntu)

EHLO localhost

250-enoti.localdomain
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-DSN
250-SMTPUTF8
250 CHUNKING

MAIL FROM: youremail@gmail.com
250 2.1.0 Ok
RCPT TO: youremail@gmail.com
454 4.7.1 <youremail@gmail.com>: Relay access denied

ไม่มีความคิดเห็น:

แสดงความคิดเห็น