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

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

获得后,您可以将适当的信息传递给邮件 命令:

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.

shell 脚本基本上是一个以 Unix 行结尾的文本文件,以名为 的行开头shebang 告诉 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/手册/html_node/index.html

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

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

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