html email sent by shell

Keywords: MySQL yum socket SSL

Article directory

1. Open the SMTP service of QQ mailbox server

I have tried to use mail to send html messages many times, but the text is still displayed, failed. Finally, the sendEmail client is used to send mail with forms. First, configure the qq mailbox to start the SMTP service
The flow chart is as follows: Settings > account

Start SMTP Service - birth authorization code

2. Install sendEmail

#Download the installation package
wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
#Create directory
mkdir -p /usr/local/bin
#decompression
tar zxf sendEmail-v1.56.tar.gz -C /usr/src/
#Enter the unzip directory
cd /usr/src/sendEmail-v1.56/
#Copy program to specified directory
cp -a sendEmail /usr/local/bin/
#Give execution permission
chmod +x /usr/local/bin/sendEmail
#Installation components
yum install perl-Net-SSLeay perl-IO-Socket-SSL -y

3. Send email with attachments

3.1 details of sendemail command

# -f from@163.com                    # Sender email address
# -t to@qq.com                     # Recipient email address
# -s smtp.163.com                   # smtp server address of sender's mailbox
# -u 'test'                      # Mail title
# -o message-content-type=html             # The message content format is html
# -o message-charset=utf8               # The message content code is utf8
# -xu from@163.com                  # Sender mailbox login user name
# -xp 'passwd'                    # Sender email login password
# -m 'test'                      # Mail content

3.2 write the test.sh script as follows

#!/bin/bash

#Inbox
EMAIL_RECIVER="571XXX512@qq.com"
#Sender email
EMAIL_SENDER=5713XXXX12@qq.com
#Mailbox user name
EMAIL_USERNAME=571325512
#Mailbox password
#Note: to use qq email to send, you need to start the POP3/SMTP service first, and then the password to send email needs to use the third-party client login code provided by Tencent when opening the POP3/SMTP service.
EMAIL_PASSWORD=laitXXXowbcei

#Accessory path
FILE1_PATH="/root/wsktest/2.png"

#smtp server address
EMAIL_SMTPHOST=smtp.qq.com

EMAIL_TITLE="test"
EMAIL_CONTENT="Hello!"

sendEmail -f ${EMAIL_SENDER} -t ${EMAIL_RECIVER} -s ${EMAIL_SMTPHOST} -u ${EMAIL_TITLE} -xu ${EMAIL_USERNAME} -xp ${EMAIL_PASSWORD} -m ${EMAIL_CONTENT} -a ${FILE1_PATH} -o message-charset=utf-8

3.3 executing script sh test.sh

[root@hadoop001 wsktest]# sh test.sh 
Apr 13 16:17:08 hadoop001 sendEmail[13678]: Email was sent successfully!

4. Send email with html form

4.1 table data

The first column is the name of the table, the second column is the total amount of data in the original mysql table, and the third column is the total amount of data in HBase. The purpose is to send the data in table ﹣ counts.txt by the display mail of the table. If the data in the table is inconsistent, the current row is marked in red.

[root@hadoop001 wsktest]# cat table_counts.txt 
table_name_1,10000,10000
table_name_2,12000,12000
table_name_3,90000,90002
table_name_4,550,550
table_name_5,8000,7999
table_name_6,30000,30000

4.2 the script is as follows

#!/bin/bash
#Inbox
EMAIL_RECIVER="57XXXX12@qq.com"
#Sender email
EMAIL_SENDER=5713XXX2@qq.com
#Mailbox user name
EMAIL_USERNAME=5713XXX2
#Mailbox password
#Note: to use qq email to send, you need to start the POP3/SMTP service first, and then the password to send email needs to use the third-party client login code provided by Tencent when opening the POP3/SMTP service.
EMAIL_PASSWORD=laitzphkinowbcei

FILE1_PATH="/root/wsktest/2.png"

#smtp server address
EMAIL_SMTPHOST=smtp.qq.com

EMAIL_TITLE="test"

html_input(){
echo "<tr>
<td>$1</td>
<td>$2</td>
<td>$3</td>
</tr>" >>/root/wsktest/mail.html
}

html_input_red(){
echo "<tr bgcolor="#FF0000">
<td>$1</td>
<td>$2</td>
<td>$3</td>
</tr>" >>/root/wsktest/mail.html
}


set_info(){
i=1
echo "
<table border=1 border=1 cellspacing='0' cellpadding='0' >
<tr>
<th>table_name</th>
<th>source_count</th>
<th>target_count</th>
</tr>" > /root/wsktest/mail.html
    table_counts=$(awk -F "," '{print $1}' /root/wsktest/table_counts.txt) #Table count set
    for tablename in $table_counts
    do
        j=2

        html_sour_count=$(awk -F "," 'NR==i { print $j}' i=$i j=$j /root/wsktest/table_counts.txt) #The corresponding count in mysql
        let "j++"
        html_tar_count=$(awk -F "," 'NR==i { print $j}' i=$i j=$j /root/wsktest/table_counts.txt) #The corresponding count in the data warehouse

        if [ "$html_sour_count" ==  "$html_tar_count" ];then
            html_input $tablename $html_sour_count $html_tar_count  #Construct table information per row
        else
			html_input_red $tablename $html_sour_count $html_tar_count #Construct table information per row
			
			
        fi

        let "i++"
        echo $tablename $html_sour_count $html_tar_count $i $j
    done
echo "</table>" >> /root/wsktest/mail.html
}     # Make mail.html and send by mail

set_info

EMAIL_EXCEL=$(cat /root/wsktest/mail.html)


sendEmail -f ${EMAIL_SENDER} -t ${EMAIL_RECIVER} -s ${EMAIL_SMTPHOST} -u ${EMAIL_TITLE} -xu ${EMAIL_USERNAME} -xp ${EMAIL_PASSWORD} -m ${EMAIL_EXCEL} ${EMAIL_CONTENT} -a ${FILE1_PATH} -o message-charset=utf-8 -o message-content-type=html 
4.3 execution script
[root@hadoop001 wsktest]# sh test2.sh 
table_name_1 10000 10000 2 3
table_name_2 12000 12000 3 3
table_name_3 90000 90002 4 3
table_name_4 550 550 5 3
table_name_5 8000 7999 6 3
table_name_6 30000 30000 7 3
Apr 13 18:31:31 hadoop001 sendEmail[14481]: Email was sent successfully!

4.4 the screenshot of the email is as follows

Posted by johnnyblaze1980 on Thu, 28 Nov 2019 11:36:15 -0800