提问人:toddehb 提问时间:10/25/2023 最后编辑:toddehb 更新时间:10/25/2023 访问量:37
在 bash 中生成 html 邮件以与 mail 命令一起使用
Generate html mail in bash to use with mail command
问:
我创建了一个简单的脚本,其中通过邮件发送特定信息,如下所示:
printf "Dear $i, please enable 2fa on $site.\n\n Your inital code: \n $code \n\n\n For detailed instructions visit:\n https://docs.nextcloud.com/server/latest/user_manual/en/user_2fa.html \n\n You must complete the above steps from instructions, otherwise you will not be able to log into your account a second time!\n\n Regards - Your Site Administrator from $site" | mail -s "Urgent notice from $site" $email -r $mail_from
我宁愿将这封邮件作为带有预定义变量的 html 发送。尝试创建一个简单的 html 文件,将其保存在本地并通过 cat 在脚本中读取:
template=`cat ./mail_template
mail_template的内容是:
Dear $i,
please enable 2fa on $site. This is very important to protect your identity.
Your inital code for logging in to your account the next time:
$code
For detailed instructions please visit:
https://docs.nextcloud.com/server/latest/user_manual/en/user_2fa.html
You must complete the above steps from instructions, otherwise you will not be able to log into your account a second time!
In short you need to do the following steps:
1.) Log in to your account on $site using above code
2.) Enable 2FA as shown in the instruction ( link above )
3.) Enable security tokens as shown in instructions ( link above )
<bold>Attention!!!</bold>
Don't save security tokens to your Nextcloud File Storage. Put them in another safe( preferably encrypted ) place, or print them out and store them in a vault.
Regards - Your Site Administrator from $site
我在 mail_template 中包含了 html 和 body 标签。无法在此处显示它们,因为它搞砸了格式。
那么新的邮件语句是这样的:
printf "$template" | mail -a 'Content-Type: text/html' -s "Urgent notice from $site" $email -r $mail_from
不幸的是,这不起作用。mail_template中的变量被写成普通字符串,html标签也无法识别。
有什么方法如何以正确的方式做到这一点吗?谢谢。
干杯,托德布
答:
您可以像这样用于当前模板:envsubst
i=John
site=foo.com
code=123456
template=$(i="$i" site="$site" code="$code" envsubst < ./mail_template)
echo "$template"
Dear John,
please enable 2fa on foo.com. This is very important to protect your identity.
Your inital code for logging in to your account the next time:
123456
For detailed instructions please visit:
...
或者更改模板以使其适合打印,如下所示:
template=$(cat ./mail_template)
echo "$template"
Dear %s,
please enable 2fa on %s. This is very important to protect your identity.
Your inital code for logging in to your account the next time:
%s
For detailed instructions please visit:
...
然后像这样使用它:printf
printf "$template" "$i" "$site" "$code" | ...
另一种方法是使用 ,像这样更改模板:sed
Dear _name_,
please enable 2fa on _site_. This is very important to protect your identity.
Your inital code for logging in to your account the next time:
_code_
For detailed instructions please visit:
...
并用于粘贴实际数据,而不是名称、站点和代码:sed
template=$(sed "s/_name_/$i/;s/_site_/$site/;s/_code_/$code/" ./mail_template)
评论
text/plain
text/html