Mail the contents of a log file in perl - html

i want to mail the contents of a log file of another script . I have tried the code it works , but however the output is not i expected. I want the output in line by line exactly like in LOG.txt, but i get it in as a paragraph in the body.
my $HOME ='/apps/stephen/data';
my $FILE ="$HOME/LOG.txt";
my #HTML =();
sub copyfile
{
`$HOME/APPL.ksh > $FILE`;
push(#HTML,`cat $FILE`);
&sendMail;
}
sub sendMail
{
$sub="TEST";
$from='ABC#ABC.com';
$to='ABC#ABC.com';
open(MAIL, "|/usr/lib/sendmail -t");
print MAIL "From: $from \12"; print MAIL "To: $to \12";print MAIL "Cc: $Cc \12";
print MAIL "Subject: $sub \12";
print MAIL "Content-Type: text/html \12";
print MAIL "Content-Disposition:inline \12";
print MAIL #HTML;
close(MAIL);
}
sub init
{
copyfile;
}
init;

Add missing MIME-Version: header to complete Content-*: headers.
open(MAIL, "|/usr/lib/sendmail -i -t");
print MAIL << "END";
From: $from
To: $to
Cc: $Cc
Subject: $sub
MIME-Version: 1.0
Content-Type: text/html
Content-Disposition: inline
END
print MAIL #HTML;
close(MAIL)
;

Related

How to display Html Tabular Data in email Body using Mailx or Mutt

I have a Scenario where i am having one .csv file called Studentdetails.csv
The student details has below following data
Ram,Mumbai,MBA
Viraj,Delhi,MCA
Vilas,Kolkata,MMS
Priya,Agra,BCA
My below code convert .csv file to html table format file
awk 'BEGIN{
FS=","
print "MIME-Version: 1.0"
print "Content-Type: text/html"
print "Content-Disposition: inline"
print "<HTML>""<TABLE border="1"><TH>Name</TH><TH>City</TH><TH>Course</TH>"
}
{
printf "<TR>"
for(i=1;i<=NF;i++)
printf "<TD>%s</TD>", $i
print "</TR>"
}
END{
print "</TABLE></BODY></HTML>"
}
' file-to-convert.csv > StudentDetails.html
But my issue is How do i Display this HTML Tabular format in Body of mail
using mutt or mailx command
Note: Not as attachment
Output should be display in mail like below html tabular format

PHPMailer allows mime boundary to be visible

I am using PHPMailer to send email via smtp-relay.gmail.com - see previous post After making an account through G-Suite, my credentials are being accept, but when the email is delivered, I can see the plain text version, as well as, the html version, with some other characters along the way:
------example.com----250cd4bbb8be52d828379181e485c269 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Order Received
...
------example.com----250cd4bbb8be52d828379181e485c269 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
Followed by the html version, that ends with the following:
------example.com----250cd4bbb8be52d828379181e485c269--
When I was using the vanilla version, it worked just fine without seeing the plain text or the mime boundary data:
$send = mail($recpEmails, $subject, $htmlMessage, $headers);
The variable $htmlMessage still holds the same information as before, but now PHPMailer is sending it via the following line:
$mail->Body = $htmlMessage;
I would not see the plain text or these other lines with the dashes. Why would sending via PHPMailer > smtp-relay.gmail.com change the results?
Is it because I added the following line?
$mail->IsHTML(true);
Is it due to the following line and, if so, then what should I set it to?
$mail->SMTPDebug = 3;
Are there configurations in the G-Suite> email> advanced settings that I need to change?
Here is the updated code from the previous post:
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->SMTPDebug = 3;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp-relay.gmail.com";
$mail->Port = "587";
$mail->Username = "info#example.com";
$mail->Password = "somePassword";
$mail->setFrom("info#example.com");
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $htmlMessage;
$mail->addAddress($recpEmails);
$mail->Send();
Thanks in advance
Turns out that the answer is in the following stackoverflow post
I used to build the mime boundary between the text only and html version via PHP's mail in the following way:
Create the text message - this should be exactly the same as your html to avoid being marked as spam:
$plain_text = ""; // same words that will appear in the html
Start creating the mime boundary:
# -=-=-=- MIME BOUNDARY
$mime_boundary = "----example.com----".md5(time());
# -=-=-=- MAIL HEADERS
Create the subject:
$subject = "example.com\n";
Create the headers, which will include the mime boundary that you began earlier. As well as, adding the plain text message in the header - in between the first mime boundary:
$headers = "From: example.com <cs#example.com>\n";
$headers .= "Reply-To: example.com <cs#example.com>\n";
$headers .= "BCC: example.com <info#example.com>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";
$htmlMessage = "--$mime_boundary\n";
$htmlMessage .= "Content-Type: text/plain; charset=us-ascii\n";
$htmlMessage .= "Content-Transfer-Encoding: 7bit\n";
$htmlMessage .= "$plain_text\n";
$htmlMessage .= "--$mime_boundary\n";
$htmlMessage .= "Content-Type: text/html; charset=UTF-8\n";
$htmlMessage .= "Content-Transfer-Encoding: 8bit\n\n";
$htmlMessage .= "<html>\n";
$htmlMessage .= "<body>\n";
$htmlMessage .= "" // has to be the same words as the plain text to avoid being marked as spam
$htmlMessage .= "</body>\n";
$htmlMessage .= "</html>\n";
Complete the mime boundary:
# -=-=-=- FINAL BOUNDARY
$htmlMessage .= "--$mime_boundary--\n\n";
# -=-=-=- SEND MAIL
However, with PHPMailer all of this code is no longer necessary by using the following two lines of code:
$mail->AltBody = $plain_text;
$mail->Body = $htmlMessage;
The following is what I have tested and use:
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->SMTPDebug = 3;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp-relay.gmail.com";
$mail->Port = "587";
$mail->Username = "cs#example.com";
$mail->Password = "somePassword";
$mail->setFrom("cs#example.com");
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->AltBody = $plain_text;
$mail->Body = $htmlMessage;
$mail->addAddress($recpEmails);
$mail->Send();
Almost forgot, but this is how I added BCC:
$mail->addBCC("info#example.com");

