<commit_before># -*- coding: utf-8 -*-
from django.core.mail import EmailMultiAlternatives
from email.errors import MessageError
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from genericpath import isfile
from os import listdir
from os.path import join
from time import sleep
from django.core.management.base import NoArgsCommand, BaseCommand
from email.header import Header
from django.template.loader import render_to_string
import sys
from bedrock.mocotw.utils import read_newsletter_context, newsletter_context_vars
import premailer
from bedrock.settings import NEWSLETTER_PRESEND_LIST
import commonware.log
from BeautifulSoup import BeautifulSoup

log = commonware.log.getLogger('newsletter')


class Unbuffered:

    def __init__(self, stream):
        self.stream = stream

    def write(self, data):
        self.stream.write(data)
        self.stream.flush()

    def __getattr__(self, attr):
        return getattr(self.stream, attr)

class Command(BaseCommand):
    help = 'Send Mail'
    option_list = NoArgsCommand.option_list

    def handle(self, *args, **options):
        self.options = options
        testing = True if 1 < len(args) else False
        sys.stdout = Unbuffered(sys.stdout)
        template = args[0]
        from_email = '"Mozilla Taiwan" <<EMAIL>>'
        with open('%s.txt' % template) as file:
            text_content = file.read().decode('utf-8')
            file.close()
        with open('%s.html' % template) as file:
            html_content = file.read().decode('utf-8')
            attachment = MIMEText(html_content, _subtype='text/html', _charset='utf-8')
            attachment.add_header('Content-Disposition', 'attachment', filename='%s.html' % template)
            file.close()
        soup = BeautifulSoup(html_content)
        subject = Header(soup.title.string.encode('utf8'), 'utf-8')
        mail_content = premailer.transform(html_content)
        # headers = {'Reply-To': '<EMAIL>'}
        headers = {}
        # charset = 'utf-8'
        if not testing:
            with open('subscriptions.txt') as file:
                subscriptions = file.readlines()
                for subscription in subscriptions:
                    self.send_mail(subject, headers, from_email, (subscription.rstrip(), ),
                                   text_content, mail_content, (attachment, ))
                    sleep(10)
        elif '@' in args[1]:
            self.send_mail(subject, headers, from_email, (args[1], ), text_content, mail_content, (attachment, ))
        else:
            with open(args[1]) as file:
                subscriptions = file.readlines()
                for subscription in subscriptions:
                    self.send_mail(subject, headers, from_email, (subscription.rstrip(), ),
                                   text_content, mail_content, (attachment, ))
                    sleep(10)

    def send_mail(self, subject, headers, from_email, to_mail, text_content, mail_content, attachments):
        mail = EmailMultiAlternatives(subject=subject, body=text_content, headers=headers,
                                      from_email=from_email, to=to_mail)
        mail.attach_alternative(mail_content, 'text/html')
        for attachment in attachments:
            mail.attach(attachment)
        try:
            mail.send()
            print('Sent mail to %s.' % to_mail)
        except MessageError as e:
            print('Failed to send to %s.' % to_mail, e)
        except RuntimeError as e:
            print('Unexpected error when sending to %s.' % to_mail, e)

    @staticmethod
    def named(email, name):
        if name:<commit_msg>Added option when sending mail.<commit_after># -*- coding: utf-8 -*-
from optparse import make_option
from django.core.mail import EmailMultiAlternatives
from email.errors import MessageError
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from genericpath import isfile
from os import listdir
from os.path import join
from time import sleep
from django.core.management.base import NoArgsCommand, BaseCommand
from email.header import Header
from django.template.loader import render_to_string
import sys
from bedrock.mocotw.utils import read_newsletter_context, newsletter_context_vars
import premailer
from bedrock.settings import NEWSLETTER_PRESEND_LIST
import commonware.log
from BeautifulSoup import BeautifulSoup

log = commonware.log.getLogger('newsletter')


class Unbuffered:

    def __init__(self, stream):
        self.stream = stream

    def write(self, data):
        self.stream.write(data)
        self.stream.flush()

    def __getattr__(self, attr):
        return getattr(self.stream, attr)

class Command(BaseCommand):
    help = 'Send Mail'
    option_list = BaseCommand.option_list + (
        make_option('--attach-markup-file', action='store_true', dest='attach-markup-file', default=False,
                    help='Attach the HTML Markup as a downloadable file.'),
    )

    def handle(self, *args, **options):
        self.options = options
        attachments = []
        testing = True if 1 < len(args) else False
        sys.stdout = Unbuffered(sys.stdout)
        template = args[0]
        from_email = '"Mozilla Taiwan" <<EMAIL>>'
        with open('%s.txt' % template) as file:
            text_content = file.read().decode('utf-8')
            file.close()
        with open('%s.html' % template) as file:
            html_content = file.read().decode('utf-8')
            if options['attach-markup-file']:
                attachments[0] = MIMEText(html_content, _subtype='text/html', _charset='utf-8')
                attachments[0].add_header('Content-Disposition', 'attachment', filename='%s.html' % template)
            file.close()
        soup = BeautifulSoup(html_content)
        subject = Header(soup.title.string.encode('utf8'), 'utf-8')
        mail_content = premailer.transform(html_content)
        # headers = {'Reply-To': '<EMAIL>'}
        headers = {}
        # charset = 'utf-8'
        if not testing:
            with open('subscriptions.txt') as file:
                subscriptions = file.readlines()
                for subscription in subscriptions:
                    self.send_mail(subject, headers, from_email, (subscription.rstrip(), ),
                                   text_content, mail_content, attachments)
                    sleep(10)
        elif '@' in args[1]:
            self.send_mail(subject, headers, from_email, (args[1], ), text_content, mail_content, attachments)
        else:
            with open(args[1]) as file:
                subscriptions = file.readlines()
                for subscription in subscriptions:
                    self.send_mail(subject, headers, from_email, (subscription.rstrip(), ),
                                   text_content, mail_content, attachments)
                    sleep(10)

    def send_mail(self, subject, headers, from_email, to_mail, text_content, mail_content, attachments):
        mail = EmailMultiAlternatives(subject=subject, body=text_content, headers=headers,
                                      from_email=from_email, to=to_mail)
        mail.attach_alternative(mail_content, 'text/html')
        for attachment in attachments:
            mail.attach(attachment)
        try:
            mail.send()
            print('Sent mail to %s.' % to_mail)
        except MessageError as e:
            print('Failed to send to %s.' % to_mail, e)
        except RuntimeError as e:
            print('Unexpected error when sending to %s.' % to_mail, e)

    @staticmethod
    def named(email, name):
        if name:
{'fraction_non_alphanumeric': 0.10178150379879486, 'fraction_numerical': 0.004453759496987163, 'mean_word_length': 2.4673024523160763, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5555562', 'n_tokens_mistral': 2129, 'n_tokens_neox': 2066, 'n_words': 542}