#! /usr/bin/env bash # User input sender=typebrook@gmail.com receiver=typebrook@gmail.com gapp=$(pass google/imap_for_typebrook) sub="$(date) test mail from curl" body="$(echo -e 'just\na\ntest\nmail')" # check for provided attachment file as a positional parameter # -z indicating an empty positional parameter # attachment doesn't exist condition if [ -z "$1" ]; then # curl command for accessing the smtp server curl -s --url 'smtps://smtp.gmail.com:465' --ssl-reqd \ --mail-from $sender \ --mail-rcpt $receiver --user $sender:$gapp \ -T <(echo -e "From: ${sender} To: ${receiver} Subject: ${sub} ${body}") # attachment exists condition else file="$1" # set file as the 1st positional parameter # MIME type for multiple type of input file extensions MIMEType=$(file --mime-type "$file" | sed 's/.*: //') curl -s --url 'smtps://smtp.gmail.com:465' --ssl-reqd \ --mail-from $sender \ --mail-rcpt $receiver --user $sender:$gapp \ -H "Subject: $sub" -H "From: $sender" -H "To: $receiver" -F \ '=(;type=multipart/mixed' -F "=$body;type=text/plain" -F \ "file=@$file;type=$MIMEType;encoder=base64" -F '=)' fi