Use a Python script to send mail using a third-party SMTP server.
This script can send messages using a configured Third-party SMTP server in the form of a parameter.
Writing scripts using Python 3.5.1, CentOS 7 's own Python 2.7.5 tests are working properly.
When using a script, you need to pass in three parameters: ' Recipient ', ' mail subject ', ' message body '. The recipient can be multiple, formatted as: ' Recipient 1, Recipient 2 '
Message records sent using scripts are written to a file where the path can be defined in the footsteps.
The SMTP account and password also need to be defined in the script.
[Root@z-dig scripts]#./send_mail.py
Usage:./send_mail.py ' touser1,touser2 ' Subject ' body '
[Root@z-dig scripts]#
Script content:
#!/usr/bin/python
#/root/python/python/bin/python3
# admin@z-dig.com
# www.z-dig.com
Import Smtplib
Import OS
Import socket
From sys import ARGV
From email.message Import message
From datetime import datetime
# Define Log Path
Log_path= '/tmp/sendmail.log '
# Define SMTP Server Port account Password
Smtp=dict ()
smtp[' host ']= ' smtp.z-dig.com '
smtp[' Port ']=25
smtp[' account ']= ' service@z-dig.com '
smtp[' password ']= ' password '
If Len (argv)!= 4:
Print ("Usage: {} ' Touser1,touser2 ') ' Subject '" ' Body '. Format (argv[0))
Exit ()
Msg=message ()
msg[' to ']=argv[1]
msg[' from ']=smtp[' account ']
msg[' Subject ']=argv[2]
Msg.set_payload (Argv[3])
Msg.set_charset (' Utf-8 ')
Log=dict ()
log[' time ']=datetime.today ()
log[' to ']=msg[' to ']
log[' subject ']=msg[' subject ']
log[' body ']=argv[3]
Try:
Socket.setdefaulttimeout (10)
Server=smtplib. SMTP (smtp[' host '],smtp[' Port ')]
Server.login (smtp[' account '],smtp[' password ')]
Server.sendmail (smtp[' account '],msg[' to '].split (', '), msg.as_string ())
Server.quit ()
log[' status ']= ' Success '
Except:
log[' status ']= ' Failed '
Print ("Failed")
logs= ("time:{}\tto:{}\tsubject:{}\tbody:{}\tstatus:{}\n". Format log[' time '],log[' to '],log[' subject '],log[' Body '],log[' status '])
F=open (Log_path, "a")
F.write (Logs)
F.close ()
[Root@z-dig scripts]#
Example:
[Root@z-dig scripts]# Cat/tmp/sendmail.log
Cat:/tmp/sendmail.log:no such file or directory
[Root@z-dig scripts]#
[Root@z-dig scripts]#./send_mail.py ' admin@z-dig.com ' send mail through python ' Hello world! '
[Root@z-dig scripts]#
[Root@z-dig scripts]# Cat/tmp/sendmail.log
TIME:2016-05-17 23:33:01.682830 to:admin@z-dig.com subject:send mail through Python Body:hello Status:success
[Root@z-dig scripts]#
Change the smtp[' host ' in the script to the wrong value, simulate a network error, and modify the value of smtp[' password ' to simulate authentication errors:
[Root@z-dig scripts]#./send_mail.py ' admin@z-dig.com ' SMTP server host is wrong ' Hello world! '
Failed
[Root@z-dig scripts]#
[Root@z-dig scripts]# Cat/tmp/sendmail.log
TIME:2016-05-17 23:33:01.682830 to:admin@z-dig.com subject:send mail through Python Body:hello Status:success
TIME:2016-05-17 23:35:30.244009 to:admin@z-dig.com SUBJECT:SMTP server host is wrong Body:hello world! Status:failed
[Root@z-dig scripts]#
[Root@z-dig scripts]#./send_mail.py ' admin@z-dig.com ' password is wrong ' Hello world! '
Failed
[Root@z-dig scripts]# Cat/tmp/sendmail.log
TIME:2016-05-17 23:33:01.682830 to:admin@z-dig.com subject:send mail through Python Body:hello Status:success
TIME:2016-05-17 23:35:30.244009 to:admin@z-dig.com SUBJECT:SMTP server host is wrong Body:hello world! Status:failed
TIME:2016-05-17 23:40:35.975172 to:admin@z-dig.com Subject:password is wrong Body:hello Status:failed
[Root@z-dig scripts]#
Using this script to send an alert message is still OK ... And there's a record.
Later can be written as a module. Easy to invoke.