def sendmail(mailconf, message): """Takes a regular dictionary as mailconf, as follows. Example:: mailconf = dict( host="smtp.gmail.com", # required port=25, # optional, default 25 or 587 for SSL/TLS username=foo, # optional, no default password=bar, # optional, no default tls=True, # optional, default False ) d = mail.sendmail(mailconf, msg) d.addCallback(on_response) """ if not isinstance(mailconf, types.DictType): raise TypeError("mailconf must be a regular python dictionary") if not isinstance(message, Message): raise TypeError("message must be an instance of cyclone.mail.Message") host = mailconf.get("host") if isinstance(host, unicode): host = str(unicode) if not isinstance(host, types.StringType): raise ValueError("mailconf requires a 'host' configuration") use_tls = mailconf.get("tls") if use_tls: port = mailconf.get("port", 587) contextFactory = ClientContextFactory() contextFactory.method = SSLv3_METHOD else: port = mailconf.get("port", 25) contextFactory = None if not isinstance(port, types.IntType): raise ValueError("mailconf requires a proper 'port' configuration") result = Deferred() u = mailconf.get("username") p = mailconf.get("password") factory = ESMTPSenderFactory(u, p, quoteaddr(message.from_addr), message.to_addrs, message.render(), result, contextFactory=contextFactory, requireAuthentication=(u and p), requireTransportSecurity=use_tls) reactor.connectTCP(host, port, factory) return result
def testQuoteAddr(self): cases = [ ['*****@*****.**', '<*****@*****.**>'], ['"User Name" <*****@*****.**>', '<*****@*****.**>'], [smtp.Address('someguy@someplace'), '<someguy@someplace>'], ['', '<>'], [smtp.Address(''), '<>'], ] for (c, e) in cases: self.assertEquals(smtp.quoteaddr(c), e)
def _sendMail_to(self, code, resp): try: self.sendLine('RCPT TO:%s' % quoteaddr(self._requestParams['toIter'].next())) except StopIteration: self.sendLine('DATA') self._expected = [354] self._okresponse = self._sendMail_data self._failresponse = self._sendMail_fail else: self._expected = SUCCESS self._okresponse = self._sendMail_to self._failresponse = self._sendMail_fail
def _sendMail_from(self, code, resp): self.sendLine('MAIL FROM:%s' % quoteaddr(self._requestParams['sender'])) self._expected = SUCCESS self._okresponse = self._sendMail_to self._failresponse = self._sendMail_fail