使用shell脚本发送HTML邮件 [英] Sending HTML mail using a shell script

查看:170
本文介绍了使用shell脚本发送HTML邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用shell脚本发送HTML电子邮件?

How can I send an HTML email using a shell script?

推荐答案

首先需要撰写邮件。最低限度由以下两个标题组成:

First you need to compose the message. The bare minimum is composed of these two headers:

MIME-Version: 1.0
Content-Type: text/html

...和相应的邮件正文:

... and the appropriate message body:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title></title>
</head>
<body>

<p>Hello, world!</p>

</body>
</html>

一旦你拥有它,你可以将适当的信息传递给 mail 命令:

Once you have it, you can pass the appropriate information to the mail command:

body = '...'

echo $body | mail \
-a "From: me@example.com" \
-a "MIME-Version: 1.0" \
-a "Content-Type: text/html" \
-s "This is the subject" \
you@example.com

这是一个过于简单的例子,因为你还需要照顾字符集,编码,最大行长度...但这基本上是这个想法。

This is an oversimplified example, since you also need to take care of charsets, encodings, maximum line length... But this is basically the idea.

或者,您可以在Perl或PHP中编写脚本,而不是纯shell。

Alternatively, you can write your script in Perl or PHP rather than plain shell.

基本上是Unix行结尾的文本文件与一个名为家当该行启动告诉shell它必须将文件传递给哪个解释器,遵循解释器理解并具有执行权限的语言中的某些命令(在Unix中是文件属性)。例如,保存以下内容为 hello-world

A shell script is basically a text file with Unix line endings that starts with a line called shebang that tells the shell what interpreter it must pass the file to, follow some commands in the language the interpreter understands and has execution permission (in Unix that's a file attribute). E.g., let's say you save the following as hello-world:

#!/bin/sh

echo Hello, world!

然后分配执行权限:

chmod +x hello-world

最后可以运行它:

./hello-world

无论如何,这与原来的问题无关。在进行高级任务之前,您应该熟悉基本的shell脚本。这里有几个关于 bash 的链接,一个受欢迎的shell:

Whatever, this is kind of unrelated to the original question. You should get familiar with basic shell scripting before doing advanced tasks with it. Here you are a couple of links about bash, a popular shell:

http://www.gnu.org/software/bash/manual/html_node/index.html

http://tldp.org/HOWTO/ Bash-Prog-Intro-HOWTO.html

这篇关于使用shell脚本发送HTML邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