Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
952 views
in Technique[技术] by (71.8m points)

bash - How to insert name field in mailx

My sample data File is

$ cat /fullpath/myfile.csv
a@gmail.com, A Singh
k@gmail.com, K Singh

I am using script.sh

   #!/bin/bash

while IFS= read -r line
do


email=$(echo $line | awk -F, '{print $1 }')
name=$(echo $line | awk -F, '{print $2 }')


echo | mailx -v -s "Helo $name" -S smtp-use-starttls -S ssl-verify=ignore -S smtp-auth=login -S smtp=smtp://smtp.gmail.com:587 -S from="xxxx@gmail.com(John Smith)" -S smtp-auth-user=xxxx@gmail.com -S  smtp-auth-password=xxxxpassword -S ssl-verify=ignore -S nss-config-dir=~/.certs "$name<$email>" 

done < /fullpath/myfile.csv

what is the correct syntax of adding receiver name


I am looking for syntax which I am not able to find I tried below

"$name<$email>"
$name<$email>
-S to:"$name<$email>"
-S To:"$name<$email>"
-S To: "$name <$email>"
-S To: $name <$email>

its picking names (A Singh) as email and say invalid email. if i use To, it pick TO as email. i.e. whatever come 1st after certs code pic that as email.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to the standard documentation, mailx does not seem to support the -S option, but some systems may add this option.

I recommend you use GNU Mailutils.

To specify a "FROM" name and address, you can use the "-a" option.

-a header:value

--append=header:value

Append the given header to the composed message.

To specify the receiver name and address, just like you did, add name and email to the end of the command will do the work.

#!/bin/bash

while IFS= read -r line
do

email=$(echo $line | awk -F, '{print $1 }')
name=$(echo $line | awk -F, '{print $2 }')

mail -s "Hello $name" -a "From: John Smith<xxxx@gmail.com>" "$name<$email>"
#echo | mailx -v -s "Helo $name" -S smtp-use-starttls -S ssl-verify=ignore -S smtp-auth=login -S smtp=smtp://smtp.gmail.com:587 -S from="xxxx@gmail.com(John Smith)" -S smtp-auth-user=xxxx@gmail.com -S  smtp-auth-password=xxxxpassword## -S ssl-verify=ignore -S nss-config-dir=~/.certs "$name<$email>" 

done < ./myfile.csv

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...