Esempio n. 1
0
    def test_new(self):
        config = dict(
            manager=dict(use="immediate"),
            transport=dict(use="mock"),
            message=dict(author="*****@*****.**", retries=1, brand=False),
        )

        interface = Mailer(config).start()
        message = interface.new(retries=2)

        assert message.author == ["*****@*****.**"]
        assert message.bcc == []
        assert message.retries == 2
        assert message.mailer is interface
        assert message.brand == False

        with pytest.raises(NotImplementedError):
            Message().send()

        assert message.send() == (message, True)

        message = interface.new("*****@*****.**", "*****@*****.**", "Test.")

        assert message.author == ["*****@*****.**"]
        assert message.to == ["*****@*****.**"]
        assert message.subject == "Test."
Esempio n. 2
0
def send_email(send_to, templaterich, templateplain, subject, **kwargs):
    """
        Sends an email to the target email with two types
            1) HTML
            2) Plain

        We will try the template with .htm for rich and .txt for plain.

        Both will rendered with Jinja2
    """

    mailer = Mailer(dict(
        transport=dict(use='smtp', host=config.EMAIL_SMTP_SERVER, debug=config.EMAIL_DEBUG),
        manager=dict()))

    mailer.start()

    message = mailer.new()
    message.author = config.EMAIL_SENDER
    message.to = send_to
    message.subject = subject

    template_rich = env.get_template(templaterich)
    template_plain = env.get_template(templateplain)

    message.rich = template_rich.render(**kwargs)
    message.plain = template_plain.render(**kwargs)

    logger.info('Sent an email to ' + send_to)

    message.send()
    mailer.stop()
class MailHandler(logging.Handler):
    """A class which sends records out via e-mail.
    
    This handler should be configured using the same configuration
    directives that Marrow Mailer itself understands.
    
    Be careful how many notifications get sent.
    
    It is suggested to use background delivery using the 'dynamic' manager.
    """
    
    def __init__(self, *args, **config):
        """Initialize the instance, optionally configuring TurboMail itself.
        
        If no additional arguments are supplied to the handler, re-use any
        existing running TurboMail configuration.
        
        To get around limitations of the INI parser, you can pass in a tuple
        of name, value pairs to populate the dictionary.  (Use `{}` dict
        notation in produciton, though.)
        """
        
        logging.Handler.__init__(self)
        
        self.config = dict()
        
        if args:
            config.update(dict(zip(*[iter(args)]*2)))
        
        self.mailer = Mailer(config).start()
        
        # If we get a configuration that doesn't explicitly start TurboMail
        # we use the configuration to populate the Message instance.
        self.config = config
    
    def emit(self, record):
        """Emit a record."""
        
        try:
            self.mailer.new(plain=self.format(record)).send()
        
        except (KeyboardInterrupt, SystemExit):
            raise
        
        except:
            self.handleError(record)
Esempio n. 4
0
class MailHandler(logging.Handler):
    """A class which sends records out via e-mail.
    
    This handler should be configured using the same configuration
    directives that Marrow Mailer itself understands.
    
    Be careful how many notifications get sent.
    
    It is suggested to use background delivery using the 'dynamic' manager.
    """
    def __init__(self, *args, **config):
        """Initialize the instance, optionally configuring TurboMail itself.
        
        If no additional arguments are supplied to the handler, re-use any
        existing running TurboMail configuration.
        
        To get around limitations of the INI parser, you can pass in a tuple
        of name, value pairs to populate the dictionary.  (Use `{}` dict
        notation in produciton, though.)
        """

        logging.Handler.__init__(self)

        self.config = dict()

        if args:
            config.update(dict(zip(*[iter(args)] * 2)))

        self.mailer = Mailer(config).start()

        # If we get a configuration that doesn't explicitly start TurboMail
        # we use the configuration to populate the Message instance.
        self.config = config

    def emit(self, record):
        """Emit a record."""

        try:
            self.mailer.new(plain=self.format(record)).send()

        except (KeyboardInterrupt, SystemExit):
            raise

        except:
            self.handleError(record)
Esempio n. 5
0
 def test_new(self):
     config = dict(manager=dict(use='immediate'), transport=dict(use='mock'),
             message=dict(author='*****@*****.**', retries=1, brand=False))
     
     interface = Mailer(config).start()
     message = interface.new(retries=2)
     
     self.assertEqual(message.author, ["*****@*****.**"])
     self.assertEqual(message.bcc, [])
     self.assertEqual(message.retries, 2)
     self.assertTrue(message.mailer is interface)
     self.assertEqual(message.brand, False)
     
     self.assertRaises(NotImplementedError, Message().send)
     
     self.assertEqual(message.send(), (message, True))
     
     message = interface.new("*****@*****.**", "*****@*****.**", "Test.")
     
     self.assertEqual(message.author, ["*****@*****.**"])
     self.assertEqual(message.to, ["*****@*****.**"])
     self.assertEqual(message.subject, "Test.")
Esempio n. 6
0
	def test_new(self):
		config = dict(
			manager=dict(use='immediate'), transport=dict(use='mock'),
			message=dict(author='*****@*****.**', retries=1, brand=False))
		
		interface = Mailer(config).start()
		message = interface.new(retries=2)
		
		assert message.author == ["*****@*****.**"]
		assert message.bcc == []
		assert message.retries == 2
		assert message.mailer is interface
		assert message.brand == False
		
		with pytest.raises(NotImplementedError):
			Message().send()
		
		assert message.send() == (message, True)

		message = interface.new("*****@*****.**", "*****@*****.**", "Test.")

		assert message.author == ["*****@*****.**"]
		assert message.to == ["*****@*****.**"]
		assert message.subject == "Test."
Esempio n. 7
0
def send_mail(subject, plain, html):
  """This function assumes that email is HTML formatted"""
  mailer = Mailer(dict( transport = dict(
    use = 'smtp',
    debug = config.EMAIL_DEBUG,
    host = config.EMAIL_HOST,
    port = config.EMAIL_PORT,
    username = config.EMAIL_FROM,
    password = config.EMAIL_PASS,
    tls = config.EMAIL_SSL),
    manager = dict())
  )

  mailer.start()
  message = mailer.new()
  message.subject = subject
  message.author = config.EMAIL_FROM
  message.to = config.EMAIL_TO
  message.plain = plain
  message.rich = html
  mailer.send(message)
  mailer.stop()
Esempio n. 8
0
def main():
    mailer = Mailer(
        dict(transport=dict(use='smtp',
                            host='smtp.163.com',
                            port=25,
                            username='******',
                            password='******',
                            timeout=30,
                            debug=False)))
    mailer.start()

    message = mailer.new(author="*****@*****.**",
                         to=["*****@*****.**", "*****@*****.**"])
    message.attach(name="/Users/kute/work/logs/a.txt")  # 附件
    message.subject = "Testing Marrow Mailer"
    # message.cc = AddressList(addresses=["*****@*****.**", "*****@*****.**"])  # 抄送-1
    message.cc = "[email protected],[email protected]"  # 抄送-2
    message.plain = "hi,你好,请问什么时候去吃饭啊,快饿死了"  # 普通文本内容
    message.rich = "<h1>为什么不回我</h1>"  # html文本
    mailer.send(message)

    mailer.stop()