Exemplo n.º 1
0
 def send(
     self,
     context,
     template,
     title,
     dests,
     sender_name,
     sender_email,
     reply_to=None,
     attachments=None,
 ):
     connection = None
     if self.debug is False:
         try:
             connection = get_connection(
                 host=self.email_host,
                 port=self.email_port,
                 sender_email=self.email_host_user,
                 sender_password=self.email_host_password,
             )
         except SMTPException as e:
             logger.info('Failed to establish connection: {}'.format(e))
             return False
     email = prepare_email(
         context=context,
         css='',
         html=template,
         title=title,
         sender='{sender_name} <{sender_email}>'.format(
             sender_name=sender_name, sender_email=sender_email
         ),
         recipients=dests,
         reply_to=reply_to,
         smtp_connection=connection,
         attachments=attachments,
         debug=self.debug,
     )
     try:
         return email.send()
     except Exception as e:
         logger.info('Failed to send email: {}'.format(e))
         return False
Exemplo n.º 2
0
 def test_get_connection(self):
     patch('concrete_mailer.utils.SMTP', ).start()
     self.assertIsNotNone(
         get_connection(host=None, port=None, user=None, password=None))
Exemplo n.º 3
0
def prepare_email(
    context,
    css,
    html,
    title,
    sender,
    recipients,
    reply_to=None,
    equivalences_key_path=None,
    smtp_connection=None,
    attachments=None,
    debug=False,
    email_host=None,
    email_port=None,
    email_host_user=None,
    email_host_password=None,
):
    if equivalences_key_path is None:
        equivalences_key_path = {}
    if reply_to is None:
        reply_to = sender

    EmailBackendClass = EmailToConsole if debug else EmailToSend

    if smtp_connection is None and debug is False:
        try:
            smtp_connection = get_connection(
                host=email_host,
                port=email_port,
                sender_email=email_host_user,
                sender_password=email_host_password,
            )
        except SMTPException as e:
            logger.info('Failed to establish connection: {}'.format(e))
            raise

    message = prepare_email_body(context=context,
                                 css=css,
                                 template=html,
                                 title=title)

    # Prepare mails
    email = MIMEMultipart()
    email['From'] = sender
    email['To'] = ", ".join(recipients)
    email['Subject'] = title
    email['reply-to'] = reply_to

    # detect image in html and attach in header of email
    message = _process_html_to_embed_image_in_email_header(
        email=email,
        message_html=message,
        equivalences_key_path=equivalences_key_path,
    )

    email.attach(MIMEText(message, 'html'))

    if attachments is None:
        return EmailBackendClass(connection=smtp_connection,
                                 email=email,
                                 message=message)
    if isinstance(attachments, (list, tuple)) is False:
        raise ValueError("Attachements should be a list or a tuple")
    for attachment in attachments:
        if os.path.isfile(attachment) is False:
            logger.info("No such file {}. Skipping ...".format(attachment))
            continue
        file = MIMEBase('application', "octet-stream")
        with open(attachment, 'rb') as fd:
            file.set_payload(fd.read())
        encoders.encode_base64(file)
        filename = os.path.basename(attachment)
        file.add_header('Content-Disposition',
                        'attachment; filename="{}"'.format(filename))

        email.attach(file)

    return EmailBackendClass(
        connection=smtp_connection,
        email=email,
        message=message,
        attachments=attachments,
    )