如何从 Linux 中的文件向用户发送具有特定模板的邮件(模板包含文件中的信息)?
基本上,我必须从文件中获取姓氏、姓氏、用户名和年级,并且必须向该用户(“用户名”)发送一封电子邮件,如下所示:
“亲爱的‘姓氏’‘姓氏’!
您在“班级名称”考试中的成绩为“等级”。”
‘班级名称’是通过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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用此 awk 命令读取 in.txt 并发送电子邮件:
in.txt
文件如下所示:说明:
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:
where as you
in.txt
file looks like this: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.