Last active
August 29, 2015 14:19
-
-
Save cascad-s/4a5ba36fd84f31c89c91 to your computer and use it in GitHub Desktop.
Send the mass mails!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding:cp1251 | |
import logging | |
import smtplib | |
import os | |
import sys | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.base import MIMEBase | |
from email.mime.text import MIMEText | |
from email.utils import COMMASPACE, formatdate | |
from email import encoders | |
class Mail_Controller(): | |
def __init__(self, | |
login='my_mail', | |
pwd='password', | |
send_from='field_from', | |
send_to=[], | |
send_cc=[], | |
subject='', | |
text='', | |
files=['log.txt'], | |
server='smtp.yandex.ru', | |
port=465, | |
logfile=''): | |
self.login=login | |
self.pwd=pwd | |
self.send_from=send_from | |
self.send_to=send_to | |
self.send_cc=send_cc | |
self.subject=subject | |
self.text=text | |
self.files=files | |
self.server=server | |
self.port=port | |
self.feedback=[] | |
if not logfile: | |
self.logfile=os.getcwd()+'\\log.txt' | |
logging.basicConfig(format = u'%(filename)s[LINE:%(lineno)d]# %(levelname)-8s [%(asctime)s] %(message)s', level = logging.DEBUG, filename = self.logfile) | |
def open_log(path): | |
try: | |
logfile=open(path,'a') | |
except IOError: | |
logfile=open(path,'w') | |
finally: | |
return logfile | |
def send_each(self): | |
for i in self.send_to: | |
self.msg = MIMEMultipart() | |
self.msg['From'] = self.send_from | |
self.msg['To'] = i #COMMASPACE.join(self.send_to) | |
self.msg['cc'] = COMMASPACE.join(self.send_cc) | |
self.msg['Date'] = formatdate(localtime=True) | |
self.msg['Subject'] = self.subject | |
self.msg.attach(MIMEText(self.text)) | |
if self.files: | |
for f in self.files: | |
part = MIMEBase('application', "octet-stream") | |
part.set_payload(open(f,"rb").read() ) | |
encoders.encode_base64(part) | |
part.add_header('Content-Disposition', 'attachment; filename="{}"'.format(os.path.basename(f))) | |
self.msg.attach(part) | |
try: | |
pack=self.send_cc[:] | |
pack.append(i) | |
smtp = smtplib.SMTP_SSL(self.server, self.port) | |
smtp.login(self.login, self.pwd) | |
smtp.sendmail(self.send_from, pack, self.msg.as_string()) | |
smtp.close() | |
logging.info('2XX ok'+' '+i) | |
print(i+' OK') | |
except smtplib.SMTPException: | |
logging.error('4XX fail'+' '+i+str(sys.exc_info()[1])) | |
def set_text(self, text): | |
self.text=text | |
def set_subject(self, subject): | |
self.subject=subject | |
def send_html(self): | |
for i in self.send_to: | |
self.msg = MIMEText(self.text, _charset='utf-8', _subtype='html') | |
self.msg['From'] = self.send_from | |
self.msg['To'] = i #COMMASPACE.join(self.send_to) | |
self.msg['cc'] = COMMASPACE.join(self.send_cc) | |
self.msg['Date'] = formatdate(localtime=True) | |
self.msg['Subject'] = self.subject | |
if self.files: | |
for f in self.files: | |
part = MIMEBase('application', "octet-stream") | |
part.set_payload(open(f,"rb").read() ) | |
encoders.encode_base64(part) | |
part.add_header('Content-Disposition', 'attachment; filename="{}"'.format(os.path.basename(f))) | |
self.msg.attach(part) | |
try: | |
pack=self.send_cc[:] | |
pack.append(i) | |
smtp = smtplib.SMTP_SSL(self.server, self.port) | |
smtp.login(self.login, self.pwd) | |
smtp.sendmail(self.send_from, pack, self.msg.as_string()) | |
smtp.close() | |
logging.info('2XX ok'+' '+i) | |
print(i+' OK') | |
except smtplib.SMTPException: | |
logging.error('4XX fail'+' '+i+str(sys.exc_info()[1])) | |
html=open('somehtml').read() | |
f=open('somefile_with_addresses','r').readlines() | |
f=[i.strip() for i in f if i.strip()] | |
#f = ['wife@life.net', 'wife2@life.net'] | |
q=Mail_Controller(send_to=f, subject='Hey', | |
text=''' | |
'Blah-blah-blah' | |
''', files=[]) | |
q.send_each() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment