I need to get a mail with HTML Structured and attachment with it but the below code is resulting only mail with HTML Script
#!/bin/bash
set -x # Enable debugging output
set +x # Disable debugging output
AWK=/bin/awk
CAT=/bin/cat
RM=/bin/rm
MAIL=/bin/mail
MKTEMP=/bin/mktemp
# Function to convert CSV to HTML table
csv_to_html_table() {
${AWK} -F',' 'BEGIN{print "<table style="font-size: 13" border="1" cellspacing="0" cellpadding="2">" }
{
if (NR == 1) {
print "<tr>";
for(i=1; i<=NF; i++)
print "<td><b>" toupper($i) "</b></td>";
print "</tr>";
} else {
print "<tr>";
for(i=1; i<=NF; i++)
print "<td>" $i "</td>";
print "</tr>";
}
}
END{print "</table>"}' "$1"
}
mail_sub=$1
csv_file=$2
# CSV file path
ERROR_FILE=abc.csv
html_table=$(csv_to_html_table "$csv_file")
# Define email details
EMAIL_TO="[email protected]"
EMAIL_SUBJECT=$mail_sub`your text`
PROXY_EMAILID="[email protected]"
MSG_FILE=$($MKTEMP)
Create the email body with correct MIME headers for HTML content
{
echo "From: $PROXY_EMAILID"
echo "To: $EMAIL_TO"
echo "Subject: $EMAIL_SUBJECT"
echo "Mime-Version: 1.0"
echo "Content-Type: text/html; charset="UTF-8""
echo
echo "<html><body><p>Hi Team,<br><p>Please find the $mail_sub and its Count<br>${html_table}<br>(Note: This is an automated message and does not require a response.)<br><p>Regards,<p>Team</body></html>"
} > "$MSG_FILE"
# Debug: Print email message file content
echo "Email Message File Content:"
$CAT "$MSG_FILE"
# Send email using mail command with attachment
$MAIL -a "$ERROR_FILE" -s "$EMAIL_SUBJECT" -r "$PROXY_EMAILID" "$EMAIL_TO" < "$MSG_FILE"
`your text`
# Cleanup temporary file
$RM "$MSG_FILE"
New contributor
Thiruvarasan M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.