月度归档:2018年10月

使用mail,mailx,sendmail,curl发送邮件

【mail】
echo hello | mail -s ‘hello’ roben@test.com

【mailx】
echo “this is content” | mailx -s “this is title” roben@test.com

【sendmail】
{
echo “From: roben@test.com”
echo “To: roben@test.com”
echo “Subject: This is subject”
echo “This is body”
} | /usr/sbin/sendmail -t

 
【curl】
curl -V 查看版本;要支持SMTP(或者POP3)协议,curl的版本必须高于7.20(含)

发送邮件的命令如下:
需要认证:
curl -v -s –url “smtp://192.168.170.130:25″ –mail-from “roben@test.com” –mail-rcpt “roben@test.com” –upload-file mail.txt –user “roben:xxxxxx”

不需要认证:
curl -v -s –url “smtp://192.168.170.130:25″ –mail-from “roben@test.com” –mail-rcpt “roben@test.com” –upload-file mail.txt

mail.txt 内容如下,注意要有空行
cat mail.txt
From:roben@test.com
To:roben@test.com
Subject: this is subject

this is body
参数说明:
-v 回显调试信息
–url :smtp地址
–mail-from:发件人邮箱
–mail-rcpt:收件人邮箱
–upload-file:信件内容,包含发件人、收件人、标题、内容
–user:账号密码,中间用冒号分隔
[root@localhost Roben]# curl -v -s –url “smtp://192.168.170.130:25″ –mail-from “roben@test.com” –mail-rcpt “roben@test.com” –upload-file mail.txt
* About to connect() to 192.168.170.130 port 25 (#0)
* Trying 192.168.170.130…
* Connected to 192.168.170.130 (192.168.170.130) port 25 (#0)
< 220 test ESMTP Postfix
> EHLO mail.txt
< 250-test
< 250-PIPELINING
< 250-SIZE 10240000
< 250-VRFY
< 250-ETRN
< 250-AUTH LOGIN PLAIN
< 250-ENHANCEDSTATUSCODES
< 250-8BITMIME
< 250 DSN
> MAIL FROM:<roben@test.com> SIZE=102
< 250 2.1.0 Ok
> RCPT TO:<roben@test.com>
< 250 2.1.5 Ok
> DATA
< 354 End data with <CR><LF>.<CR><LF>
} [data not shown]
* We are completely uploaded and fine
< 250 2.0.0 Ok: queued as EAF6284038
* Connection #0 to host 192.168.170.130 left intact
[root@localhost Roben]#

两个postfix之间相互发邮件

首先为两台机器配置好hostname,配置hostname方法如下:

export HOSTNAME=test.com
echo $HOSTNAME>/etc/HOSTNAME
/etc/rc.d/boot.localnet stop
/etc/rc.d/boot.localnet start

配置OK:

test:/etc/postfix # hostname
test
test:/etc/postfix #

修改/etc/hosts,在其中指定host和ip的对应关系

test:/etc/postfix # cat /etc/hosts
192.168.170.130 test.com test
192.168.170.134 test2.com test2
test:/etc/postfix #

然后为postfix配置smtp_host_lookup,该参数表示在转发邮件时如何查询主机;native表示在本地/etc/hosts中查找,dns表示通过dns服务器查找

postconf -d 打印默认配置

postconf 打印当前配置

test:/ # postconf -d smtp_host_lookup
smtp_host_lookup = dns
test:/ # postconf smtp_host_lookup
smtp_host_lookup = native, dns
test:/ #

默认只查dns,需要增加native:

smtp_host_lookup = native, dns

这样两台机器就可以相互转发邮件了

另外要注意,mydestination 表示本机的域名,并不是目的机器的域名