Ubuntu中sendmail的安裝、配置

來源:互聯網
上載者:User

Ubuntu中sendmail的安裝、配置
因為項目需要一個郵件伺服器功能,用已有的企業郵箱又有各種限制,就來搗鼓了下和這個相關的一些東西。一般是有好幾個選擇,比如Postfix,sendmail,qmail,第一個我之前用過,但是項目需求只有發郵件,也不知怎的就選擇了sendmail,事實證明還是不要作,廢話不說,結合自己的一些經驗總結一下,希望能讓大家少踩坑。
一、安裝


必裝:
sudo apt-get install sendmail
sudo apt-get install sendmail-cf
sudo apt-get install mailutils

可選:

squirrelmail //提供webmail
spamassassin //提供郵件過濾
mailman //提供郵件清單支援
dovecot // 提供IMAP和POP內送郵件伺服器守護進程
sharutils // 提供帶附件的功能

終端輸入命令:ps aux |grep sendmail

輸出如下:

root 20978 0.0 0.3 8300 1940 ? Ss 06:34 0:00 sendmail: MTA: accepting connections

root 21711 0.0 0.1 3008 776 pts/0 S+ 06:51 0:00 grep sendmail

說明sendmail 已經安裝成功並啟動了

二、配置

(1)/etc/mail/sendmail.mc

(a)配置sendmail可以串連任何伺服器

DAEMON_OPTIONS(`Family=inet, Name=MTA-v4, Port=smtp, Addr=127.0.0.1')dnl 修改Addr=0.0.0.0

(b)添加auth配置(注意字串前面不是單引號,而是鍵盤中的波浪號鍵上的符號)

TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl

define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl

(c)啟用access_db和trust_users

添加FEATURE(use_ct_file)

修改FEATURE(`access_db', , `skip')dnl為FEATURE(`access_db')dnl

(2)/etc/mail/access

將需要使用該郵件伺服器的IP按照以下格式加入到該檔案中:Connect:your_ip RELAY

然後更新access_db:makemap hash /etc/mail/access.db < /etc/mail/access

(3)/etc/mail/trusted-users

將需要使用該郵件伺服器的IP按照以下格式加入到該檔案中:一行一個ip

 

最後產生新的設定檔:

cd /etc/mail
mv sendmail.cf sendmail.cf~ //做一個備份
m4 sendmail.mc > sendmail.cf //>的左右有空格,提示錯誤沒有安裝sendm 為了防止其他郵件伺服器將郵件投入垃圾郵件,可以添加spf記錄,添加方式是在網域名稱DNS管理裡添加一條TXT記錄,內容類別似於

“v=spf1 ip4:67.202.107.106 -all”,同時修改/etc/host,在127.0.0.1 localhost之後將郵件伺服器網域名稱及主機名稱加進去

三、測試

(1)本機測試

 

1.一般的郵件: mail test@126.com Cc 編輯抄送對象,Subject:郵件主題,輸入斷行符號,郵件內文後,按Ctrl-D結束

 

2.快速發送方式: echo “郵件內文” | mail -s 郵件主題 test@126.com

 

3.以檔案內容作為郵件內文來發送: mail -s test test@126.com < test.txt

 

4.發送帶附件的郵件: uuencode 附件名稱 附件顯示名稱 | mail -s 郵件主題 發送地址

 

例如: uuencode test.txt test.txt | mail -s Test test@126.com

 

(2)其他機器測試

 

# -*- coding: UTF-8 -*-

 

import smtplib

 

from email.mime.text import MIMEText

 

from email.mime.multipart import MIMEMultipart

 

if __name__ == "__main__":

 

to = "to email addr"

 

me = "your email addr"

 

content = "test"

 

msg = MIMEMultipart('alternative')

 

msg['Subject'] = "test"

 

msg['From'] = me

 

msg['To'] = to

 

part2 = MIMEText(content, 'html',_charset='utf-8')

 

msg.attach(part2)

 

try:

 

server = smtplib.SMTP(your email server)

 

server.sendmail(me, to, msg.as_string())

 

server.quit()

 

except Exception, e:

 

print e

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.