Send email from Linux server as html - Content type and html tags also visible in email

I am trying to send an email as html.
#!/bin/sh
#MAIL_LIST="gangadhar.n#xx.com"
MAIL_SENDER=foo
fnSendEmail(){
echo ${BODY}| mail -r $MAIL_SENDER -s "$(echo "$MAIL_SUBJECT\nContent-Type: text/html")" "$MAIL_LIST"
}
MAIL_SUBJECT="Test email"
BODY="<html><body><div><h2>Hi All</h2><hr></div></body></html>";
fnSendEmail $BODY $MAIL_SENDER $MAIL_SUBJECT $MAIL_LIST
I am receiving email but html tags and Content type also visible in mails as below.
Subject as
"Test email\nContent-Type: text/html"
Email body as:
<html><body><div><h2>Hi All</h2><hr></div></body></html> NOTICE TO RECIPIENT: If you are not the intended recipient of this e-mail, you are prohibited from sharing, copying, or otherwise using or disclosing its contents. If you have received this e-mail in error, please notify the sender immediately by reply e-mail and permanently delete this e-mail and any attachments without reading, forwarding or saving them. Thank you.
Thank you in advance
I have done it using sendmail
#MAIL_LIST1="Gangadhar.N#xx.com"
MAIL_SENDER=dap
fnSendEmail(){
(
echo To: $MAIL_LIST
echo Cc: $MAIL_LIST
echo From: dap53
echo "Content-Type: text/html; "
echo Subject: $MAIL_SUBJECT
echo
echo $BODY
) | /usr/sbin/sendmail -t
}
MAIL_SUBJECT="Test email"
BODY="<html><body>Sample</body></html>"
fnSendEmail $BODY $MAIL_SENDER $MAIL_SUBJECT $MAIL_LIST
Use option -a to append content-type header, -s is for subject, change your fnSendEmail to following it should work
fnSendEmail(){
echo ${BODY}| mail -r $MAIL_SENDER -a "Content-type: text/html" -s "$(echo "$MAIL_SUBJECT\n")" "$MAIL_LIST"
}

need to send html mail with attachment in unix

