How to mail from a bash script?

Ask questions about Arch Linux ARM. Please search before making a new topic.

How to mail from a bash script?

Postby silentcreek » Fri Jan 25, 2013 10:13 am

Hi,

I'm still fairly new to ArchLinuxARM but so far I'm really happy with what I can do with it. At the moment I'm working on a script that will send me automatic notifications via email. My problem is, I have never sent an email from bash :D

I saw that alarm comes with mailx. I read somewhere else that mailx can be used to send emails over SMTP. (Let me know if I'm wrong.)
Where do I have to put my SMTP server and account details? In some conf file or do I put them in the parameters of the command itself?
Btw, are mail and mailx on alarm the same? I tried 'man mail' and it showed me the man pages for mailx (which were so extensive that they were more confusing rather than helping, at least given that I didn't have a lot of time last night...)

Note: Since I only need to send a few notification emails, I thought I won't need a fully loaded mail server on my Arm device ;)

Thanks a lot,

Timo
silentcreek
 
Posts: 21
Joined: Mon Jan 07, 2013 8:38 pm

Re: How to mail from a bash script?

Postby pepedog » Fri Jan 25, 2013 1:58 pm

Install say postfix, and service is running
$this->bbcode_second_pass_code('', 'mail -eiIUdEFntBDNHRV~ -T FILE -u USER -h hops -r address -s SUBJECT -a FILE -q FILE -f FILE -A ACCOUNT -b USERS -c USERS -S OPTION users')
Example
mail -s "This is subject line" address@tosendto.com
Nothing will be in body
pepedog
Developer
 
Posts: 2431
Joined: Mon Jun 07, 2010 3:30 pm
Location: London UK

Re: How to mail from a bash script?

Postby silentcreek » Fri Jan 25, 2013 9:02 pm

Thanks for your answer. But since I try to be as minimalistic as possible (i.e. not need to install a full featured mail server), I did some further research after work and played with mailx for a while.

As a result, I found out how to send an email without any additional packages and without changes in any conf files:
$this->bbcode_second_pass_code('', 'echo "PUT YOUR MAIL BODY HERE" | mailx -s "SUBJECT" -S smtp=smtp://yoursmtpserver.com -S smtp-auth=login -S smtp-auth-user=YOUR_USERNAME -S smtp-auth-password=YOUR_PASSWORD -S from="Sender Name <sender@mail.com>" recipient@mail.com')

Two things I'm still working on:
1) I haven't managed to use a text/log file for the mail body yet. I tried
$this->bbcode_second_pass_code('', 'cat mylogfile.log | mailx [...]')
but for some reason the log ends up as an attachment without name in the recipient's client. Thunderbird recognizes identifies the MIME type as application/octet-stream. If I open it in a text editor, I can see my log file. But, I don't understand yet, why it's not being displayed in the mail body. If I try 'cat mylogfile.log' alone, my log file will be displayed correctly. Maybe it's an encoding issue, since the log file was created on a windows client... I will do some further testing (but of course, hints and tips are welcome).

2) I haven't tried yet, how to use STARTTLS but I found some options in the man pages that I will test soon.

Thanks,

Timo
silentcreek
 
Posts: 21
Joined: Mon Jan 07, 2013 8:38 pm

Re: How to mail from a bash script?

Postby moonman » Sat Jan 26, 2013 10:26 am

you probably need something like this: echo $(cat mylog.log)
Pogoplug V4 | GoFlex Home | Raspberry Pi 4 4GB | CuBox-i4 Pro | ClearFog | BeagleBone Black | Odroid U2 | Odroid C1 | Odroid XU4
-----------------------------------------------------------------------------------------------------------------------
[armv5] Updated U-Boot | [armv5] NAND Rescue System
moonman
Developer
 
Posts: 3388
Joined: Sat Jan 15, 2011 3:36 am

Re: How to mail from a bash script?

Postby silentcreek » Sat Jan 26, 2013 11:20 am

Thanks, I will try that, too.

I got it to work last night, after testing a bit more. My guess about the Windows/UNIX issue was correct. I installed the package dos2unix and when I convert the log file I have no issues having the content of the logfile displayed in the mail body. I also made STARTTLS work by simply adding an option and attaching the port number to the server address:
$this->bbcode_second_pass_code('', '-S smtp-use-starttls -S smtp=smtp://yoursmtpserver.com:portnumber')

Timo
silentcreek
 
Posts: 21
Joined: Mon Jan 07, 2013 8:38 pm


Return to User Questions

Who is online

Users browsing this forum: No registered users and 9 guests