Sendmail bash script read text file and add hyperlink - linux

Below I have partial of my script. I can send email using this, my only problem is when I read text file (body.txt) it ignores new lines, because of that my body email send one big line.
if I remove
Mime-version:1.0
Content-type:text/html"
Than I it does understand new line on text file. However this time by hyperlinks because plain text, when I want to be html.
links=<a href='deleteserid=test#gmail.com'>Unsubscribe</a>
xmail="/usr/sbin/sendmail"
# Fetches subject.txt generated from input.php page.
xsub=$(cat /data3/sss/subject.txt)
# Show from in email.
xfrom="xxx.net"
# Fetches body.txt generated from input.php page.
xmsg=$(cat /data3/ssss/body.txt)
text="$xmsg <br><br> $links"
echo $text
### Compose emails one at a time, per loop.
"$xmail" "$line" << EOF
subject:$xsub
from:$xfrom
Mime-version:1.0
Content-type:text/html
$text
EOF

Instead of:
...
Content-type:text/html
$text
EOF
Replace it with:
...
Content-type:text/html
$(cat /data3/ssss/body.txt)
$xmsg <br><br> $links
EOF

According to
[https://stackoverflow.com/questions/3902455/mail-multipart-alternative-vs-multipart-mixed]
Set the Content-Type to multipart/alternative when sending HTML email messages.

Related

sendmail add a second attachment to an email

Have a program that issues the following command at the linux level
EXE1= "SH -c '/usr/lib/sendmail ":EMAIL<1,X>:' < "/thisdata/level1/VRE/&HOLD&/':PC.FILE:'.CSV':'"':"'"
Is it possible to attach a second PC.File in this instruction?
The rightest answer to this would require knowledge of OS and any mail server restrictions you might have but I have been working on an old routine of ours that maybe provide some insight.
We used to us uuencode as #RedCabbage suggested but we had problems with some servers rejecting the messages. As we have no control over other people's servers and because uuencode is almost as old as dirt we updated our solution to use MIME instead. See "Not As Easy Way"
Easy way
On our Linux system we use mailx (which in our case linked to /usr/bin/mail) to add multiple attachments.
echo "It's Wednessday, my dudes." |mail -s "foobar" -a foo.txt -a bar.txt dudes#wednessday.com
This results in an email with two attachments and message body if the files are properly pathed (if used with a real address).
Not As Easy Way
Create your own MIME (multipart/mixed) message. We create a record similar to this.
To: foo#foo.bar
From: bar#foo.bar
Subject: Not Rocket Surgery
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="SomelongBoundryMarkerYouMakeUpDoNotUseThis"
This is a multipart message in MIME format.
--SomelongBoundryMarkerYouMakeUpDoNotUseThis
Content-Type: text/plain
The body of that message go here.
--SomelongBoundryMarkerYouMakeUpDoNotUseThis
Content-Type: application/pdf
Content-Transfer-Encoding: Base64
Content-Disposition: attachment; filename="YourFileName.pdf"
Content-Base: http://www.IputOurDomainHereDunnoWhyDoNotUseThis.com
##BASE64 encoded string go here.
--SomelongBoundryMarkerYouMakeUpDoNotUseThis
Rinse and Repeat with more files, newlines are important here.
For the encoding part, assuming that your file is in /foo/bar.pdf
ENCODED = ""
FILE.LOC = "/foo/bar.pdf"
TEST = ENCODE("Base64",1,FILE.LOC,2,ENCODED,1)
IF TEST EQ 0 THEN
;*Put the text in ENCODED where it says ##BASE64 encoded string go here
END
This is much more tedious because you have to figure out the all the mime types and make sure everything is formatted just right.
Good Luck
This is how we have managed multiple attachments. For example, /home/jbloggs/attachment1.doc /home/jbloggs/attachment2.pdf.
You will (as you have done) have to build the commands and execute them with SH -c 'blah' (as you have done already.)
Create a text file with the address, subject and body in (for example) /home/jbloggs/tempemail.txt
To: recipient.email#gmail.com
subject: Your subject line here
This is the main body text
Cycle through the attachments and uuencode them to an incrementing name (ENCODEDn here):
uuencode /home/jbloggs/attachment1.doc attachment1.doc > ENCODED1
uuencode /home/jbloggs/attachment2.pdf attachment2.pdf > ENCODED2
Use cat to join all the files together to a single one:
cat /home/jbloggs/tempemail.txt ENCODED1 ENCODED2 > COMBOFILE
Use sendmail on the combo:
sendmail recipient.email#gmail.com < COMBOFILE
You can loop through as many ENCODEDn files as you like.

If either of the files exist then send email alert in bash shell script

Trying to mock a little shell script.. There is a process that already exists which creates one of the two files (i.e, file1 or file2). My goal is to be able to send email alert depending on which file is created.
For example, if file1 exists then send email to John, David, Smith stating that file1 was created. If file2 exists then send an email to the same group of people (John, David, Smith ) stating that file2 was created.
If I write a simple if-else it will work but I will need to repeat some parts of the code which I am trying to avoid.
#!/bin/bash
file="file1"
if [ -f "$file1" ]
then
send email to three people mentioned
else
send email to three people mentioned saying file2 was created since it always creates two files
fi
}
Here I am trying to construct a script in a better way and also I don't want to repeat "send email..." commands three times because I may need to add more people into the list in the future.
Please help. Thanks in advance.
You can create a function block that send a mail to a desired list of people.Call the function from either of the case
Something like
#!/bin/bash
userlist="$HOME/myusers.cfg" # one user per line
function send_alert {
while read -r user comment; do
echo "my command to send to ${user}"
echo "subject=$1"
echo "Content=Hello ${comment}\n$2"
done < "${userlist}"
}
file="file1"
if [ -f "$file1" ]; then
send_alert "My subject" "Content: ${file1} exists."
else
send_alert "My subject" "Content: ${file2} exists."
fi
I added an optional field comment in the myusers.cfg, so you can add some comment after a space (name person, something).
You can adjust send_alert for more business rules, such as only send once an hour an alert, only send during working hours except for the person on night shift, whatever.