I am facing problems in sending html mail with attachemnt.I will able to send mail with
attachment (plain text ) using mailx -s and uuencode command and also html mail
without attachment using sendmail option.
However I am not able to send html mail along with attachment.
Either one of it is working (html mail or attachment)
Below are the different ways I have tried. Could you please help me in resolving the same.
1) Failed because of illegal option base64
#!/usr/bin/ksh
export MAILTO="abc#abc.com"
export SUBJECT="Mail Subject"
export BODY="card_summary_mail.html"
export ATTACH="query5_result.csv"
(
echo "To: $MAILTO"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
echo
echo '---q1w2e3r4t5'
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat $BODY
echo '---q1w2e3r4t5'
echo 'Content-Type: application; name="'$(basename $ATTACH)'"'
echo "Content-Transfer-Encoding: base64"
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
uuencode --base64 $ATTACH $(basename $ATTACH)
echo '---q1w2e3r4t5--'
) | /usr/lib/sendmail $MAILTO
2)
cib-sokay2{u384283}323:cat test_html2.sh
{
uuencode query5_result.csv query5_result.csv > attachment.txt
cat mail.html attachment.txt > attachment2.html
} | /usr/lib/sendmail -t abc#abc.com
-----------------------------------------------
3)
cib-sokay2{u384283}324:cat test_html3.sh
export MAILTO="abc#abc.com"
export CONTENT="mail.html"
export CONTENT_F="attachment.txt"
export SUBJECT="TEST EMAIL: TESTING HTML"
BOUNDARY='=== This is the boundary between parts of the message. ==='
{
print - "From: Someone <$MAILFROM>"
print - "To: Someone <${MAILTO}>"
print - 'Subject:' $SUBJECT
print - 'MIME-Version: 1.0'
print - 'Content-Type: MULTIPART/MIXED; '
print - ' BOUNDARY='\"$BOUNDARY\"
print -
print - ' This message is in MIME format. But if you can see this,'
print - " you aren't using a MIME aware mail program. You shouldn't "
print - ' have too many problems because this message is entirely in'
print - ' ASCII and is designed to be somewhat readable with old '
print - ' mail software.'
print -
print - "--${BOUNDARY}"
print - 'Content-Type: TEXT/PLAIN; charset=US-ASCII'
print -
cat $CONTENT
print -
print -
print - "--${BOUNDARY}"
print - 'Content-Type: TEXT/PLAIN; charset=US-ASCII; name='${CONTENT}
print - 'Content-Disposition: attachment; filename='${CONTENT_F}
print -
cat ${CONTENT}
print -
print - "--${BOUNDARY}--"
} | /usr/lib/sendmail ${MAILTO}
------------------------------------------------------------
cib-sokay2{u384283}326:cat test_html4.sh
#!/usr/bin/ksh
export MAILTO="abc#abc.com"
export CONTENT="mail.html"
export SUBJECT="subject of email"
(
echo "Subject: $SUBJECT"
# This appears in the mail body
cat $CONTENT
# The next line creates the attachment with a suitable extension to read
# with Windows notepad
unix2dos "attachment.txt" | uuencode myattach.txt
echo "."
) | /usr/lib/sendmail $MAILTO
-------------------------------------
Your first attempt is fairly close. Your second attempt seems to write something to a file which is then abandoned, and you feed empty input to sendmail. Your third attempt has a grave error in the MIME boundary and some peculiarities (why do you put the same content twice?), but is basically fairly similar to the first. Your fourth attempt looks fairly reasonable but old-fashioned, but could have failed because of false assumptions about how things need to be formatted (for example, if $CONTENT does not start with an empty line, your contents would go smack dab in the message headers, creating illegal headers and probably being rejected).
Assuming you have a command base64 on your system (it is included in GNU Coreutils since 6.0, almost ten years back), just replace the uuencode call with that, and fix the various minor formatting errors. Here is a refactored script which hopefully catches most of them.
#!/bin/sh
# No need to export anything, we are not passing these to child processes
MAILTO="abc#abc.com"
SUBJECT="Mail Subject"
BODY="card_summary_mail.html"
ATTACH="query5_result.csv"
( cat <<____HERE
To: $MAILTO
Subject: $SUBJECT
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"
... You could put a MIME preamble here but nobody ever reads it ...
---q1w2e3r4t5
Content-Type: text/html
Content-Disposition: inline
X-Notice: you need an empty line before the body here:
____HERE
cat "$BODY"
# also notice empty line at beginning of next snippet
cat <<____THERE
---q1w2e3r4t5
X-Notice: just "application" is incompletely specified
Content-Type: application/octet-stream; name="$(basename "$ATTACH")"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$(basename "$ATTACH")"
X-Notice: another empty line
____THERE
base64 "$ATTACH"
echo '---q1w2e3r4t5--'
) | /usr/lib/sendmail "$MAILTO"
However, assembling your own MIME structure gets tired really fast -- I would recommend looking for a command-line MUA with proper attachment support (mutt is popular; some modern mail/mailx clones have similar facilities, but it's not standard).
Also, there is nothing ksh-specific here, so I changed the shebang.
Your first attempt is almost correct. Just replace --base64 by -m as shown below:
#!/usr/bin/ksh
export MAILTO="abc#abc.com"
export SUBJECT="Mail Subject"
export BODY="card_summary_mail.html"
export ATTACH="query5_result.csv"
(
echo "To: $MAILTO"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
echo
echo '---q1w2e3r4t5'
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat "$BODY"
echo '---q1w2e3r4t5'
echo 'Content-Type: application; name="'$(basename "$ATTACH")'"'
echo "Content-Transfer-Encoding: base64"
echo 'Content-Disposition: attachment; filename="'$(basename "$ATTACH")'"'
uuencode -m "$ATTACH" "$(basename "$ATTACH")"
echo '---q1w2e3r4t5--'
) | /usr/lib/sendmail $MAILTO

Send email HTML formatted with CGI - Perl

I'm trying to send an email with HTML content, but it doesn't send formatted.
This is my code,
print OUT "To: $email\n";
print OUT "From: $pedidos\n";
print OUT "CC: $pedidos\n";
print OUT "BCC: $tecnico\n";
print OUT "Subject: $subject\n";
print OUT "Content-type: text/html; charset=iso-8859-1\n\n";
print OUT "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'>";
print OUT "<html><head><title>Title</title></head>";
(...more html...)
Any idea?
Thanks!
Well, for one thing, you left out
print OUT "MIME-Version: 1.0\n";
without which your Content-type header doesn't mean anything. I'm assuming that content going to OUT is correctly being emailed.