大网数据广告活动
服务器运维问答

Sendmail in FreeBSD send with external SMTP without auth

· 52CE 我爱测

I'm sorry if this might sound confusing at first. My brother asked me to make a web server for him because he wanted to self host his own website and not use a hosting service anymore. The problem with this is the ISP. Besides th…

问题原文

原帖链接 · Comrade Zed · CC BY-SA 4.0

I'm sorry if this might sound confusing at first.

My brother asked me to make a web server for him because he wanted to self host his own website and not use a hosting service anymore. The problem with this is the ISP. Besides the dynamic IP scheme, they block outgoing TCP connections on port 25, and instead they offer their external server which requires no authentication.

For the web server setup I have chosen FreeBSD, as I prefer jails to virtual machines or running the services directly on the host machine. It makes backing up and security management a lot easier (at least for me).

For the moment I have disabled sendmail completely, but I wish to enable it again, so users can make use of the recover password function when necessary. My question is, how can I configure sendmail to use an external SMTP without user or password authentication?

Thank you in advance for your help!

已采纳解答

原帖链接 · Jim L. · CC BY-SA 4.0

Both of the comments to your post are on the right track. Here is a more fully fledged response.

(TL;DR at bottom)

As root, start by making a safety copy of your /etc/mail directory:

# cd /etc
# cp -Rp mail mail.safety

For the purposes of this example, I'm going to assume that you have no customizations to your email configuration, and in fact, we'll start by "zeroing-out" your existing sendmail files to ensure that we start with a clean slate. If you do have customizations, not to worry, we started off by making a backup of those files. You can re-apply your customizations later.

First move into the mail directory and remove your existing *.cf files:

# cd mail
# rm *.cf

It is common on FreeBSD systems to have a $(hostname).mc file and a $(hostname).submit.mc file, in addition to the stock freebsd.mc and freebsd.submit.mc files that are distributed with the OS. Here is the output of ls on a system with the simple hostname test:

# ls *.mc
freebsd.mc         freebsd.submit.mc     test.mc         test.submit.mc

You will probably see similar files, but referencing your own machine's hostname. The reason behind this is that the freebsd*.mc files represent the basic starting point for sendmail configuration. The $(hostname)*.mc files are (potentially) customized files that are specific to your system. While an OS upgrade may well overwrite the freebsd*.mc files, it will not touch your host-specific files, so your carefully customized configs will not be overwritten by an OS upgrade. Your part of that bargain is that you should confine your changes to the $(hostname)*.mc files and leave the freebsd*.mc reference files untouched.

Throughout the remainder of this tutorial, I will use the string test to represent places where you should see your machine's hostname as part of a filename.

So with that background, let's continue.

Remove your $(hostname)*.mc files so that the Makefile will clone your configuration from the known-good freebsd*.mc files.

# rm -v $(hostname)*.mc
test.mc
test.submit.mc

Now that we've removed your old config files, let's run make to create new ones based on the freebsd.mc and freebsd.submit.mc files provided by the OS:

# make
cp -f freebsd.mc test.mc
/usr/bin/m4 -D_CF_DIR_=/usr/share/sendmail/cf/   /usr/share/sendmail/cf/m4/cf.m4 test.mc > test.cf
cp -f freebsd.submit.mc test.submit.mc
/usr/bin/m4 -D_CF_DIR_=/usr/share/sendmail/cf/   /usr/share/sendmail/cf/m4/cf.m4 test.submit.mc > test.submit.cf

That actually does a little more than we need, but that's alright. The important thing is that the Makefile copied freebsd.mc to $(hostname).mc and freebsd.submit.mc to $(hostname).submit.mc.

Now, let's inspect the $(hostname).mc file. At or near line 89 (of the file I'm looking at for FreeBSD 12) is the SMART_HOST directive that the previous authors have mentioned:

# grep SMART_HOST $(hostname).mc
dnl define(`SMART_HOST', `your.isp.mail.server')

The 'dnl' at the beginning of the line means the line is commented out. DNL can be thought to stand for "Discard (or ignore) until NewLine."

Edit your .mc file:

# vi $(hostname).mc

and change that line to read:

define(`SMART_HOST', `smtp.example.com')

where smtp.example.com is the DNS name of the server your ISP told you to use. Be sure to remove the "dnl" also. Save the file and exit.

Now run make to re-generate your $(hostname)*.cf files:

# make
/usr/bin/m4 -D_CF_DIR_=/usr/share/sendmail/cf/   /usr/share/sendmail/cf/m4/cf.m4 test.mc > test.cf
/usr/bin/m4 -D_CF_DIR_=/usr/share/sendmail/cf/   /usr/share/sendmail/cf/m4/cf.m4 test.submit.mc > test.submit.cf

Run make install to copy your $(hostname)*.cf files to the standard names that sendmail expects (sendmail.cf and submit.cf):

# make install
install -m 444 test.cf /etc/mail/sendmail.cf
install -m 444 test.submit.cf /etc/mail/submit.cf

Restart sendmail:

# make restart

Finally, inspect /var/log/maillog to ensure there are no errors.


TL;DR

# cd /etc
# cp -Rp mail mail.safety
# cd mail
# rm *.cf
# rm -v $(hostname)*.mc
# make
# vi $(hostname).mc

Change the SMART_HOST line to read:

define(`SMART_HOST', `smtp.example.com')

and continue:

# make
# make install
# make restart

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-02-01)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。