海底暴风雪

富在术数不在劳身,利在局势不在力耕

搭建邮件服务器

搭建环境

Debian12,postfix
首先安装postfix服务,使用aptyum,安装完成后配置选择Internet Site填入自己的域名。

编辑/et.postfix/main.cf,

mydomain = yourdomain.com

查看并编辑hostname

如果hostname是想要的就不用编辑,如果不是,使用下面的命令编辑

// 查看hostname
hostname
//修改hostname
sudo hostnamectl set-hostname mail.yourdomain.com

发送邮件测试

使用mailsendmail进行测试,

echo "This is a test email." | mail -s "Test Subject" your_email@example.com

这回以系统用户名为用户名,发送一个邮件到指定的邮箱。

使用sendmail可以灵活的设置发件人

sendmail -f your_email@example.com recipient@example.com << EOF
Subject: Test Email
This is the body of the test email.
EOF

使用python发送验证

import smtplib
from email.mime.text import MIMEText

sender = 'your_email@example.com'
receiver = 'recipient@example.com'
password = 'your_password'  # 如果使用身份验证
smtp_server = 'smtp.example.com'
port = 587  # 根据您的 SMTP 服务器可能不同

msg = MIMEText('This is the body of the test email.')
msg['Subject'] = 'Test Email'
msg['From'] = sender
msg['To'] = receiver

with smtplib.SMTP(smtp_server, port) as server:
    server.starttls()
    server.login(sender, password)
    server.sendmail(sender, [receiver], msg.as_string())

搜索

文章分类