[abandoned] send mail from bash script - mailx

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
bulsatar
Level 2
Level 2
Posts: 97
Joined: Sun May 26, 2013 1:05 am
Contact:

[abandoned] send mail from bash script - mailx

Post by bulsatar »

So, another day, another couple hours trying one tutorial after another to try and get this script to work. I have everything working except the email part and almost, almost have that. I know this is a little lengthy. apologies.

My mountain:
When I do a command from the cli such as: echo "body stuff" | mailx -s "subjecthere" -a "path/to/picturefile.jpg" myemail@gmail.com Everything works just fine and it is sent. Even when I echo all of the arguments from my bash script, copy and paste it into a new mailx command in the cli, it works. When I just run the bash command, I get:
Send options without primary recipient specified.
Usage: mailx -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

My boulder:

Code: Select all

#!/bin/bash

#get flag values
while getopts p:v: option
do
  case "${option}"
  in
    p) PICTUREPATH=${OPTARG};;
    v) VIDEOPATH=${OPTARG};;
  esac
done

#Destination e-mail addresses
CELLTO="myemail@gmail.com"
EMAILTO="myotheremail@yahoo.com"
ARGS='-s "Motion detected" '

#check files exist before checking times
PICFILE='/home/bulsatar/.motion/lastpic.txt'
VIDFILE='/home/bulsatar/.motion/lastvid.txt'
if [[ ! -e "${PICFILE}" ]] 
then
    touch "${PICFILE}"
fi
if [[ ! -e "${VIDFILE}" ]] 
then
    touch "${VIDFILE}"
fi
NOW=$(date +"%Y%m%d%H%M%S")
#check for pic first
if [[ ! -z "${PICTUREPATH}" ]]
then
  #check if time frame is greater than (-gt) than 20 seconds
  PICLINE=$(head -n 1 "${PICFILE}")
  if [[ ! -z "${PICLINE}" ]]
  then
	if [[ $((${NOW} - ${PICLINE})) -gt 20 ]]
	then
	  echo ${NOW} > ${PICFILE}
	  ARGS+=' -a "'"${PICTUREPATH}"'" '"${CELLTO}"
	  echo "${ARGS}"
	  echo 'take a look' | mailx "${ARGS}"                                              <<<<<<<<this is where I am having trouble
	fi
  else
	echo ${NOW} > ${PICFILE}
  fi
fi

#check for vid next
if [[ ! -z "${VIDEOPATH}" ]]
then
  #check if time frame is greater than (-gt) than 1 minute
  VIDLINE=$(head -n 1 "${VIDFILE}")
  if [[ ! -z "${VIDLINE}" ]]
  then
	if [[ $((${NOW} - ${VIDLINE})) -gt 60 ]]
	then
	  echo ${NOW} > ${VIDFILE}
	  ARGS+=' -a "'"${VIDEOPATH}"'" '"${EMAILTO}"' < "/home/bulsatar/bin/Motion\ Setup\ Files/msg.txt"
	  echo "${ARGS}"
	  mailx "${ARGS}"                                              <<<<<<<<this is where I am having trouble
	fi
  else
	echo ${NOW} > ${VIDFILE}
  fi
fi
my other boulder (setupfiles for mailx):

Code: Select all

.mailrc
set smtp-use-starttls
set ssl-verify=ignore
set smtp=smtp://smtp.gmail.com:587
set smtp-auth=login
set smtp-auth-user=myemail@gmail.com
set smtp-auth-password=mypassword
set from="myemail@gmail.com"

.msmtprc
#Gmail account
defaults
logfile ~/msmtp.log
account gmail
auth on
host smtp.gmail.com
from myemail@gmail.com
auth on
tls on
tls_trust_file /usr/share/ca-certificates/mozilla/Equifax_Secure_CA.crt
user myemail@gmail.com
password mypassword
port 587
account default : gmail
Resources I have looked at to come up with this code:
http://www.fclose.com/1411/sending-emai ... ails-smtp/
http://tuxtweaks.com/2012/10/send-gmail ... mand-line/

Things I have tried so far:
as you can tell from the script, there are two different ways I am trying to set the body of the email, both with the same results
I have tried setting up postfix (everything that said "tutorial" was waaaayyy over my head to set that up)
I have tried setting up mutt (see above)
apparently ssmtp can't do attachments
mailutils (see postfix)

I am so close, please help!
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
bulsatar
Level 2
Level 2
Posts: 97
Joined: Sun May 26, 2013 1:05 am
Contact:

Re: send mail from bash script - mailx

Post by bulsatar »

I moved over to perl and enjoying writing in that MUCH more than bash. Got all the parts working, now just trying to figure out permissions when multiple scripts are involved to get everything working together.
User avatar
bjornmu
Level 3
Level 3
Posts: 189
Joined: Wed Dec 19, 2012 2:50 am
Location: Trondheim, Norway

Re: [abandoned] send mail from bash script - mailx

Post by bjornmu »

With all this quoting it can be hard to figure out what $ARGS ends up containing. You are echoing it for debugging but neglect to tell us what you see. That would help.
bulsatar
Level 2
Level 2
Posts: 97
Joined: Sun May 26, 2013 1:05 am
Contact:

Re: [solved] send mail from bash script - mailx

Post by bulsatar »

I don't remember exactly what the results were when echoing, but I remember feeling that they were correct. I still stuck with perl (and probably will in the future as it is much easier for me to use)

BUT

more importantly, I found out what was causing me so many troubles. Most of it boiled down to permission levels. So, I added myself to the Motion group and allowed that group to create/read everyplace that it was access scripts, including the mailx settings. Once I did that, I successfully created a 1 click launcher to start motion and firefox pointing to motion's output, then a second click to stop it. Wooohooo :D
Locked

Return to “Scripts & Bash”