def test_email(): "test if we can send an email" #options.'smtp_server'] == 'mockserver.com' # force mock smtp connect data = {'subject':'test email', 'message':'test body', "from_email":'Test Sender<*****@*****.**>', "recipient_list":'*****@*****.**') num_sent = mail.send_mail_toeach(data) assert num_sent == 1 data['recipient_list'] = ['*****@*****.**','*****@*****.**'] num_sent = mail.send_mail_toeach(data) assert num_sent == 2 mockserver = mail.get_smtp_server('mockserver.com') assert type(mockserver) == mail.mocksmtp assert mockserver.server_address == 'mockserver.com' num_sent = mail.send_email(data), to_each=True,server=mockserver)
def email_send(job_object): """Email background sendor format:: jsondict = { 'apikey':'optional val to override ds default', 'template_name':'thank_you_for_registering_with_demisauce', 'emails':['*****@*****.**'], 'template_data':{ 'displayname':'Bob User', 'title':'welcome', 'email':'*****@*****.**', 'link':'http://www.demisauce.com/?go' } } gearman_client.do_task(Task("email_send",json.dumps(jsondict), background=True)) """ try: emailargs = json.loads(job_object.arg) email_name = urllib.quote_plus(emailargs['template_name']) if 'apikey' in emailargs: response = demisauce_ws('email',email_name,cache=True,apikey=emailargs['apikey']) else: response = demisauce_ws('email',email_name,cache=True) if response.success and response.json and len(response.json) ==1: emailjson = response.json[0] s = Template(emailjson['template']) sh = Template(emailjson['template']) if emailjson['template_html'] not in (None,''): sh = Template(emailjson['template_html']) if 'template_data' in emailargs: template = s.substitute(emailargs['template_data']) templateh = sh.substitute(emailargs['template_data']) else: template = s.substitute({}) templateh = sh.substitute({}) #tuple: (subject, body, to<*****@*****.**>, [[email protected],[email protected]]) data = {'subject': emailjson['subject'], 'message':template, 'from_email':'%s<%s>' % (emailjson['from_name'],emailjson['from_email']), 'recipient_list': emailargs['emails'], 'message_html': templateh} num_sent = mail.send_mail_toeach(data) logging.info('sent email to %s, num_sent = %s' % (emailargs['emails'], num_sent)) return num_sent else: #logging.error("len json = %s" % (len(response.json))) logging.error('Error retrieving that template %s \n\n %s' % (emailargs,response.json)) return -1 except: #logging.error("Error in gearman task email_send: %s" % err) traceback.print_exc() return -1
def send_emails(email_template, recipient_list, substitution_dict=None): """ Gets an email template from demisauce and sends to recipient list using scheduler which runs in the background allowing this current request to continue processing """ raise NotImplemented("refactor needed") email = Template.GET(email_template) if email: from string import Template if email.template: s = Template(email.template) sh = Template(email.template_html) template = s.substitute(substitution_dict) mail.send_mail_toeach((t.subject, template, "%s<%s>" % (t.from_name, t.from_email), recipient_list)) log.debug("sent emails to %s" % recipient_list) else: log.error("Error retrieving that template 1") elif not emails.success: log.error("Error retrieving that template 2") return False