sending an email as part of a bash shell script - bash

I have herd of examples of people using alpine to send emails based on data collected or generated from bash scripts.
I have been looking for tutorials to create this for myself with no success. I do have alpine talking to my email server, but requires me to still enter a password. I am guessing I will need to correct this as well.
My end result would be to run a long task such as do a time lapse on my Pi3, and send an email... perform some imagemagick work, render a video on my servers gpu, send an email... it would be easier for me to check emails looking for machines reporting in completion of tasks, sending logs as well for review etc... I got all of this set except the darn email.
Any help would be appreciated

I'm using this method to send mail in bash one-liner via smtp public service using "nail" mail-client:
echo 'Test mail body text' | env MAILRC=/dev/null from='a#a.ru' smtp=mail.a.ru smtp-auth-user='a' smtp-auth-password='xxx' smtp-auth=login mailx -v -n -s 'Test mail again v01' b#b.ru

You can try using mutt
To install in Ubuntu
sudo apt-get install mutt
To send an email with attachment
echo "Please find the file attached." | mutt -a somefile.ext -s "Important File"

Related

Behavior of sending email with text and attachment changed in Ubuntu 20.04 for mail from mailutils

In Ubuntu 16.04 I used the following command to send an email with text and attachements, which worked fine.
echo "body" | mail -s "Subject" -A "/path/to/attachment" "email#adress"
In Ubuntu 20.04 this command attaches the body-part as an attachment too. The mail --version is 3.7.
Sending only the attachments or only the body is working fine.
This question is about mail from mailutils not mutt nor mailx nor sendmail, etc.
A solution is very very welcome. Thx a lot!
Thanks a lot to #jhnc for all the information!
This behavior is a bug in mail 3.7 in the package mailutils and adressed in launchpad by
1911894.
But as of today it is unassigned! Not likely to be changed soon.
But Ubuntu 20.04 is a LTS version and sending mail with text and attachements is quite common these days.
If you come along and have the same problem, please fill in a bug report, which is quit easy and helps to increase the importance.
To generate a bug report run in a terminal
apport-cli mailutils
and send it with S to launchpad.
Open browser with 1 and login.
Then enter 1911894 to find the inital bug thread and join. THX!
Another simple 'solution' is to install the latest mailutils-3.15. This issue is fixed as I verified.
Download link:
https://mailutils.org/download.html
To add to #onemorequestion's answer, I found a workaround in the mailing list for the bug:
you can use the --mime option as a workaround:
echo "body text" | /usr/bin/mail --mime -s "some subject" -A "somefile.csv" my#email.com
This adds the body text as an attachment, but it's better than nothing!
Attachement with uuencode works fine with UB20.04
( cat body-message.txt; uuencode input-attachment.txt output-attachment.txt ) \
| mail -s "Email With Body Text and Attachment" ****#gmail.com

Bash script with sendmail delivers email when executed manually but not from crontab

I wrote the following bash script to send me an alert if there is a problem with my website:
#!/bin/bash
# 1. download the page
BASE_URL="https://www.example.com/ja"
JS_URL="https://www.example.com/"
# # 2. search the page for the following URL: /sites/default/files/google_tag/google_tag.script.js?[FIVE-CHARACTER STRING WITH LETTERS AND NUMBERS]
curl -k -L ${BASE_URL} 2>/dev/null | grep -Eo "/sites/default/files/google_tag/google_tag.script.js?[^<]+" | while read line
do
# 3. download the js file
if curl -k -L ${JS_URL}/$line | grep gtm_preview >/dev/null 2>&1; then
# 4. check if this js file has the text "gtm_preview" or not; if it does, send an email
# echo "Error: gtm_preview found"
sendmail error-ec2#example.com < email-gtm-live.txt
else
echo "No gtm_preview tag found."
fi
done
I am running this from an Amazon EC2 Ubuntu instance. When I execute the script manually like ./script.sh, I receive an email in my webmail inbox for example.com.
However, when I configure this script to run via crontab, the mail does not get sent via the Internet; instead, it gets sent to /var/mail on the EC2 instance.
I don't understand why this is happening or what I can do to fix it. Why does sendmail behave different if it is being run from bash vs being run from crontab?
Be aware that the PATH environment variable is different for crontab executions than it is for your typical interactive sessions. Also, not all of the same environment variables are set. Consider specifying the full path for the sendmail executable ( which you can learn by issuing the 'which sendmail' command ).

How to get SFTP login alert (email or SMS) on ubuntu?

I want to get Email or/and SMS alert whenever someone logs into my server. I have successfully done this for SSH login by editing .bashrc file.
But this method doesn't work for SFTP login.
I know I can do this by extracting information from log files, but i want a more efficient way (Since users can delete from log file).
I have also tried this, but it doesn't work.(I'm using ubuntu 12.04).
Basically, I want to execute a curl command (SMS API) on successful login.
Please help, thank you.
First, make sure you have the sendmail package (sudo apt-get install sendmail).
You can create or edit the file /etc/ssh/sshrc and give it the following code to achieve this:
ip=`echo $SSH_CONNECTION | cut -d " " -f 1`
logger -t ssh-wrapper $USER login from $ip echo "User $USER logged in from $ip" | sendemail -q -u "Email Title" -f "Sender <from#server.com>" -t "Your Name <you#email.com>" -s smtp.server.com &
Fill the appropriate variables (sender and recipient names and addresses) into this code.

Sending mail with mailx and windows

I installed MKS ToolKit 8.5 in windows 7 and i want to send an email using mailx, i tried this in linux and works fine, but in Windows i dont have the same result. I'm trying send mail with a external smtp server. This is a command i used to sending mail from linux:
echo "This is a test..." | mailx -v -s "Test Email" -S smtp=my.server.smtp:25 -S from=test#test.cl me#gmail.com
I need this command in windows but can't yet run the right way of.
Please help me.
Thanks.

shell script to check ftp and sftp communication

First I have to say you that I'm a newbie, but I wanna learn!
I need a script which suppose to check the ftp and sftp communication. This is for 26 ftp and sftp.
Script has to execute just 1 times and script suppose to login to ftp and sftp servers using username with password, easy from an extern list.
If ftp works also it has to send an mail and even if it is fail also it suppose to send the mail. Or just a print on screen with status : alive or not alive should be ok.
I am starting with the below script:
I found something like :
"You can use your script with a regular user, creating the file .netrc in the user's homedir (~/.netrc), with the following contents:
Code:
machine 192.1.1.1
login usename
password user-passwd"
...
ftp -v -n <<EOF > ${LOG_FTP} 2>&1
open ${IP_ADDRESS_SERVER}
user ${FTPUSER} ${FTPPASS}
...
EOF
I need now to understand how I can send the email to my email adres or just print on screen the results.
Please can you suggest me the right way or help me write the script.
It would be great help for me.
Thanks in advance for all your help!
Nico
you can use sendmail for sending emails to your email id. IF you're working on bash, then pre-pend this line to your script:
#!/bin/bash
Then do this on terminal:
chmod +x <scriptname> #setting executable permission for script
./<scriptname> #executing the script

Resources