mail format change when received

I have file.out logfile which contain plain text and formated as
1abcdefghijklmnopqrstuvwxyz
2abcdefghijklmnopqrstuvwxyz
3abcdefghijklmnopqrstuvwxyz
<--------------------
4abcdefghijklmnopqrstuvwxyz
5abcdefghijklmnopqrstuvwxyz
6abcdefghijklmnopqrstuvwxyz
My problem is when I received it in my mail box the format is changed
OUTPUT in MAILBOX
1abcdefghijklmnopqrstuvwxyz 2abcdefghijklmnopqrstuvwxyz 3abcdefghijklmnopqrstuvwxyz
<---------------
4abcdefghijklmnopqrstuvwxyz
5abcdefghijklmnopqrstuvwxyz
6abcdefghijklmnopqrstuvwxyz
the first 1,2,3 is not supposed to be in a single line. while the 4,5,6 stays formated as is.
the file was originally sent by perl script using Mail:Sendmail module, but I also tried sending through mail command to test also.
cat file.out | mail -s reportfile email#email.com got the same result.
I've also check the file and there is no extra spaces or characters.
MS Outlook 2007 issue. resolve it with this: go to Tools > Preferences > Email > Email-Options >Message handling > "Remove extra line breaks in plain text messages".

Create serial mail on linux commandline

I would like to generate serial emails on the linux commandline. Assume I have a file indicating mail address, subject and message text in columns on separate lines for each recipient. I.e.
recipient1#mail.com subject1 text1
recipient2#mail.com subject2 text2
...
The script should use standard commands as I intend to send it to colleagues who should create some emails for me.
The loop over the lines could be with xargs ... Can I use the commandline tool mail?
It is important that the mails are not send immediately. Ideally it creates files for import in the users preferred mail client. So that senders can check the mails before they submit.
I also would like to be able to add attachments to the Mails.
I tried e.g.
function mail_kmail {
kmail -s "$2" --body "$3" --attach "$4" --composer "$1"
}
function mail_thunderbird {
thunderbird -compose "to='$1',subject='$2',body='$3',attachment='$4'"
}
and reading the input data from file with
while read recipient subject body attach $file
do
mail_kmail "$recipient" "$subject" "$body" "$attach";
done
but this will work only if my colleagues installed and set up either of these mail clients.
I found this (closed) related question:
How can i send automated email in linux? .
You can use mutt to send the emails, here is an example:
echo "This is the message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" -- recipient#domain.com
Since it's hard to know what you are trying to achieving here, you can even create a configuration file to be used, but you'll to investigate more or give more details about your use case.

How do I send an email using awk to a list of email addresses in a text file?

If I have a text file with a list of email addresses, how can I go through the list and send an email to each of those email addresses with a text file as the message.
I.e. I want to take in an email as a variable so I can execute this command:
mail -s "Welcome" email#address.com < welcome.txt
for example you have a mails_addresses.txt file with one address per line like that:
email1#mail.com
email2#mail.com
email3#mail.com
In case you have another complex structure which you need to parse with for example awk you should to show it us.
So you need just to write a loop which will read it and send it to mail command:
while read MAIL
do
mail -s "Welcome" "$MAIL" < welcome.txt
done < mails_addresses.txt
You can do this even without awk:
cat users-list | while read addr
do
mail -s "Welcome" "$addr" < welcome.txt
done

Resources