Bash: Sending HTML with an attached file? - html

I'm looking for a way to send an HTML email from bash with an attached file.
I've tried the following line, but it doesn't work 100%. The below line sends an HTML email, but there is no attached file:
( cat body.html ; uuencode in-attachfile.txt out-attachfile.txt ) | mail -s "$(echo -e "my subject\nContent-Type: text/html")" foo#example.com
If I remove the Content-Type: text/html to indicate this is an HTML email then the attachment works:
( cat body.html ; uuencode in-attachfile.txt out-attachfile.txt ) | mail -s "$(echo -e "my subject")" foo#example.com
How can I have both?
Thank you

Try this:
( cat body.html; uuencode in-attachfile.txt out-attachfile.txt ) | mail -s "my subject" -a "Content-Type: text/html" foo#example.com
You may want to send the attachment using MIME (via mutt, for example). See this for more information.

Related

How to attach several files in mail using unix command?

I'm implementing a script to backup my MySQL database. All process is OK and I send an email when it finish. But I want to attach the file in that email and I don't know how to do it.
My command line is:
mail -s "$1" -a "MIME-Version: 1.0;" -a "Content-type: text/html;" root#$domain -c ops#mydomain.com < $2
Where $1 = My subject and $2 = my message body
Thanks!
You are very close. You can use mail command to send 1 attachment as follow (you'd better TAR / ZIP your files before sending):
echo "$2" | mail -s "$1" -a /path/to/file.tar.gz ops#mydomain.com
Next, if you want to have more features, you can use mutt (install with apt-get install mutt):
mutt -s "$1" -a /path/to/file1.tar.gz -a /path/to/file2.tar.gz -a /path/to/file3.tar.gz ops#mydomain.com < /tmp/mailbody.txt
where:
file1.tar.gz to file3.tar.gz are file attachments
ops#mydomain.com is recipient
mailbody.txt is the contents of email
or use uuencode (install with apt-get install sharutils):
uuencode /path/to/file.tar.gz /path/to/file.tar.gz | mailx -s "$1" ops#mydomain.com
Note:
you have to repeat the file.tar.gz twice (read uuencode documentation for more information)
mailx is a newer version of mail, but still an ancient command
to send multiple attachments with mail command (well, if you insist):
$ uuencode file1.tar.gz file1.tar.gz > /tmp/out.mail
$ uuencode file2.tar.gz file3.tar.gz >> /tmp/out.mail
$ uuencode file3.tar.gz file3.tar.gz >> /tmp/out.mail
$ cat email-body.txt >> /tmp/out.mail
$ mail -s "$1" ops#mydomain.com < /tmp/out.mail
Hope the above helps.

Bash script output JSON variable to file

I am using twurl in Ubuntu's command line to connect to the Twitter Streaming API, and parse the resulting JSON with this processor. I have the following command, which returns the text of tweets sent from London:
twurl -t -d locations=-5.67,50.06,1.76,58.62 language=en -H stream.twitter.com /1.1/statuses/filter.json | jq '.text'
This works great, but I'm struggling to output the result to a file called london.txt. I have tried the following, but still no luck:
twurl -t -d locations=-5.67,50.06,1.76,58.62 language=en -H stream.twitter.com /1.1/statuses/filter.json | jq '.text' > london.txt
As I'm fairly new to Bash scripting, I'm sure I've misunderstood the proper use of '>' and '>>', so if anyone could point me in the right direction that'd be awesome!
twurl -t -d locations=-5.67,50.06,1.76,58.62 language=en -H stream.twitter.com /1.1/statuses/filter.json | jq '.text' > london.txt
It will replace on each new line pasting on it. But if you use >> it will append the next write operation to end of file. So try the following rather above example, I'm certain it will work.
twurl -t -d locations=-5.67,50.06,1.76,58.62 language=en -H stream.twitter.com /1.1/statuses/filter.json | jq '.text' >> london.txt
Also you can use tee command to check what is printing along side the redirection
twurl -t -d locations=-5.67,50.06,1.76,58.62 language=en -H stream.twitter.com /1.1/statuses/filter.json | jq '.text'| tee london.txt

How to send html files as email? [duplicate]

I need to send email with html format. I have only linux command line and command "mail".
Currently have used:
echo "To: address#example.com" > /var/www/report.csv
echo "Subject: Subject" >> /var/www/report.csv
echo "Content-Type: text/html; charset=\"us-ascii\"" >> /var/www/report.csv
echo "<html>" >> /var/www/report.csv
mysql -u ***** -p***** -H -e "select * from users LIMIT 20" dev >> /var/www/report.csv
echo "</html>" >> /var/www/report.csv
mail -s "Built notification" address#example.com < /var/www/report.csv
But in my mail-agent i get only plain/text.
This worked for me:
echo "<b>HTML Message goes here</b>" | mail -s "$(echo -e "This is the subject\nContent-Type: text/html")" foo#example.com
My version of mail does not have --append and it too smart for the echo -e \n-trick (it simply replaces \n with space). It does, however, have -a:
mail -a "Content-type: text/html" -s "Built notification" address#example.com < /var/www/report.html
Send email using command Line
This answer is over 11 years old, these days I use python's import ezgmail for a 4 line plug, auth and play solution
Create a file named tmp.html with the following contents:
<b>my bold message</b>
Next, paste the following into the command line (parentheses and all):
(
echo To: youremail#blah.com
echo From: el#defiant.com
echo "Content-Type: text/html; "
echo Subject: a logfile
echo
cat tmp.html
) | sendmail -t
The mail will be dispatched including a bold message due to the <b> element.
Shell Script
As a script, save the following as email.sh:
ARG_EMAIL_TO="recipient#domain.com"
ARG_EMAIL_FROM="Your Name <you#host.com>"
ARG_EMAIL_SUBJECT="Subject Line"
(
echo "To: ${ARG_EMAIL_TO}"
echo "From: ${ARG_EMAIL_FROM}"
echo "Subject: ${ARG_EMAIL_SUBJECT}"
echo "Mime-Version: 1.0"
echo "Content-Type: text/html; charset='utf-8'"
echo
cat contents.html
) | sendmail -t
Create a file named contents.html in the same directory as the email.sh script that resembles:
<html><head><title>Subject Line</title></head>
<body>
<p style='color:red'>HTML Content</p>
</body>
</html>
Run email.sh. When the email arrives, the HTML Content text will appear red.
Related
How to send a html email with the bash command "sendmail"?
On OS X (10.9.4), cat works, and is easier if your email is already in a file:
cat email_template.html | mail -s "$(echo -e "Test\nContent-Type: text/html")" karl#marx.com
The problem is that when redirecting a file into 'mail' like that, it's used for the message body only. Any headers you embed in the file will go into the body instead.
Try:
mail --append="Content-type: text/html" -s "Built notification" address#example.com < /var/www/report.csv
--append lets you add arbitrary headers to the mail, which is where you should specify the content-type and content-disposition. There's no need to embed the To and Subject headers in your file, or specify them with --append, since you're implicitly setting them on the command line already (-s is the subject, and address#example.com automatically becomes the To).
With heirloom-mailx you can change sendmail program to your hook script, replace headers there and then use sendmail.
The script I use (~/bin/sendmail-hook):
#!/bin/bash
sed '1,/^$/{
s,^\(Content-Type: \).*$,\1text/html; charset=utf-8,g
s,^\(Content-Transfer-Encoding: \).*$,\18bit,g
}' | sendmail $#
This script changes the values in the mail header as follows:
Content-Type: to text/html; charset=utf-8
Content-Transfer-Encoding: to 8bit (not sure if this is really needed).
To send HTML email:
mail -Ssendmail='~/bin/sendmail-hook' \
-s "Built notification" address#example.com < /var/www/report.csv
Very old question, however it ranked high when I googled a question about this.
Find the answer here:
Sending HTML mail using a shell script
I was struggling with similar problem (with mail) in one of my git's post_receive hooks and finally I found out, that sendmail actually works better for that kind of things, especially if you know a bit of how e-mails are constructed (and it seems like you know). I know this answer comes very late, but maybe it will be of some use to others too. I made use of heredoc operator and use of the feature, that it expands variables, so it can also run inlined scripts. Just check this out (bash script):
#!/bin/bash
recipients=(
'john#example.com'
'marry#not-so-an.example.com'
# 'naah#not.this.one'
);
sender='highly-automated-reporter#example.com';
subject='Oh, who really cares, seriously...';
sendmail -t <<-MAIL
From: ${sender}
`for r in "${recipients[#]}"; do echo "To: ${r}"; done;`
Subject: ${subject}
Content-Type: text/html; charset=UTF-8
<html><head><meta charset="UTF-8"/></head>
<body><p>Ladies and gents, here comes the report!</p>
<pre>`mysql -u ***** -p***** -H -e "SELECT * FROM users LIMIT 20"`</pre>
</body></html>
MAIL
Note of backticks in the MAIL part to generate some output and remember, that <<- operator strips only tabs (not spaces) from the beginning of lines, so in that case copy-paste will not work (you need to replace indentation with proper tabs). Or use << operator and make no indentation at all. Hope this will help someone. Of course you can use backticks outside o MAIL part and save the output into some variable, that you can later use in the MAIL part — matter of taste and readability. And I know, #!/bin/bash does not always work on every system.
I found a really easy solution: add to the mail command the modifier -aContent-Type:text/html.
In your case would be:
mail -aContent-Type:text/html -s "Built notification" address#example.com < /var/www/report.csv
you should use "append" mode redirection >> instead of >
Try with :
echo "To: address#example.com" > /var/www/report.csv
echo "Subject: Subject" >> /var/www/report.csv
echo "MIME-Version: 1.0" >> /var/www/report.csv
echo "Content-Type: text/html; charset=\"us-ascii\"" >> /var/www/report.csv
echo "Content-Disposition: inline" >> /var/www/report.csv
echo "<html>" >> /var/www/report.csv
mysql -u ***** -p***** -H -e "select * from users LIMIT 20" dev >> /var/www/report.csv
echo "</html>" >> /var/www/report.csv
mail -s "Built notification" address#example.com < /var/www/report.csv

Bash Script Loop through MySQL row and use curl and grep

I have a mysql database, with a table :
url | words
And datas like, for example :
------Column URL------- -------Column Words------
www.firstwebsite.com | hello, hi
www.secondwebsite.com | someword, someotherword
I want to loop through that table to check if the word is present in the content of the website specified by the url.
I have something like this :
!/bin/bash
mysql --user=USERNAME --password=PASSWORD DATABASE --skip-column-names -e "SELECT url, keyword FROM things" | while read url keyword; do
content=$(curl -sL $url)
echo $content | egrep -q $keyword
status=$?
if test $status -eq 0 ; then
# Found...
else
# Not found...
fi
done
One problems :
It's very slow : how set curl to optimize the load time of each website, don't load images, things like that ?
Also, Is it a good idea to put things like that in a shell script, or is it better to create a php script, and call it with curl ?
Thanks !
As it stands your script will not work as you might expect when you have multiple keywords per row as in your example. The reason is that when you pass hello, hi to egrep it will look for the exact string "hello, hi" in its input, not for either "hello" or "hi". You can fix this without making changes to what's in your database by turning each list of keywords into an egrep-compatible regular expression with sed. You'll also need to remove the | from mysql's output, e.g, with awk.
curl doesn't retrieve images when downloading a webpage's HTML. If the order in which the URLs are queried does not matter to you then you can speed things up by making the whole thing asynchronous with &.
#!/bin/bash
handle_url() {
if curl -sL "$1" | egrep -q "$2"; then
echo 1 # Found...
else
echo 0 # Not found...
fi
}
mysql --user=USERNAME --password=PASSWORD DATABASE --skip-column-names -e "SELECT url, keyword FROM things" | awk -F \| '{ print $1, $2 }' | while read url keywords; do
keywords=$(echo $keywords | sed -e 's/, /|/g;s/^/(/;s/$/)/;')
handle_url "$url" "$keywords" &
done

How to send HTML email using linux command line

I need to send email with html format. I have only linux command line and command "mail".
Currently have used:
echo "To: address#example.com" > /var/www/report.csv
echo "Subject: Subject" >> /var/www/report.csv
echo "Content-Type: text/html; charset=\"us-ascii\"" >> /var/www/report.csv
echo "<html>" >> /var/www/report.csv
mysql -u ***** -p***** -H -e "select * from users LIMIT 20" dev >> /var/www/report.csv
echo "</html>" >> /var/www/report.csv
mail -s "Built notification" address#example.com < /var/www/report.csv
But in my mail-agent i get only plain/text.
This worked for me:
echo "<b>HTML Message goes here</b>" | mail -s "$(echo -e "This is the subject\nContent-Type: text/html")" foo#example.com
My version of mail does not have --append and it too smart for the echo -e \n-trick (it simply replaces \n with space). It does, however, have -a:
mail -a "Content-type: text/html" -s "Built notification" address#example.com < /var/www/report.html
Send email using command Line
This answer is over 11 years old, these days I use python's import ezgmail for a 4 line plug, auth and play solution
Create a file named tmp.html with the following contents:
<b>my bold message</b>
Next, paste the following into the command line (parentheses and all):
(
echo To: youremail#blah.com
echo From: el#defiant.com
echo "Content-Type: text/html; "
echo Subject: a logfile
echo
cat tmp.html
) | sendmail -t
The mail will be dispatched including a bold message due to the <b> element.
Shell Script
As a script, save the following as email.sh:
ARG_EMAIL_TO="recipient#domain.com"
ARG_EMAIL_FROM="Your Name <you#host.com>"
ARG_EMAIL_SUBJECT="Subject Line"
(
echo "To: ${ARG_EMAIL_TO}"
echo "From: ${ARG_EMAIL_FROM}"
echo "Subject: ${ARG_EMAIL_SUBJECT}"
echo "Mime-Version: 1.0"
echo "Content-Type: text/html; charset='utf-8'"
echo
cat contents.html
) | sendmail -t
Create a file named contents.html in the same directory as the email.sh script that resembles:
<html><head><title>Subject Line</title></head>
<body>
<p style='color:red'>HTML Content</p>
</body>
</html>
Run email.sh. When the email arrives, the HTML Content text will appear red.
Related
How to send a html email with the bash command "sendmail"?
On OS X (10.9.4), cat works, and is easier if your email is already in a file:
cat email_template.html | mail -s "$(echo -e "Test\nContent-Type: text/html")" karl#marx.com
The problem is that when redirecting a file into 'mail' like that, it's used for the message body only. Any headers you embed in the file will go into the body instead.
Try:
mail --append="Content-type: text/html" -s "Built notification" address#example.com < /var/www/report.csv
--append lets you add arbitrary headers to the mail, which is where you should specify the content-type and content-disposition. There's no need to embed the To and Subject headers in your file, or specify them with --append, since you're implicitly setting them on the command line already (-s is the subject, and address#example.com automatically becomes the To).
With heirloom-mailx you can change sendmail program to your hook script, replace headers there and then use sendmail.
The script I use (~/bin/sendmail-hook):
#!/bin/bash
sed '1,/^$/{
s,^\(Content-Type: \).*$,\1text/html; charset=utf-8,g
s,^\(Content-Transfer-Encoding: \).*$,\18bit,g
}' | sendmail $#
This script changes the values in the mail header as follows:
Content-Type: to text/html; charset=utf-8
Content-Transfer-Encoding: to 8bit (not sure if this is really needed).
To send HTML email:
mail -Ssendmail='~/bin/sendmail-hook' \
-s "Built notification" address#example.com < /var/www/report.csv
Very old question, however it ranked high when I googled a question about this.
Find the answer here:
Sending HTML mail using a shell script
I was struggling with similar problem (with mail) in one of my git's post_receive hooks and finally I found out, that sendmail actually works better for that kind of things, especially if you know a bit of how e-mails are constructed (and it seems like you know). I know this answer comes very late, but maybe it will be of some use to others too. I made use of heredoc operator and use of the feature, that it expands variables, so it can also run inlined scripts. Just check this out (bash script):
#!/bin/bash
recipients=(
'john#example.com'
'marry#not-so-an.example.com'
# 'naah#not.this.one'
);
sender='highly-automated-reporter#example.com';
subject='Oh, who really cares, seriously...';
sendmail -t <<-MAIL
From: ${sender}
`for r in "${recipients[#]}"; do echo "To: ${r}"; done;`
Subject: ${subject}
Content-Type: text/html; charset=UTF-8
<html><head><meta charset="UTF-8"/></head>
<body><p>Ladies and gents, here comes the report!</p>
<pre>`mysql -u ***** -p***** -H -e "SELECT * FROM users LIMIT 20"`</pre>
</body></html>
MAIL
Note of backticks in the MAIL part to generate some output and remember, that <<- operator strips only tabs (not spaces) from the beginning of lines, so in that case copy-paste will not work (you need to replace indentation with proper tabs). Or use << operator and make no indentation at all. Hope this will help someone. Of course you can use backticks outside o MAIL part and save the output into some variable, that you can later use in the MAIL part — matter of taste and readability. And I know, #!/bin/bash does not always work on every system.
I found a really easy solution: add to the mail command the modifier -aContent-Type:text/html.
In your case would be:
mail -aContent-Type:text/html -s "Built notification" address#example.com < /var/www/report.csv
you should use "append" mode redirection >> instead of >
Try with :
echo "To: address#example.com" > /var/www/report.csv
echo "Subject: Subject" >> /var/www/report.csv
echo "MIME-Version: 1.0" >> /var/www/report.csv
echo "Content-Type: text/html; charset=\"us-ascii\"" >> /var/www/report.csv
echo "Content-Disposition: inline" >> /var/www/report.csv
echo "<html>" >> /var/www/report.csv
mysql -u ***** -p***** -H -e "select * from users LIMIT 20" dev >> /var/www/report.csv
echo "</html>" >> /var/www/report.csv
mail -s "Built notification" address#example.com < /var/www/report.csv