sendmail: passing subject from shell script - bash

i am using a template to send an html file in the body of an email.
now what i want to do is to pass the SUBJECT of the email via command like in the shell script.
My html file looks like this:
To: test#test.com
From: noreply#test.com
Subject: subject will change
Content-Type: text/html; charset="us-ascii"
<html>
this is test email body
</html>
bash script:
email=/usr/sbin/sendmail
report=/opt/html_report.html
template=/opt/email.template
$email -t < $final_report.html

I'm not sure the program sendmail is the one you want to use here. From the sendmail man page:
Sendmail is not intended as a user interface routine; other programs provide user-friendly front ends; sendmail is used only to deliver pre-formatted messages.
You probably want to use /bin/mail like this:
$ mail -s 'insert subject here' recipient#random.com < /opt/b2bpiv/email.template
Typing man mail or mail --help should list all the command-line options you'll need.

Related

mutt send email to variable list from file

I setup mutt and msmtp to send email via my google account.
Another script have a variable status.txt output like this:
newyork#mydomain.com
paris#mydomain.com
berlin#mydomain.com
How could I send email to this list, with status.txt content as body ?
Something like this:
mutt -s 'my list' $listemail < /pathto/status.txt
Thanks!
I found that this code worked for me:
while read line
do mutt $line -s 'subject-mylist' < /pahthto/status.txt
done < /pathto/listemails.txt
thanks for all!

sending text file as email with curl when text file has variable

I have a script that sends an email with curl. For example:
name="John"
curl...
I have a text file for my email template. The template will have the $name variable in it. For example:
Hello, $name. This is a test.
The curl part is fine but the problem is that it sends Hello, $name. This is a test instead of Hello, John. This is a test. I'm not sure how to do this. I've tried to search how to do it but I'm not even sure how to phrase the question. I keep turning up stuff on reading variables values from a files which isn't what I'm looking to do.
This fixed my issue:
eval message=$(cat test.txt) | echo $message| curl --netrc-file "/config/.netrc" --ssl-reqd --mail-from "<myemail#gmail.com>" --mail-rcpt "<youremail#gmail.com>" --url smtps://smtp.gmail.com:465 -T -

Mail the content of a plain text file from the shell

I'm using the following command to email the content a file from the shell:
mail -s 'Subject' to#domain.ext < file.txt
I always got a plain text mail with empty body and an attachment named "Subject.txt.dat" containing the content of file.txt.
I would like to get a plain text mail with the content of file.txt as body, and no attachments.
All tutorials I've found on Google suggests to use such command but in my case is not working as expected.
I'm on CentOS 6 and
man mail
shows it is using mailx.
This page seems to match your problem and offers some solutions:
https://access.redhat.com/solutions/1136493
Remove non-US-ASCII or non-printable characters from the e-mail text, or
use sendmail, which will accept and forward DOS-style formatted text, or
use mutt, which provides more funcionality regarding how the e-mail should be sent.

Unix Shell Script does not send intended email

I have a shell script like this. The purpose of this script is to tail+head out a certain amount of data from file.csv and then send it to email Bob#123.com. DataFunction seems to work fine alone however when I try to call DataFunction within the email function body. It seems it sends a empty email with the correct Title and destination. The body of the email is missing which should be the data from DataFunction. Is there a workaround for this ? Thank you in advance.
#!/bin/bash
DataFunction()
{
tail -10 /folder/"file.csv" | head -19
}
fnEmailFunction()
{
echo ${DataFunction}| mail -s Title Bob#123.com
}
fnEmailFunction
You are echoing an unset variable, $DataFunction (written ${DataFunction}), not invoking the function.
You should use:
DataFunction | mail -s Title Bob#123.com
You may have been trying to use:
echo $(DataFunction) | mail -s Title Bob#123.com
but that is misguided for several reasons. The primary problem is that it converts the 10 lines of output from the DataFunction function into a single line of input to mail. If you enclosed the $(DataFunction) in double quotes, that would preserve the 'shape' of the input, but it wastes time and energy compared to running the command (function) directly as shown.
Try this:
mail -s "Title" "bob#123com" <<EOF
${DataFunction}
EOF
I tried #0xAX's answer and couldnt get it to work properly within a bash script. You simply need to save the output of DataFunction() in some variable. You can simply use these three lines to achieve this.
#!/bin/bash
VAR1=`tail -10 "/folder/file.csv" | head -19`
echo $VAR1 | mail -s Title Bob#123.com

Showing special characters in bash echo

I am writing a bash script that reads the results of an sql query in which the results are output as HTML (using the -H option) to a file (using the -o option) and then sends those results in an email. When the results are output to the file, they come out as:
'<IDLE>'
But when I parse them from the output file they show up in the email as:
<IDLE>
Can anyone help me format these so I get the actual characters and not the entity representation?
EDIT: The way I am sending the text now is:
echo -e $EMAIL_TXT | mail -s $SUBJECT $RECIPIENT
And the way I am extracting the text from the html file ($OUT_FILE) is:
QRY_LINE=$(sed "${QRY_LNUM}q;d" $OUT_FILE)
I ended up just using string replace to replace all <s and >s with < and > respectively.

Resources