-
Notifications
You must be signed in to change notification settings - Fork 0
/
2020-03-18-Postfix.html
31 lines (30 loc) · 19.3 KB
/
2020-03-18-Postfix.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=1"><meta name="renderer" content="webkit"><title>在Linux中使用Postfix进行SMTP邮件发送服务 | BLOG | HDSKY</title><meta name="format-detection" content="telephone=no"><meta name="description" content="因为自建Email Server发送的情况下,经常会被一些邮箱服务识别成垃圾邮件从而导致用户无法收到信息,在Linux中我就使用了 Postfix 然后调用外部 SMTP 服务进行发件,这样有效解决了上面提到的经常会被垃圾邮件的问题,同时觉得使用外部发送邮件的方式能够解决集群的报警并及时处理的问题。"><meta name="keywords" content="黑&蛋,个人博客,Linux,Ubuntu,Centos,HPC,tag.name,tag.name"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><meta name="applicable-device" content="pc,mobile"><link href="/dist/iris-20b518ec25.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/[email protected]/instantpage.min.js" type="module" defer></script><script>var TinyCache=function(){"use strict";var n=window.localStorage,t=document;function e(n){return new Error("Failed to load "+n.name+": "+n.url)}function r(n){return new Error("No such item or item is staled: "+n.name)}function o(n,e){var r=t.createElement("script");r.id=n.name,r.text=e,r.defer=!0,t.getElementsByTagName("head")[0].appendChild(r)}function i(n,r){var o=t.createElement("script");o.id=n.name,o.src=n.url,o.onload=function(){r(null)},o.onerror=function(){r(e(n))},t.getElementsByTagName("body")[0].appendChild(o)}function u(t,e,r){!function(t,e){try{n.setItem(t,JSON.stringify(e))}catch(t){}}(""+t+e.name,{content:r,expire:e.maxAge?(new Date).getTime()+1e3*e.maxAge:null,name:e.name,url:e.url})}return function(){function t(n){this.config={prefix:"TC:",timeout:6e3},function(n,t){for(var e in t)t.hasOwnProperty(e)&&(n[e]=t[e])}(this.config,n)}var a=t.prototype;return a.load=function(n,t){var e,r,o,i,u=this;if(!t)return new Promise((function(t,e){u.load(n,(function(n){n?e(n):t()}))}));e=n.map((function(n){return function(t){u.loadScript(n,t)}})),r=t,o=[],i=e.length,e.forEach((function(n,t){n((function(n,e){o[t]=e,n&&r&&(r(n),r=null),0==--i&&r&&r(null,o)}))}))},a.remove=function(n){!function(n){try{localStorage.removeItem(n)}catch(n){}}(""+this.config.prefix+n.name)},a.loadScript=function(t,a){var c=this;!function(t,e,o){var i=function(t){var e,r=null;try{var o=n.getItem(t);o&&(r=JSON.parse(o))&&("string"!=typeof(e=r).name||"string"!=typeof e.url||"string"!=typeof e.content)&&(r=null)}catch(t){}return r}(""+t+e.name);i?i.url!==e.url||i.expire&&i.expire<(new Date).getTime()?o(r(e)):o(null,i.content):o(r(e))}(this.config.prefix,t,(function(n,r){var l,f,m,s;n?(l=t,f=c.config.timeout,m=function(n,e){e&&!n?(o(t,e),u(c.config.prefix,t,e),a(null)):i(t,a)},(s=new XMLHttpRequest).open("GET",l.url,!0),s.timeout=f,s.onload=function(){200<=s.status&&s.status<300||304===s.status?m(null,s.responseText):m(e(l))},s.ontimeout=s.onerror=function(){m(e(l))},s.send()):r?(o(t,r),a(null)):i(t,a)}))},t}()}()</script><script>(new TinyCache).load([{url:"/dist/iris-dfae13bc3a.js",name:"iris"}])</script><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="//www.googletagmanager.com"><link rel="dns-prefetch" href="//hdsky-pw.disqus.com"><link rel="shortcut icon" type="image/ico" href="/favicon.ico"><link rel="icon" type="image/png" href="/favicon-192x192.png"><link rel="apple-touch-icon" href="/apple-touch-icon.png"><link rel="alternate" type="application/atom+xml" href="/atom.xml"><meta property="og:title" content="在Linux中使用Postfix进行SMTP邮件发送服务 | BLOG | HDSKY"><meta property="og:type" content="blog"><meta property="og:url" content="https://hdsky.pw/2020-03-18-Postfix.html"><meta property="og:image" content="/favicon-192x192.png"><meta property="og:description" content="因为自建Email Server发送的情况下,经常会被一些邮箱服务识别成垃圾邮件从而导致用户无法收到信息,在Linux中我就使用了 Postfix 然后调用外部 SMTP 服务进行发件,这样有效解决了上面提到的经常会被垃圾邮件的问题,同时觉得使用外部发送邮件的方式能够解决集群的报警并及时处理的问题。"><meta property="og:article:tag" content="黑&蛋,个人博客,Linux,Ubuntu,Centos,HPC,tag.name,tag.name"><meta property="og:locale" content="zh-Hans"><meta property="article:published_time" content="Wed Mar 18 2020 15:01:42 GMT+0800"><meta property="article:modified_time" content="Tue Jun 28 2022 00:08:48 GMT+0800"><meta name="twitter:card" content="summary"><script async src="https://www.googletagmanager.com/gtag/js?id=G-BNEM1LH28R"></script><script>function gtag(){dataLayer.push(arguments)}window.dataLayer=window.dataLayer||[],gtag("js",new Date),gtag("config","G-BNEM1LH28R")</script><script type="application/ld+json">{
"@context": "http://schema.org",
"@type": "BlogPosting",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://hdsky.pw"
},
"headline": "在Linux中使用Postfix进行SMTP邮件发送服务",
"datePublished": "2020-03-18T07:01:42.000Z",
"dateModified": "2022-06-27T16:08:48.271Z",
"author": {
"@type": "Person",
"name": "黑&蛋",
"image": {
"@type": "ImageObject",
"url": "avatar.png"
},
"description": "黑&蛋的个人博客"
},
"publisher": {
"@type": "Organization",
"name": "BLOG | HDSKY",
"logo": {
"@type": "ImageObject",
"url": "/favicon-192x192.png"
}
},
"keywords": "黑&蛋,个人博客,Linux,Ubuntu,Centos,HPC,tag.name,tag.name",
"description": "因为自建Email Server发送的情况下,经常会被一些邮箱服务识别成垃圾邮件从而导致用户无法收到信息,在Linux中我就使用了 Postfix 然后调用外部 SMTP 服务进行发件,这样有效解决了上面提到的经常会被垃圾邮件的问题,同时觉得使用外部发送邮件的方式能够解决集群的报警并及时处理的问题。"
}</script><script>"serviceWorker"in navigator&&navigator.serviceWorker.getRegistrations().then((function(e){e.map((function(e){e.unregister()}))}))</script><meta name="generator" content="Hexo 7.1.1"></head><body data-instant-intensity="viewport"><header class="header header__no-hero"><div class="header-wrapper"><div class="header__left"><div class="header-logo"><a href="/">BLOG | HDSKY</a></div></div><div class="header__right"><nav class="nav"><a id="nav-toggle" class="nav-toggle"><div></div></a><div class="nav-wrapper"><ul><li><a class="nav-link" href="/links">Link</a></li><li><a class="nav-link" href="/archives">Archive</a></li><li><a class="nav-link" href="/about">About</a></li></ul></div></nav></div></div></header><main class="main"><article class="post"><header class="post-header"><h1 class="post-title">在Linux中使用Postfix进行SMTP邮件发送服务</h1><section class="post-meta"><time datetime="2020-03-18T07:01:42.000Z"><i class="far fa-calendar" aria-hidden></i> 2020-03-18</time></section></header><aside class="post-aside"><section class="post-toc"><header class="post-toc-title">Contents</header><ol class="post-toc-inst"><li class="post-toc-inst-item post-toc-inst-level-2"><a class="post-toc-inst-link" href="#%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE"><span class="post-toc-inst-number">1.</span> <span class="post-toc-inst-text">安装配置</span></a></li><li class="post-toc-inst-item post-toc-inst-level-2"><a class="post-toc-inst-link" href="#%E5%8F%91%E4%BB%B6%E6%B5%8B%E8%AF%95"><span class="post-toc-inst-number">2.</span> <span class="post-toc-inst-text">发件测试</span></a></li><li class="post-toc-inst-item post-toc-inst-level-2"><a class="post-toc-inst-link" href="#%E8%B0%83%E8%AF%95"><span class="post-toc-inst-number">3.</span> <span class="post-toc-inst-text">调试</span></a></li></ol></section></aside><section class="post-content"><p>因为自建Email Server发送的情况下,经常会被一些邮箱服务识别成垃圾邮件从而导致用户无法收到信息,在Linux中我就使用了 <code>Postfix</code> 然后调用外部 SMTP 服务进行发件,这样有效解决了上面提到的经常会被垃圾邮件的问题,同时觉得使用外部发送邮件的方式能够解决集群的报警并及时处理的问题。<span id="more"></span></p><p>在此我选择使用腾讯企业邮箱作为发件SMTP,做下记录。</p><h2 id="安装配置"><a href="#安装配置" class="headerlink" title="安装配置"></a>安装配置</h2><p>先是安装 Postfix 软件</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install postfix</span><br></pre></td></tr></table></figure><p>我们需要对以下的配置文件进行调整</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">### Domain 设置</span></span><br><span class="line">mydomain = Demo.com</span><br><span class="line">myorigin = <span class="variable">$mydomain</span></span><br><span class="line">myhostname = <span class="variable">$mydomain</span></span><br><span class="line">mydestination = <span class="variable">$mydomain</span></span><br><span class="line"></span><br><span class="line"><span class="comment">### alias设定</span></span><br><span class="line">alias_database = <span class="built_in">hash</span>:/etc/aliases</span><br><span class="line">alias_maps = <span class="built_in">hash</span>:/etc/aliases</span><br><span class="line"></span><br><span class="line"><span class="comment">### SMTP SETTINGs</span></span><br><span class="line">relayhost = [smtp.exmail.qq.com]:587</span><br><span class="line">smtp_sasl_auth_enable = <span class="built_in">yes</span></span><br><span class="line">smtp_sasl_password_maps = <span class="built_in">hash</span>:/etc/postfix/sasl_passwd</span><br><span class="line">smtp_tls_security_level = encrypt</span><br><span class="line">smtp_use_tls = <span class="built_in">yes</span></span><br><span class="line">smtp_sasl_security_options = noanonymous</span><br><span class="line"></span><br><span class="line"><span class="comment">### postfix version 2.2 or up</span></span><br><span class="line">smtp_generic_maps = <span class="built_in">hash</span>:/etc/postfix/generic</span><br></pre></td></tr></table></figure><p>使用 <code>postconf -n</code> 可以查看当前的配置,下面提供一份示例:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">alias_database = <span class="built_in">hash</span>:/etc/aliases</span><br><span class="line">alias_maps = <span class="built_in">hash</span>:/etc/aliases</span><br><span class="line">command_directory = /usr/sbin</span><br><span class="line">config_directory = /etc/postfix</span><br><span class="line">daemon_directory = /usr/libexec/postfix</span><br><span class="line">data_directory = /var/lib/postfix</span><br><span class="line">debug_peer_level = 2</span><br><span class="line">debugger_command = PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin ddd <span class="variable">$daemon_directory</span>/<span class="variable">$process_name</span> <span class="variable">$process_id</span> & <span class="built_in">sleep</span> 5</span><br><span class="line">html_directory = no</span><br><span class="line">inet_interfaces = all</span><br><span class="line">inet_protocols = all</span><br><span class="line">mail_owner = postfix</span><br><span class="line">mailq_path = /usr/bin/mailq.postfix</span><br><span class="line">manpage_directory = /usr/share/man</span><br><span class="line">mydestination = <span class="variable">$myhostname</span>, localhost.<span class="variable">$mydomain</span>, localhost</span><br><span class="line">mydomain = Demo.com</span><br><span class="line">myhostname = <span class="variable">$mydomain</span></span><br><span class="line">myorigin = <span class="variable">$mydomain</span></span><br><span class="line">newaliases_path = /usr/bin/newaliases.postfix</span><br><span class="line">queue_directory = /var/spool/postfix</span><br><span class="line">readme_directory = /usr/share/doc/postfix-2.10.1/README_FILES</span><br><span class="line">relayhost = [smtp.exmail.qq.com]:587</span><br><span class="line">sample_directory = /usr/share/doc/postfix-2.10.1/samples</span><br><span class="line">sendmail_path = /usr/sbin/sendmail.postfix</span><br><span class="line">setgid_group = postdrop</span><br><span class="line">smtp_generic_maps = <span class="built_in">hash</span>:/etc/postfix/generic</span><br><span class="line">smtp_sasl_auth_enable = <span class="built_in">yes</span></span><br><span class="line">smtp_sasl_password_maps = <span class="built_in">hash</span>:/etc/postfix/sasl_passwd</span><br><span class="line">smtp_sasl_security_options = noanonymous</span><br><span class="line">smtp_tls_security_level = encrypt</span><br><span class="line">smtp_use_tls = <span class="built_in">yes</span></span><br><span class="line">unknown_local_recipient_reject_code = 550</span><br></pre></td></tr></table></figure><p>然后在 <code>/etc/postfix/sasl_passwd</code> 里边写入邮箱用户名和密码:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[smtp.exmail.qq.com]:587 [email protected]:[PASSWORD]</span><br></pre></td></tr></table></figure><p>在 <code>/etc/postfix/generic</code> 里配置信封上的发件人(MAIL FROM 命令)的地址重写,不然会出现认证错误:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@Demo.com [email protected]</span><br></pre></td></tr></table></figure><p>在 <code>/etc/aliases</code> 中添加 root 邮件转发,这样发送到root的邮件,都会转发到<code>[email protected]</code></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">postmaster: root</span><br><span class="line">root: [email protected] <span class="comment">##添加这个</span></span><br></pre></td></tr></table></figure><p>之后要使用<code>newaliases</code>命令来重新生成 <code>aliases</code> 库文件,使更改生效。</p><p>最后生成 hash 数据库,并更改文件权限,每次修改这些文件:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">postmap /etc/postfix/sasl_passwd</span><br><span class="line">postmap /etc/postfix/generic</span><br><span class="line"><span class="built_in">chmod</span> 600 /etc/postfix/sasl_passwd*</span><br></pre></td></tr></table></figure><p>重启或重新加载 Postfix:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">service postfix restart</span><br></pre></td></tr></table></figure><h2 id="发件测试"><a href="#发件测试" class="headerlink" title="发件测试"></a>发件测试</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">echo</span> <span class="string">'This is a mail'</span> | mail -s <span class="string">'This is a mail'</span> [email protected]</span><br></pre></td></tr></table></figure><h2 id="调试"><a href="#调试" class="headerlink" title="调试"></a>调试</h2><p>过程中可能并不会那么的一帆风顺,在下面提供了一些简单的调试方法仅供参考:</p><p>首先看日志文件:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">tail</span> -f /var/log/maillog</span><br></pre></td></tr></table></figure><p>查看邮件队列:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mailq</span><br></pre></td></tr></table></figure></section><footer class="post-meta"><section class="tags"><a href="/tags/Linux/"><i class="fas fa-tag" aria-hidden></i> Linux </a><a href="/tags/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/"><i class="fas fa-tag" aria-hidden></i> 学习笔记</a></section></footer><div class="post-end" data-text="End"></div></article><section><div id="disqus_thread"></div><script>!function(s){window.DISQUS=null;var e=s.createElement("link");e.rel="stylesheet",e.href="https://cdn.jsdelivr.net/npm/[email protected]/dist/disqusjs.css",s.head.appendChild(e);var i=s.createElement("script");i.async=!0,i.src="https://cdn.jsdelivr.net/npm/[email protected]/dist/disqus.js",i.onload=function(){new DisqusJS({shortname:"hdsky-pw",siteName:"BLOG | HDSKY",identifier:"2020-03-18-Postfix",url:"https://hdsky.pw/2020-03-18-Postfix.html",title:"在Linux中使用Postfix进行SMTP邮件发送服务",api:"",apikey:"nvPDNH7YoE53bGKClf2CTyHqz9pYcnEsDoMFdpBEdarIrYJNfsdSDrAioHU5292n",admin:"disqus_trck5pCCAM",adminLabel:""})},s.head.appendChild(i)}(document)</script></section></main><footer class="footer"><div class="footer-wrapper"><p class="copyright">© 2016 - 2024 黑&蛋</p><p class="theme">Hexo Theme <a href="https://github.com/giuem/hexo-theme-iris" target="_blank" rel="noopener noreferrer">Iris</a> with <i class="fas fa-heart"></i></p></div></footer></body></html>