跳至主要内容

linux 发邮件

https://linuxhint.com/bash_script_send_email/


https://www.linuxtechi.com/install-configure-postfix-mailserver-centos-8/

https://support.itrsgroup.com/hc/en-us/articles/360020253193-Sending-outgoing-notifications-via-SMTP-relay


https://www.mail-tester.com/

https://www.linuxtechi.com/log-email-subject-maillog/

https://community.spiceworks.com/linux?source=navbar-subnav


By default postfix only capture ‘From’ and ‘To’ details in the log file (/var/log/maillog).



如何在CentOS 上安装和配置Postfix邮件服务器

Postfix是免费的开源MTA(邮件传输代理),用于在Linux系统上路由或传递电子邮件

设置:
操作系统:CentOS 7服务器
IP地址:192.168.43.128
主机名:server1.admin.info(确保域名指向服务器的IP)

在继续进行操作之前,还请确保不存在其他MTA(例如Sendmail),因为这将导致与Postfix配置冲突。例如,要删除Sendmail,请运行以下命令
# yum remove sendmail

设置主机名并更新/etc/hosts文件
使用下面的hostnamectl命令在系统上设置主机名
# hostnamectl set-hostname server1.admin.info
# exec bash

此外,您需要在/etc/hosts文件中添加系统的主机名和IP条目
# vi /etc/hosts 
192.168.43.128 server1.admin.info
保存并退出文件

安装Postfix邮件服务器
在验证系统上没有其他MTA正在运行之后,通过执行以下命令安装Postfix
# yum install postfix

启动并启用Postfix服务
成功安装Postfix后,通过运行以下命令启动并启用Postfix服务
# systemctl start postfix
# systemctl enable postfix
要检查Postfix状态,请运行以下systemctl命令
# systemctl status postfix


我们已经验证了Postfix已启动并正在运行。接下来,我们将配置Postfix以将电子邮件本地发送到我们的服务器。

安装mailx电子邮件客户端
在配置Postfix服务器之前,我们需要安装mailx功能,要安装mailx,请运行以下命令
# yum install mailx

配置Postfix邮件服务器
Postfix的配置文件位于/etc/postfix/main.cf中。我们需要对配置文件进行一些更改,因此请使用您喜欢的文本编辑器将其打开。
# sudo vi /etc/postfix/main.cf

更改以下几行
myhostname = server1.admin.info
mydomain = admin.info
myorigin = $mydomain
## Uncomment comment ##
inet_interfaces = all
## Comment ##
#inet_interfaces = localhost
## Change to all ##
inet_protocols = all
## Comment ##
#mydestination = $myhostname, localhost.$mydomain, localhost
## Uncomment ##
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
## Uncomment and add IP range ##
mynetworks = 192.168.43.0/24, 127.0.0.0/8
## Uncomment ##
home_mailbox = Maildir/

校验更改
# cat /etc/postfix/main.cf | grep -Env '^$|#'

完成后,保存并退出配置文件。重新启动后缀服务以使更改生效
# systemctl restart postfix

测试Postfix邮件服务器
测试我们的配置是否有效,首先,创建一个测试用户
# useradd postfixuser 
# passwd postfixuser

接下来,运行以下命令以将电子邮件从beijing本地用户发送给另一个用户“postfixuser ”
# telnet localhost smtp
or
# telnet localhost 25

如果未安装telnet服务,则可以使用以下命令进行安装
# dnf install telnet -y

如前所述运行命令时,应获得如下所示的输出
[root@server1 ~]# telnet localhost smtp
Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 server1.admin.info ESMTP Postfix

以上确认与postfix邮件服务器的连接正常。接下来,键入命令:
# ehlo localhost
输出将是这样的
250-server1.admin.info
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN

接下来,运行以橙色突出显示的命令,例如“mail from”,“rcpt to”,data,最后键入quit
mail from:<beijing>
250 2.1.0 Ok
rcpt to:<postfixuser>
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
hello,welcom to my mailserver(Postfix)
.
250 2.0.0 Ok: queued as D1DCA813CE6
quit
221 2.0.0 Bye
Connection closed by foreign host.

完整的telnet命令可以将电子邮件从本地用户“beijing”发送到另一个本地用户“postfixuser”

如果一切都按计划进行,则您应该能够查看在新用户的主目录中发送的电子邮件
[root@server1 new]# ls /home/postfixuser/Maildir/new
1576549565.Vfd04I200a0adM774839.server1.admin.info

