2022年10月30日 星期日

To send colon in script using msmtp

In an project of building a file server for a small office and which should be able to access outside the office via Internet. The office did not have a public IP. It was decided to setup a nextcloud server together with a samba server and send the dynamic public IP obtained from the ISP to related staff in a email each time it started.

Msmtp was installed and configured properly for sending the required email in a script. The simple email just consisted of the subject and the IP as the body. The command is 

printf "Subject: NextCloud Server New IP\n${myip}" | msmtp xxx@gmail.com

It worked. In order to improve the performance, it would be better to send a link to the server rather than the IP itself. So, the command was modified to

printf "Subject: NextCloud Server New IP\nhttps://${myip}" | msmtp xxx@gmail.com

Here came the problem, the email had still to be send, but nothing in the body. There seemed do not have any syntax error. The printf function prints exactly what is needed. The problem should be on msmtp.

Google the great came to help. Not many articles related were suggested from Google. Colon cannot be send with msmtp is still an open question. Some suggested to prepare a text file for the email first and then send the text file with msmtp. So, text files were prepared and it worked once and only once. It was rather strange. After trying to make it work again, many many times, one noticed that at the time it worked, the text file was copied from somewhere and consisted many blank lines before the body text. So several blank was then inserted before the https: and tried again. It worked and could be duplicated. The final command is

printf "Subject: NextCloud Server New IP\n\n\n\n\n\nhttps://${myip}" | msmtp xxx@gmail.com

No attempt was made to see at least how many blank lines are needed. At least it worked for what was needed. 

The conclusion is that msmtp uses blank lines to distinguish the header and the body of an email. If the colon is inside the body, it will be handled properly.

沒有留言:

張貼留言