如何从 Linux 中的文件向用户发送具有特定模板的邮件(模板包含文件中的信息)?

发布于 2024-10-31 15:32:19 字数 545 浏览 0 评论 0原文

基本上,我必须从文件中获取姓氏、姓氏、用户名和年级,并且必须向该用户(“用户名”)发送一封电子邮件,如下所示:

“亲爱的‘姓氏’‘姓氏’!
您在“班级名称”考试中的成绩为“等级”。”

‘班级名称’是通过shell的参数列表给出的,其余信息都在该文件中。

所以我一步一步进行。首先打印出来使用 awk 所需的布局

awk '{print  "Dear",$1,$2 "!","\nYour grade on the '$class' exam was ",$4;}' in.txt

工作正常。然后我测试了我的邮件是否有效:

$ mail -s 'Subj' bando < /tmp/msg.txt

所以我需要将输出写入文本文件,以便将其发送给特定用户。

这就是我的问题出现的地方。尝试了几个版本,尝试将我的字符串打印到文件中,但它有问题,我不知道与 echo 和 cat 相同,但仍然没有任何结果。

Basically I have to take the surname, lastname, username, and grade from a file and I have to send an email to that user ('username') that looks something like this:

"Dear 'surname' 'lastname'!
Your grade on the 'class name' exam was 'grade'."

The 'class name' is given via the parameter list of the shell, the rest of the info is in that file.

So I went step by step. First printed out with awk the desired layout

awk '{print  "Dear",$1,$2 "!","\nYour grade on the '$class' exam was ",$4;}' in.txt

Works fine. Then I tested if my mailing works and it does:

$ mail -s 'Subj' bando < /tmp/msg.txt

So I need to write my output to a textfile so I can send it to the specific user.

This is where my problems kicked in. I've tried several versions, tried printing my string to a file but there is something wrong with it and I don't know what. Same with echo and cat. Tried chopping it up in smaller pieces but still nothing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方。的韩爷 2024-11-07 15:32:19

尝试使用此 awk 命令读取 in.txt 并发送电子邮件:

class='9th'
awk '{msg="Dear " $1 " " $2 "!,\nYour grade on the '$class' exam was " $4; system("echo \"" msg "\" | mail -s 'Subj' " $3);}' in.txt

in.txt 文件如下所示:

cat in.txt
Lname Fname [email protected] 7.6

说明:

awk 命令将 in.txt 文件解析为 $1、$2、$3、$4占位符。

然后它在变量 msg 中格式化邮件消息。

最后调用系统函数执行echo "" | mail -s 'Subj' $3 来回显 msg 变量并将其通过管道传递给 mail 命令来发送。那里需要反斜杠来用双引号将 msg 变量括起来。

Try this awk command to read from in.txt and send an email:

class='9th'
awk '{msg="Dear " $1 " " $2 "!,\nYour grade on the '$class' exam was " $4; system("echo \"" msg "\" | mail -s 'Subj' " $3);}' in.txt

where as you in.txt file looks like this:

cat in.txt
Lname Fname [email protected] 7.6

Explanation:

awk command parses in.txt file into $1, $2, $3, $4 placeholders.

Then it formats the mail message in a variable msg.

Finally it calls system function to execute echo "<msg>" | mail -s 'Subj' $3 to echo msg variable and pipe it to mail command to send it. Backslashes are needed there to enclose msg variable with double quotes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文