-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinux shell发送邮件.html
211 lines (197 loc) · 13.9 KB
/
linux shell发送邮件.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="littlewhite" />
<meta name="copyright" content="littlewhite" />
<meta property="og:type" content="article" />
<meta name="twitter:card" content="summary">
<meta name="keywords" content="linux, shell, Skill, " />
<meta property="og:title" content="linux shell发送邮件 "/>
<meta property="og:url" content="https://chukeer.github.io/linux shell发送邮件.html" />
<meta property="og:description" content="一封最简单的邮件¶ echo -e "To: [email protected]\nCC: [email protected]\nFrom: handy<[email protected]>\nSubject: test\n\nhello world" | sendmail -t 看上去有点复杂,其实就是sendmail程序从标准输入读取邮件源码,-t参数表示从邮件源码提取收件人信息,然后发送到收件人的邮件服务器,我们稍做整理,将邮件源码保存在email.txt中如下 To: [email protected] CC: [email protected] From: handy<[email protected]> Subject: test hello world 将以上命令改为cat email ..." />
<meta property="og:site_name" content="楚客" />
<meta property="og:article:author" content="littlewhite" />
<meta property="og:article:published_time" content="2016-11-23T00:00:00+08:00" />
<meta property="" content="2016-11-23T00:00:00+08:00" />
<meta name="twitter:title" content="linux shell发送邮件 ">
<meta name="twitter:description" content="一封最简单的邮件¶ echo -e "To: [email protected]\nCC: [email protected]\nFrom: handy<[email protected]>\nSubject: test\n\nhello world" | sendmail -t 看上去有点复杂,其实就是sendmail程序从标准输入读取邮件源码,-t参数表示从邮件源码提取收件人信息,然后发送到收件人的邮件服务器,我们稍做整理,将邮件源码保存在email.txt中如下 To: [email protected] CC: [email protected] From: handy<[email protected]> Subject: test hello world 将以上命令改为cat email ...">
<title>linux shell发送邮件 · 楚客
</title>
<!--
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.1/css/font-awesome.css" rel="stylesheet">
--!>
<link href="https://chukeer.github.io/theme/css/bootstrap-combined.min.css" rel="stylesheet">
<link href="https://chukeer.github.io/theme/css/font-awesome.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="https://chukeer.github.io/theme/css/pygments.css" media="screen">
<link rel="stylesheet" type="text/css" href="https://chukeer.github.io/theme/tipuesearch/tipuesearch.css" media="screen">
<link rel="stylesheet" type="text/css" href="https://chukeer.github.io/theme/css/elegant.css" media="screen">
<link rel="stylesheet" type="text/css" href="https://chukeer.github.io/theme/css/custom.css" media="screen">
</head>
<body>
<div id="content-sans-footer">
<div class="navbar navbar-static-top">
<div class="navbar-inner">
<div class="container-fluid">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="https://chukeer.github.io/"><span class=site-name>楚客</span></a>
<div class="nav-collapse collapse">
<ul class="nav pull-right top-menu">
<li ><a href="https://chukeer.github.io">Home</a></li>
<li ><a href="https://chukeer.github.io/categories.html">Categories</a></li>
<li ><a href="https://chukeer.github.io/tags.html">Tags</a></li>
<li ><a href="https://chukeer.github.io/archives.html">Archives</a></li>
<li><form class="navbar-search" action="https://chukeer.github.io/search.html" onsubmit="return validateForm(this.elements['q'].value);"> <input type="text" class="search-query" placeholder="Search" name="q" id="tipue_search_input"></form></li>
</ul>
</div>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row-fluid">
<div class="span1"></div>
<div class="span10">
<article>
<div class="row-fluid">
<header class="page-header span10 offset2">
<h1><a href="https://chukeer.github.io/linux shell发送邮件.html"> linux shell发送邮件 </a></h1>
</header>
</div>
<div class="row-fluid">
<div class="span2 table-of-content">
<nav>
<h4>Contents</h4>
<div class="toc">
<ul>
<li><a href="#_1">一封最简单的邮件</a></li>
<li><a href="#_2">邮件的格式</a></li>
<li><a href="#_3">邮件标题使用中文</a></li>
<li><a href="#html">邮件内容使用html</a></li>
</ul>
</div>
</nav>
</div>
<div class="span8 article-content">
<h2 id="_1">一封最简单的邮件<a class="headerlink" href="#_1" title="Permanent link">¶</a></h2>
<div class="highlight"><pre><span></span>echo -e "To: [email protected]\nCC: [email protected]\nFrom: handy<[email protected]>\nSubject: test\n\nhello world" | sendmail -t
</pre></div>
<p>看上去有点复杂,其实就是sendmail程序从标准输入读取邮件源码,-t参数表示从邮件源码提取收件人信息,然后发送到收件人的邮件服务器,我们稍做整理,将邮件源码保存在email.txt中如下</p>
<div class="highlight"><pre><span></span><span class="n">To</span><span class="o">:</span> <span class="n">handy1989</span><span class="err">@</span><span class="n">qq</span><span class="o">.</span><span class="na">com</span>
<span class="n">CC</span><span class="o">:</span> <span class="n">handy1989</span><span class="err">@</span><span class="n">qq</span><span class="o">.</span><span class="na">com</span>
<span class="n">From</span><span class="o">:</span> <span class="n">handy</span><span class="o"><</span><span class="n">handy</span><span class="err">@</span><span class="n">test</span><span class="o">.</span><span class="na">com</span><span class="o">></span>
<span class="n">Subject</span><span class="o">:</span> <span class="n">test</span>
<span class="n">hello</span> <span class="n">world</span>
</pre></div>
<p>将以上命令改为<code>cat email.txt | sendmail -t</code>,这样就一目了然了。收到的邮件信息如下</p>
<p><img alt="" src="http://littlewhite.us/pic/mail-helloworld.png"/></p>
<h2 id="_2">邮件的格式<a class="headerlink" href="#_2" title="Permanent link">¶</a></h2>
<p>从前面的邮件源码可以看到,邮件是和http类似的文本协议,由邮件头和邮件内容两部分组成,中间以空行分隔,邮件头每行对应一个字段,和http头类似,比如这里的To,CC,From,Subject,分别代表收件人,抄送人,发件人,标题,如果有多个收件人或抄送人,用逗号分隔,邮件内容才是我们在邮件客户端真正看到的东西</p>
<p>邮件客户端都可以查看邮件源码,比如下面就是我收到的一封邮件的源码
<img alt="" src="http://littlewhite.us/pic/mail-qq.png"/></p>
<h2 id="_3">邮件标题使用中文<a class="headerlink" href="#_3" title="Permanent link">¶</a></h2>
<p>如果邮件标题直接使用中文字符会导致收到的邮件乱码,为了避免这种情况,应该对中文进行base64编码,而这也是邮件最常用的编码方式,当然,在进行base64编码之前先得对中文字符进行编码(UTF-8或GBK等等),这和html的编码是一样的概念,采用UTF-8和base64编码的格式如下</p>
<div class="highlight"><pre><span></span>=?UTF-8?B?xxxxxx?=
</pre></div>
<p>其中<code>xxxxxx</code>为编码后的数据,用python可以快速对中文进行编码,比如对中文'测试'先进行utf-8编码再进行base64编码结果为</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">import</span> <span class="nn">base64</span>
<span class="o">>>></span> <span class="n">base64</span><span class="o">.</span><span class="n">standard_b64encode</span><span class="p">(</span><span class="s1">u'测试'</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">'utf-8'</span><span class="p">))</span>
<span class="s1">'5rWL6K+V'</span>
</pre></div>
<p>在From和Subject中使用中文,邮件源码如下</p>
<div class="highlight"><pre><span></span><span class="n">To</span><span class="o">:</span> <span class="n">handy1989</span><span class="err">@</span><span class="n">qq</span><span class="o">.</span><span class="na">com</span>
<span class="n">CC</span><span class="o">:</span> <span class="n">handy1989</span><span class="err">@</span><span class="n">qq</span><span class="o">.</span><span class="na">com</span>
<span class="n">From</span><span class="o">:</span> <span class="o">=?</span><span class="n">UTF</span><span class="o">-</span><span class="mi">8</span><span class="o">?</span><span class="n">B</span><span class="o">?</span><span class="mi">5</span><span class="n">rWL6K</span><span class="o">+</span><span class="n">V</span><span class="o">?=<</span><span class="n">handy</span><span class="err">@</span><span class="n">test</span><span class="o">.</span><span class="na">com</span><span class="o">></span>
<span class="n">Subject</span><span class="o">:</span> <span class="o">=?</span><span class="n">UTF</span><span class="o">-</span><span class="mi">8</span><span class="o">?</span><span class="n">B</span><span class="o">?</span><span class="mi">5</span><span class="n">rWL6K</span><span class="o">+</span><span class="n">V</span><span class="o">?=</span>
<span class="n">hello</span> <span class="n">world</span>
</pre></div>
<p>这里将发件人的名字和邮件标题都改为了'测试',收到的邮件效果为</p>
<p><img alt="" src="http://littlewhite.us/pic/mail-chinese-title.png"/></p>
<h2 id="html">邮件内容使用html<a class="headerlink" href="#html" title="Permanent link">¶</a></h2>
<p>如果邮件内容是html代码,则需要在邮件头添加Content-type字段来标记文本类型,同时还需要标记邮件内容的字符编码,以下邮件源码发送的正是html内容</p>
<div class="highlight"><pre><span></span>To: [email protected]
From: =?UTF-8?B?5rWL6K+V?=<span class="nt"><handy</span><span class="err">@test.com</span><span class="nt">></span>
Subject: =?UTF-8?B?5rWL6K+V?=
Content-type: text/html;charset=utf-8
<span class="nt"><h1></span>hello world<span class="nt"></h1></span>
</pre></div>
<p>收到的邮件效果为</p>
<p><img alt="" src="http://littlewhite.us/pic/mail-html.png"/></p>
<hr/>
<aside>
<nav>
<ul class="articles-timeline">
<li class="previous-article">« <a href="https://chukeer.github.io/iTerm2固定标签名字.html" title="Previous: iTerm2固定标签名字">iTerm2固定标签名字</a></li>
<li class="next-article"><a href="https://chukeer.github.io/Unix IPC概述.html" title="Next: Unix IPC概述">Unix IPC概述</a> »</li>
</ul>
</nav>
</aside>
</div>
<section>
<div class="span2" style="float:right;font-size:0.9em;">
<table class="table">
<!-- <time pubdate="pubdate" datetime="2016-11-23T00:00:00+08:00">11 23, 2016</time> -->
<tr>
<td>Published</td>
<td><time pubdate="pubdate" datetime="2016-11-23T00:00:00+08:00">2016-11-23</time></td>
</tr>
<tr>
<td>Category</td>
<td><a class="category-link" href="https://chukeer.github.io/categories.html#skill-ref">Skill</a></td>
</tr>
<tr>
<td>Tags</td>
<td>
<ul class="list-of-tags tags-in-article">
<li><a href="https://chukeer.github.io/tags.html#linux-ref">linux
<span>7</span>
</a></li>
<li><a href="https://chukeer.github.io/tags.html#shell-ref">shell
<span>4</span>
</a></li>
</ul>
</td>
</tr>
</table>
</div>
</section>
</div>
</article>
</div>
<div class="span1"></div>
</div>
</div>
<div id="push"></div>
</div>
<footer>
<div id="footer">
<ul class="footer-content">
<li class="elegant-power">Powered by <a href="http://getpelican.com/" title="Pelican Home Page">Pelican</a>. Theme: <a href="http://oncrashreboot.com/pelican-elegant" title="Theme Elegant Home Page">Elegant</a> by <a href="http://oncrashreboot.com" title="Talha Mansoor Home Page">Talha Mansoor</a></li>
</ul>
</div>
</footer> <!--
<script src="http://code.jquery.com/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
-->
<script src="https://chukeer.github.io/theme/js/jquery.min.js"></script>
<script src="https://chukeer.github.io/theme/js/bootstrap.min.js"></script>
<script>
function validateForm(query)
{
return (query.length > 0);
}
</script>
<script>
$("div.article-content table").addClass("table table-hover");
</script>
</body>
<!-- Theme: Elegant built for Pelican
License : http://oncrashreboot.com/pelican-elegant -->
</html>