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

查看:323
本文介绍了使用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>

一旦你拥有了它,你可以传递适当的信息到邮件命令:

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脚本,而不是简单的外壳。

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

一个shell脚本基本上是Unix行结尾的文本文件,与一个叫行启动家当告诉什么际preTER它必须通过文件,遵循语言中的一些命令间preTER理解并具有执行权限(在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!

然后分配执行权限:

Then you assign execution permission:

chmod +x hello-world

和你终于可以运行它:

./hello-world

不管结果如何,这是种无关的原题。做高级任务,与它之前,您应该熟悉基本的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/手动/ html_node / index.html的

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

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

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