要阅读电子邮件,只需使用cat命令,如下所示
# cat /home/postfixuser/Maildir/new/1576549565.Vfd04I200a0adM774839.server1.admin.info

Postfix邮件服务器日志
Postfix邮件服务器邮件日志存储在文件“/var/log/maillog”中,使用以下命令查看实时日志
# tail -f /var/log/maillog

退出用Ctrl+C

保护Postfix邮件服务器
始终建议确保客户端和后缀服务器之间的通信安全,这可以使用SSL证书来实现,这些证书可以来自受信任的权威机构或“自签名”证书。在本教程中,我们将使用openssl命令生成用于后缀的自签名证书
我假设openssl已经安装在您的系统上,如果未安装,请使用以下yum命令
# yum install openssl -y


使用下面的openssl命令生成私钥和CSR(证书签名请求)
# openssl req -nodes -newkey rsa:2048 -keyout mail.key -out mail.csr
Generating a 2048 bit RSA private key
.....................................+++
.....................................................................+++
writing new private key to 'mail.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Shang Hai
Locality Name (eg, city) [Default City]:Shang Hai
Organization Name (eg, company) [Default Company Ltd]:IT
Organizational Unit Name (eg, section) []:IT
Common Name (eg, your name or your server's hostname) []:server1.admin.info
Email Address []:info@admin.info

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

现在,使用以下openssl命令生成自签名证书,
# openssl x509 -req -days 365 -in mail.csr -signkey mail.key -out mail.crt
Signature ok
subject=/C=CN/ST=Shang Hai/L=Shang Hai/O=IT/OU=IT/CN=server1.admin.info/emailAddress=info@admin.info
Getting Private key


现在将私钥和证书文件复制到/etc/postfix目录
# cp mail.key mail.crt /etc/postfix

在后缀配置文件中添加私钥和证书文件的路径
# vi /etc/postfix/main.cf
………
smtpd_use_tls = yes
smtpd_tls_cert_file = /etc/postfix/mail.crt
smtpd_tls_key_file = /etc/postfix/mail.key
smtpd_tls_security_level = may
………


重新启动postfix服务以使上述更改生效
systemctl restart postfix

让我们尝试使用mailx客户端将电子邮件发送到内部本地域和外部域
发送本地内部电子邮件
# echo "test email" | mailx -s "Test email from Postfix MailServer" -r shanghai@admin.info postfixuser@admin.info
使用以下方法检查并阅读电子邮件
[root@server1 new]# cd /home/postfixuser/Maildir/new/
[root@server1 new]# ll
总用量 20
-rw-------. 1 postfixuser postfixuser  434 12月 17 10:26 1576549565.Vfd04I200a0adM774839.server1.admin.info
-rw-------. 1 postfixuser postfixuser  561 12月 17 10:48 1576550892.Vfd04I200a0aeM465920.server1.admin.info
-rw-r--r--. 1 root        root        1306 12月 17 10:39 mail.crt
-rw-r--r--. 1 root        root        1054 12月 17 10:38 mail.csr
-rw-r--r--. 1 root        root        1704 12月 17 10:38 mail.key
[root@server1 new]# cat 1576550892.Vfd04I200a0aeM465920.server1.admin.info 
Return-Path: <shanghai@admin.info>
X-Original-To: postfixuser@admin.info
Delivered-To: postfixuser@admin.info
Received: by server1.admin.info (Postfix, from userid 0)
        id 60BFC813CE6; Tue, 17 Dec 2019 10:48:12 +0800 (CST)
Date: Tue, 17 Dec 2019 10:48:12 +0800
From: shanghai@admin.info
To: postfixuser@admin.info
Subject: Test email from Postfix MailServer
Message-ID: <5df841ec.OIQwHU4o+XUCrKk0%shanghai@admin.info>
User-Agent: Heirloom mailx 12.5 7/5/10
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

test email


发送电子邮件到外部域
# echo "External Test email" | mailx -s "Postfix MailServer" -r info@admin.info jk.d@aisino.sh.cn
# echo "External Test email" | mailx -s "Postfix MailServer" -r infomaster@admin.info jk.d@aisino.sh.cn
echo "External Test email" | mailx -s "Postfix MailServer"  jk.d@aisino.sh.cn
注册发QQ邮箱要加白名单,发件人不填则默认为root@admin.info



检查Postfix邮件队列
使用mailq命令列出队列中的邮件
# mailq
Mail queue is empty

# 查看邮件日志
cat /var/log/maillog





评论

此博客中的热门博文

Mongo 入门

https://pymongo.readthedocs.io/en/stable/tutorial.html https://www.mongodb.com/languages/python https://zhuanlan.zhihu.com/p/51171906 https://www.runoob.com/python3/python-mongodb.html https://blog.baoshuo.ren/post/luogu-spider/ https://hub.docker.com/_/mongo 安装 MongoDB $ docker search mongo 启动一个mongo服务器实例 $ docker run --name some-mongo -d mongo:tag some-mongo是您要分配给容器的名称,tag是指定您想要的 MongoDB 版本的标签 MongoDB 的默认数据目录路径是/data/db 如下: $ docker run -it -v mongodata:/data/db -p 27017:27017 --name mongodb --restart unless-stopped -d mongo 你应该让 MongoDB 在端口 27017 上运行,并且可以通过localhostWindows 和 Ubuntu 20.04 上的URL访问 http://localhost:27017/ -p 是 HOST_PORT:CLIENT_PORT  -P 随机端口 -p 27017:27017 :将容器的27017 端口映射到主机的27017 端口 -v mongodata:/data/db :将主机中当前目录下的db挂载到容器的/data/db,作为mongo数据存储目录 从另一个 Docker 容器连接到 MongoDB 镜像中的 MongoDB 服务器侦听标准 MongoDB 端口27017,因此通过 Docker 网络连接将与连接到远程mongod. 以下示例启动另一个 MongoDB 容器实例,并mongo针对上述示例中的原始 MongoDB 容器运行命令行客户端,从而允许您针对数据库实例执行 MongoDB 语句: $ docker run -it --network some-network --...

MechanicalSoup

用于自动与网站交互的 Python 库。 MechanicalSoup 自动存储和发送 cookie,跟踪重定向,并且可以跟踪链接和提交表单。 它不执行 JavaScript。 https://github.com/MechanicalSoup/MechanicalSoup https://mechanicalsoup.readthedocs.io/en/stable/index.html https://realpython.com/python-web-scraping-practical-introduction/ pip show Mechanicalsoup 找到模块的安装位置 https://stackoverflow.com/questions/54352162/download-file-with-mechanicalsoup # Install dependencies # pip install requests # pip install BeautifulSoup4 # pip install MechanicalSoup # Import libraries import mechanicalsoup import urllib.request import requests from bs4 import BeautifulSoup import re # Create a browser object that can collect cookies browser = mechanicalsoup.StatefulBrowser() browser.open("https://www.ons.gov.uk/economy/grossdomesticproductgdp/timeseries/l2kq/qna") browser.download_link(link_text=".xls",file="D:/ONS_Data.xls" )

端口映射 公网访问内网

https://portforward.com/ Holer 通过安全隧道将位于NAT和防火墙之后的本地服务器暴露给公共Internet。 Holer是一个将原型中的应用映射到公网访问的端口映射软件,支持转发基于TCP协议的报文 https://github.com/wisdom-projects/holer 方式一:使用(公告)的holer映射或者开通holer服务,通过holer客户端软件经 holer服务器实现公网访问。 公开的holer映射详情如下: 访问密钥 访问域名 公网地址 本地地址 使用场景 HOLER_CLIENT-2F8D8B78B3C2A0AE holer65530.wdom.net holer.org:65530 127.0.0.1:8080 网页 HOLER_CLIENT-3C07CDFD1BF99BF2 holer65531.wdom.net holer.org:65531 127.0.0.1:8088 网页 HOLER_CLIENT-2A623FCB6E2A7D1D holer65532.wdom.net holer.org:65532 127.0.0.1:80 网页 HOLER_CLIENT-AF3E6391525F70E4 不适用 holer.org:65533 127.0.0.1:3389 远程桌面 HOLER_CLIENT-822404317F9D8ADD 不适用 holer.org:65534 127.0.0.1:22 SSH协议 HOLER_CLIENT-27DD1389DF1D4DBC 不适用 holer.org:65535 127.0.0.1:3306 数据库 使用Java版本的holer客户端 ①java 1.7或者更高版本 ②下载holer-client.zip 修改配置文件C:\holer-client\conf\holer.conf HOLER_ACCESS_KEY=HOLER_CLIENT-2A623FCB6E2A7D1D HOLER_SERVER_HOST=holer65532.wdom.net ③建议先双击运行C:\holer-client\bin\shutdown.bat,再双击运行C:\holer-client\bin\startup.bat...