邮件服务器

时间:2019-11-19 09:22:05 阅读:54次
邮件服务器


https://www.jianshu.com/p/502f23489dd4


防火墙

firewall-cmd --zone=public --add-port=25/tcp --permanent

firewall-cmd --zone=public --add-port=110/tcp --permanent

firewall-cmd --zone=public --add-port=465/tcp --permanent

firewall-cmd --zone=public --add-port=587/tcp --permanent

firewall-cmd --zone=public --add-port=995/tcp --permanent

firewall-cmd  --reload


域名解析:

A 记录

记录类型:A

主机记录:@

记录值: 服务器IP


MX 记录

记录类型:MX

主机记录:@

记录值: mail.domain.com


TXT记录

记录类型:TXT

主机记录:@

记录值: v=spf1 include:mail.domain.com -all


查询解析是否生效

nslookup -qt=mx mail.domain.com


#生成证书

cd /etc/pki/tls/certs

make server.key

openssl rsa -in server.key -out server.key

make  server.csr

openssl x509 -in server.csr -out server.crt -req -signkey server.key -days 3650

chmod 400 server.*


yum -y install postfix dovecot bind-utils telnet mailx


vi /etc/postfix/main.cf

postconf -e 'myhostname = mail.domain.com'

postconf -e 'myorigin = $mydomain'

postconf -e 'mynetworks = 0.0.0.0/0'

postconf -e 'inet_interfaces = all'

postconf -e 'inet_protocols = all'

postconf -e 'mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain'

postconf -e 'home_mailbox = Maildir/'

postconf -e 'smtpd_sasl_type = dovecot'

postconf -e 'smtpd_sasl_path = private/auth'

postconf -e 'smtpd_sasl_auth_enable = yes'

postconf -e 'broken_sasl_auth_clients = yes'

postconf -e 'smtpd_sasl_authenticated_header = yes'

postconf -e 'smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination'

postconf -e 'smtpd_use_tls = yes'

postconf -e 'smtpd_tls_cert_file = /etc/pki/tls/certs/server.crt'

postconf -e 'smtpd_tls_key_file = /etc/pki/tls/certs/server.key'


vi /etc/postfix/master.cf


smtps     inet  n       -       n       -       -       smtpd  -o syslog_name=postfix/smtps  -o smtpd_tls_wrappermode=yes  -o smtpd_sasl_auth_enable=yes


vi /etc/dovecot/dovecot.conf

protocols = imap pop3 lmtp

listen = *, :: 



vi /etc/dovecot/conf.d/10-auth.conf 

disable_plaintext_auth = no


vi /etc/dovecot/conf.d/10-ssl.conf

ssl_cert = </etc/pki/tls/certs/server.crt

ssl_key = </etc/pki/tls/certs/server.key


vi /etc/dovecot/conf.d/10-mail.conf 

mail_location = Maildir:~/Maildir


vi /etc/dovecot/conf.d/10-master.conf 

service auth {

  unix_listener /var/spool/postfix/private/auth {

    mode = 0666

  }

}



systemctl start postfix

systemctl start dovecot


useradd notreply -s /sbin/nologin

passwd  notreply


#可登录用户

su notreply

mkdir -p ~/mail/.imap/INBOX

#非可登录用户

mkdir /home/notreply 

mkdir /home/notreply/mail/.imap/INBOX

chown notreply:notreply -R mail


#测试

echo "Mail Content" | mail -s "Mail Subject" test@qq.com



#465端口 587端口

yum -y install cyrus-sasl

systemctl start saslauthd


vi /etc/services

smtp            465/tcp         mail

smtp            465/udp         mail

smtp            587/tcp         mail

smtp            587/udp         mail


vi /etc/sysconfig/saslauthd

MECH=shadow


vi /etc/sasl2/smtpd.conf

log_level: 3

saslauthd_path:/var/run/saslauthd/mux


testsaslauthd -u notreply -p 'notreply用户密码'


systemctl restart saslauthd




#测试发送

$mail = new PHPMailer();

try {

    $mail=new PHPMailer();//建立邮件发送类

    $mail->IsSMTP();//使用SMTP方式发送 设置设置邮件的字符编码,若不指定,则为'UTF-8

    $mail->Host= 'mail.domain.com';//您的企业邮局域名

    $mail->SMTPAuth = false;

    $mail->SMTPAutoTLS = false; 

    $mail->Port = 25; 

    $mail->Username= 'notreply@domain.com';//邮局用户名(请填写完整的email地址)

    $mail->Password= 'notreply密码';//邮局密码

    $mail->From= 'notreply@henan28.com'; //邮件发送者email地址

    $mail->FromName= 'notreply@henan28.com';//邮件发送者名称

    $mail->AddAddress('test@qq.com');// 收件人邮箱,收件人姓名

    $mail->IsHTML(true); // set email format to HTML //是否使用HTML格式

    $mail->CharSet= 'UTF-8';

    $mail->Subject="=?UTF-8?B?".base64_encode("Hello")."?=";

    $mail->Body= "Hello World"; //邮件内容

    $mail->AltBody = "这是一封HTML格式的电子邮件。"; //附加信息,可以省略

    $mail->Send();

    if(!empty($mail->ErrorInfo)) throw new Exception($mail->ErrorInfo);


    echo "Message has been send";

} catch (Exception $e) {

    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";

}


扫描二维码关注程序员爱笔记,接收更多资讯

评论

快速评论