send e-mail with python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import smtplib,sys,os
if sys.version_info<(3,9):
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import formataddr
from email.utils import formatdate
from email.utils import COMMASPACE
elif sys.version_info<(2,9):
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
def sendmail(server,sender,recipients,subject,bodymessage,attach_files=[],smtp_debug=0):
"""
sendmail function for sendmail :)
parameters
server:string
sender:string
recipients:list
subject:string,
bodymessage:string
attach_files: list or tuple of filenames
smtp_debug:0/1 - to show debug info
"""
msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = COMMASPACE.join(recipients)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach(MIMEText(bodymessage))
for f in attach_files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(f,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
msg.attach(part)
try:
smtpObj = smtplib.SMTP(server)
smtpObj.set_debuglevel(smtp_debug)
smtpObj.sendmail(sender, recipients, msg.as_string())
smtpObj.close()
except SMTPException:
return 1
return 0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import smtplib,sys,os
if sys.version_info<(3,9):
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import formataddr
from email.utils import formatdate
from email.utils import COMMASPACE
elif sys.version_info<(2,9):
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
def sendmail(server,sender,recipients,subject,bodymessage,attach_files=[],smtp_debug=0):
"""
sendmail function for sendmail :)
parameters
server:string
sender:string
recipients:list
subject:string,
bodymessage:string
attach_files: list or tuple of filenames
smtp_debug:0/1 - to show debug info
"""
msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = COMMASPACE.join(recipients)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach(MIMEText(bodymessage))
for f in attach_files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(f,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
msg.attach(part)
try:
smtpObj = smtplib.SMTP(server)
smtpObj.set_debuglevel(smtp_debug)
smtpObj.sendmail(sender, recipients, msg.as_string())
smtpObj.close()
except SMTPException:
return 1
return 0
if __name__ == '__main__':
if 0==sendmail('10.10.10.10','from@dom.foo',['to1@dom.foo','to2@dom.foo'],'ololo subject','Hello WORLD!\n',['/tmp/myfile1','/tmp/myfile2']):.
print "All ok!"
else:
Комментариев нет:
Отправить комментарий