def send_digest(): sg = SendGridClient(SENDGRID_USER, SENDGRID_PASSWORD, secure=True) responses = [] today, next_week = week_interval() tomorrow = today + relativedelta(days=+1) date_format = "%m/%d" subject = 'NYC Jazz Digest: {} to {}'.format(tomorrow.strftime(date_format), next_week.strftime(date_format)) from_addr = 'NYC Jazz Digest <*****@*****.**>' all_shows = shows_this_week() text = all_shows_to_markdown(all_shows) for user in User.query(User.opt_out == False).fetch(): unsubcribe_link = ROOT_URL + '/unsubscribe/' + user.uuid unsubscribe_phrase = "\n\nClick [here]({}) to unsubscribe.".format(unsubcribe_link) text += unsubscribe_phrase html = markdown(text) message = Mail() message.set_subject(subject) message.set_html(html) message.set_text(text) message.set_from(from_addr) # add a recipient message.add_to(user.email) # use the Web API to send your message responses.append(str(sg.send(message))) return '\n'.join(responses)
def test_emails(): test_emails = ['*****@*****.**'] all_shows = shows_this_week() text = all_shows_to_markdown(all_shows) html = markdown(text) today, next_week = week_interval() tomorrow = today + relativedelta(days=+1) date_format = "%m/%d" responses = [] # make a secure connection to SendGrid sg = SendGridClient(SENDGRID_USER, SENDGRID_PASSWORD, secure=True) for email in test_emails: # make a message object message = Mail() message.set_subject('NYC Jazz Digest: {} to {}'.format(tomorrow.strftime(date_format), next_week.strftime(date_format))) message.set_html(html) message.set_text(text) message.set_from('NYC Jazz Digest <*****@*****.**>') # add a recipient message.add_to(email) # use the Web API to send your message responses.append(str(sg.send(message))) return '\n'.join(responses)
def test_send(self): m = Mail() m.add_to('John, Doe <*****@*****.**>') m.set_subject('test') m.set_html('WIN') m.set_text('WIN') m.set_from('*****@*****.**') m.add_substitution('subKey', 'subValue') m.add_section('testSection', 'sectionValue') m.add_category('testCategory') m.add_unique_arg('testUnique', 'uniqueValue') m.add_filter('testFilter', 'filter', 'filterValue') m.add_attachment_stream('testFile', 'fileValue') self.sg.send(m) url = self.sg._build_body(m) url.pop('api_key', None) url.pop('api_user', None) url.pop('date', None) testUrl = json.loads('''{"to[]": ["*****@*****.**"], "toname[]": ["John Doe"], "html": "WIN", "text": "WIN", "subject": "test", "files[testFile]": "fileValue", "from": "*****@*****.**", "headers": "", "fromname": "", "replyto": ""}''') testUrl['x-smtpapi'] = json.dumps(json.loads('''{"sub":{"subKey":["subValue"]}, "section":{"testSection":"sectionValue"}, "category":["testCategory"], "unique_args":{"testUnique":"uniqueValue"}, "filters":{"testFilter":{"settings":{"filter":"filterValue"}}}}''')) self.assertEqual(url, testUrl)
def send_email(report, title): API_KEY = os.environ.get('SENDGRID_KEY') if API_KEY is None: print 'No SendGrid API key found! Please set the SENDGRID_KEY env var' sys.exit(1) sg = SendGridClient(API_KEY, raise_errors=True) # Backwards compatability for emails stored as strings, not lists emails = report['project_details']['email'] if type(emails) is not list: emails = [emails] for address in emails: message = Mail() message.add_to(address) message.set_subject(title) message.set_html(generate_email_text(report)) message.set_from('STACKS <*****@*****.**>') try: sg.send(message) except SendGridError as e: print e except SendGridClientError as e: print e except SendGridServerError as e: print e
def send_email(report, title): API_KEY = os.environ.get('SENDGRID_KEY') if API_KEY is None: print 'No SendGrid API key found! Please set the SENDGRID_KEY env var' sys.exit(1) sg = SendGridClient(API_KEY, raise_errors=True) # Backwards compatability for emails stored as strings, not lists emails = report['project_details']['email'] if type(emails) is not list: emails = [emails] for address in emails: message = Mail() message.add_to(address) message.set_subject(title) message.set_html(generate_email_text(report)) message.set_from('STACKS <*****@*****.**>') try: sg.send(message) except SendGridError as e: print e except SendGridClientError as e: print e except SendGridServerError as e: print e
def test_send(self): m = Mail() m.add_to('John, Doe <*****@*****.**>') m.set_subject('test') m.set_html('WIN') m.set_text('WIN') m.set_from('*****@*****.**') m.set_asm_group_id(42) m.add_cc('*****@*****.**') m.add_bcc('*****@*****.**') m.add_substitution('subKey', 'subValue') m.add_section('testSection', 'sectionValue') m.add_category('testCategory') m.add_unique_arg('testUnique', 'uniqueValue') m.add_filter('testFilter', 'filter', 'filterValue') m.add_attachment_stream('testFile', 'fileValue') url = self.sg._build_body(m) url.pop('api_key', None) url.pop('api_user', None) url.pop('date', None) test_url = json.loads(''' { "to[]": ["*****@*****.**"], "toname[]": ["John Doe"], "html": "WIN", "text": "WIN", "subject": "test", "files[testFile]": "fileValue", "from": "*****@*****.**", "cc[]": ["*****@*****.**"], "bcc[]": ["*****@*****.**"] } ''') test_url['x-smtpapi'] = json.dumps(json.loads(''' { "sub": { "subKey": ["subValue"] }, "section": { "testSection":"sectionValue" }, "category": ["testCategory"], "unique_args": { "testUnique":"uniqueValue" }, "filters": { "testFilter": { "settings": { "filter": "filterValue" } } }, "asm_group_id": 42 } ''')) try: self.assertItemsEqual(url, test_url) except: # Python 3+ self.assertCountEqual(url, test_url)
def send_activation_mail(self): message = Mail() message.add_to(self.info['email']) message.set_subject('UthPortal activation') address = self.info['email'] token = self.info['token'] auth_id = self.info['auth_id'] self.logger.debug('domain: ' + self._domain) #TODO: make some proper html message.set_html( "Please click on the following link to activate your account:\ <a href={0}/api/v1/users/activate?email={1}&token={2}>Activate</a>, \ This is your 8-digit unique user id: {3}\ Use this in your app, when asked for it.\ This id is used to personalize your push notifications.\ Please don't share this ID as it is supposed to be kept secret."\ .format(self._domain, address, token, auth_id)) message.set_text('Token: {0}, 8-digit: {1}'.format(token, auth_id)) message.set_from('UthPortal <%s>' % self._email_from) try: self._sg.send(message) except SendGridError as error: self.logger.error('SendGrid error: ' + str(error)) raise NetworkError("Cannot send activation-email.", 500) except SendGridClientError as error: self.logger.error('SendGrid CLIENT error: ' + error.args[1]) raise NetworkError('Cannot send activation e-mail.', 500) except SendGridServerError as error: self.logger.error('SendGrid SERVER error: [{0}] -> [{1}] ',format( error.args[0], error.args[1] )) raise NetworkError('Mail server currently un-available', 503)
class SGEmailClient(object): def __init__(self): self.email_config = SendGridConfiguration.objects.all()[:1].get() self.sg = SendGridClient(self.email_config.api_key) self.message = Mail() self.message.set_from(self.email_config.email_from) self.message.set_from_name(self.email_config.email_from_name) self.message.set_subject(self.email_config.email_subject) def send_report_email(self, email, report): template_config = TemplateReport.objects.all()[:1].get() user = get_object_or_404(User, pk=email) logger.info(user) html = unicode(template_config.html_template).format( user_name=user.first_name, ) self.message.add_to(user.email) self.message.add_to_name(user.first_name) self.message.set_html(html) if report['report']: self.message.add_attachment_stream( report['name'] , report['report'] ) status, msg = self.sg.send(self.message) logger.info('{0} - {1}'.format(status, msg))
def test_send(self): m = Mail() m.add_to('John, Doe <*****@*****.**>') m.set_subject('test') m.set_html('WIN') m.set_text('WIN') m.set_from('*****@*****.**') m.set_asm_group_id(42) m.add_cc('*****@*****.**') m.add_bcc('*****@*****.**') m.add_substitution('subKey', 'subValue') m.add_section('testSection', 'sectionValue') m.add_category('testCategory') m.add_unique_arg('testUnique', 'uniqueValue') m.add_filter('testFilter', 'filter', 'filterValue') m.add_attachment_stream('testFile', 'fileValue') url = self.sg._build_body(m) url.pop('api_key', None) url.pop('api_user', None) url.pop('date', None) test_url = json.loads(''' { "to[]": ["*****@*****.**"], "toname[]": ["John Doe"], "html": "WIN", "text": "WIN", "subject": "test", "files[testFile]": "fileValue", "from": "*****@*****.**", "cc[]": ["*****@*****.**"], "bcc[]": ["*****@*****.**"] } ''') test_url['x-smtpapi'] = json.dumps( json.loads(''' { "sub": { "subKey": ["subValue"] }, "section": { "testSection":"sectionValue" }, "category": ["testCategory"], "unique_args": { "testUnique":"uniqueValue" }, "filters": { "testFilter": { "settings": { "filter": "filterValue" } } }, "asm_group_id": 42 } ''')) self.assertEqual(url, test_url)
def send_email_verification(user_id, resend=False): if not Configuration.PUSH_ENABLED: return sendgrid = create_sendgrid() user = User.find(user_id) verify_code = wigo_db.get_new_code({ 'type': 'verify_email', 'user_id': user_id, 'email': user.email }) verify_link = '{}://{}/c/{}'.format( 'https' if Configuration.ENVIRONMENT != 'dev' else 'http', Configuration.WEB_HOST, verify_code) logger.info('generated verify code for user "%s", "%s"' % (user.email, verify_code)) first_name = user.first_name if not first_name: first_name = user.email msg = Mail() if resend: msg.set_subject('Everyone deserves a second chance') else: msg.set_subject('Welcome to Wigo') msg.set_from('Wigo <*****@*****.**>') if user.first_name and user.last_name: msg.add_to('%s <%s>' % (user.full_name, user.email)) else: msg.add_to(user.email) msg.set_text( "Hi %s\n\nPlease click the following link to verify your email address:\n\n%s\n\n" % (first_name, verify_link)) msg.set_html( render_template('confirmation_email.html', name=first_name, confirmation_link=verify_link, resend=resend)) msg.add_unique_arg('user_id', user.id) msg.add_category('verify') msg.add_filter('opentrack', 'enable', 0) msg.add_filter('subscriptiontrack', 'enable', 1) msg.add_filter('subscriptiontrack', 'replace', '-UNSUB-') sendgrid.send(msg) logger.info('sent verification email to "%s"' % user.email)
def test_send(self): m = Mail() m.add_to("John, Doe <*****@*****.**>") m.set_subject("test") m.set_html("WIN") m.set_text("WIN") m.set_from("*****@*****.**") m.add_substitution("subKey", "subValue") m.add_section("testSection", "sectionValue") m.add_category("testCategory") m.add_unique_arg("testUnique", "uniqueValue") m.add_filter("testFilter", "filter", "filterValue") m.add_attachment_stream("testFile", "fileValue") url = self.sg._build_body(m) url.pop("api_key", None) url.pop("api_user", None) url.pop("date", None) test_url = json.loads( """ { "to[]": ["*****@*****.**"], "toname[]": ["John Doe"], "html": "WIN", "text": "WIN", "subject": "test", "files[testFile]": "fileValue", "from": "*****@*****.**" } """ ) test_url["x-smtpapi"] = json.dumps( json.loads( """ { "to" : ["John, Doe <*****@*****.**>"], "sub": { "subKey": ["subValue"] }, "section": { "testSection":"sectionValue" }, "category": ["testCategory"], "unique_args": { "testUnique":"uniqueValue" }, "filters": { "testFilter": { "settings": { "filter": "filterValue" } } } } """ ) ) self.assertEqual(url, test_url)
def send(sender_mail, send_to, subject, html, text=None): client = SendGridClient(current_app.config.get('SENDGRID_USERNAME'), current_app.config.get('SENDGRID_PASSWORD')) message = Mail() message.set_from(sender_mail) message.add_to(send_to) message.set_subject(subject) message.set_html(html) message.set_text(text) client.send(message)
def recieve_result(): account_sid = "ACe36f8844f05de80021faa460764b6d33" auth_token = "f22d67391209d2a4f8f54266cd721978" client = TwilioRestClient(account_sid, auth_token) global numberTwo value = request.form fromValue = value.get("From") bodyValue = value.get("Body") toValue = value.get("To") # print "Body: " + bodyValue # print "From: " + fromValue # print "To: " + toValue value = "True" if str(bodyValue) == "An outage was reported in your area. We expect this to be resolved by 6pm today.": value = "False" elif str(bodyValue).strip().lower() == "yes": value = "yes" if value == "True": htmlForEmail = '<html><body><img src="http://wedte.com/wp-content/uploads/2013/01/PowerOutage.jpg" alt="Power Outage"><p></p><p></p><h3> We think that your house may have a power outage. If this is true, simply reply to this e-mail with any response so that the Electricty Supplier can serve you faster. <p></p><br><br></h3></body></html>' sg = SendGridClient(SendGridUserName, SendGridPassword) message = Mail() message.add_to(emailValue) message.set_subject("Is there a Power Outage at your house?") message.set_html(htmlForEmail) message.set_from(FromEmail) status, msg = sg.send(message) message = client.messages.create( body="We think that your house may have a power outage. If this is true, simply reply to this text with a 'yes' so that the Electricty Supplier can serve you faster.", to=str(personalNumber), # Replace with your phone number from_=str(number), ) # Replace with your Twilio number elif value == "yes": value = "True" elif value == "no": value = "False" payload = {"powerOutage": value, "twilioNumber": number} r = requests.post("http://ec2-54-68-73-74.us-west-2.compute.amazonaws.com:5000/powerreply", data=payload) numberTwo = number print str(value) print numberTwo return "Test"
def sendEmail(sender, recipient, subject, html, text): sg = SendGridClient('ecpf', 'Iheart1!', secure=True) message = Mail() message.set_subject(subject) message.set_html(html) message.set_text(text) message.set_from(sender) message.add_to(recipient) sg.send(message) return True
def send_message_with_sg(send_to, send_from, subject, body): message = Mail() message.add_to(send_to) message.set_from(send_from) message.set_subject(subject) message.set_html(body) print "Sending email:({}) to :({})".format(subject, send_to) sg.send(message)
def sendEmail(sender, recipient, subject, html, text) : sg= SendGridClient('ecpf', 'Iheart1!', secure=True) message = Mail() message.set_subject(subject) message.set_html(html) message.set_text(text) message.set_from(sender) message.add_to(recipient) sg.send(message) return True
def test_drop_to_header(self): m = Mail() m.add_to('John, Doe <*****@*****.**>') m.set_from('*****@*****.**') m.set_subject('test') m.set_text('test') m.add_bcc('John, Doe <*****@*****.**>') url = self.sg._build_body(m) print url
def test_drop_to_header(self): smtpapi = "{}" m = Mail() m.add_to("John, Doe <*****@*****.**>") m.set_from("*****@*****.**") m.set_subject("test") m.set_text("test") m.add_bcc("John, Doe <*****@*****.**>") url = self.sg._build_body(m) self.assertEqual(smtpapi, url["x-smtpapi"])
def _send_message_with_sg(send_to, send_from, subject, body): message = Mail() message.add_to(send_to) message.set_from(send_from) message.set_subject(subject) message.set_html(body) print "Sending email:({}) to :({})".format(subject, send_to) sg.send(message)
class EmailClient(object): def __init__(self): self.sg = SendGridClient(SG_API_KEY) self.message = Mail() self.message.set_from(SG_FROM) self.message.set_from_name(SG_FROM_NAME) def send_sg_email(self, correo): # valores de envío self.message.add_to(correo.correo) self.message.add_to_name(correo.nombre_cliente) self.message.set_subject(correo.asunto) self.message.set_html(correo.html) # valores personalizados unique_args = { 'empresa': correo.empresa, 'rut_receptor': correo.rut_receptor, 'rut_emisor': correo.rut_emisor, 'tipo_envio': correo.tipo_envio, 'tipo_dte': correo.tipo_dte, 'numero_folio': correo.numero_folio, 'resolucion_receptor': correo.resolucion_receptor, 'resolucion_emisor': correo.resolucion_emisor, 'monto': correo.monto, 'fecha_emision': correo.fecha_emision, 'fecha_recepcion': correo.fecha_recepcion, 'estado_documento': correo.estado_documento, 'tipo_operacion': correo.tipo_operacion, 'tipo_receptor': correo.tipo_receptor, } self.message.set_unique_args(unique_args) # Validacion de adjuntos if correo.attachs: for adjunto in correo.attachs: adj = AttachModel.query(ancestor=adjunto).get() self.message.add_attachment_stream(adj.nombre, adj.archivo) # enviando el mail status, msg = self.sg.send(self.message) # imprimiendo respuesta logging.info(status) logging.info(msg) def send_mail_to_user_attach(self, correo): # valores de envío self.message.add_to(correo['email']) self.message.add_to_name(correo['user_name']) self.message.set_subject(correo['subject']) self.message.set_html(correo['html']) if correo['attach']: self.message.add_attachment_stream(correo['attach']['name'], correo['attach']['file']) status, msg = self.sg.send(self.message) # imprimiendo respuesta logging.info(status) logging.info(msg)
def config_mail(): message = Mail() message.add_to("<*****@*****.**>") message.add_to_name("Kumar, Palani") message.set_from("*****@*****.**") message.set_from_name("Kavin"); message.set_subject("Test message.,") message.set_text("Test text") message.set_html("<i><b>Test HTML</b></i>") message.set_replyto("*****@*****.**") message.set_date(rfc822.formatdate()) return message
def config_mail(): message = Mail() message.add_to("<*****@*****.**>") message.add_to_name("Kumar, Palani") message.set_from("*****@*****.**") message.set_from_name("Kavin") message.set_subject("Test message.,") message.set_text("Test text") message.set_html("<i><b>Test HTML</b></i>") message.set_replyto("*****@*****.**") message.set_date(rfc822.formatdate()) return message
def send_email(name: str, address: str, subject: str, template: str, values: dict) -> bool: sg = SendGridClient(config.SENDGRID_API_USER, config.SENDGRID_API_KEY) mail = Mail() mail.set_from('tototo <*****@*****.**>') mail.add_to(name + ' <' + address + '>') mail.set_subject(subject) mail.set_html(render_template(template, **values)) status, _ = sg.send(mail) return status == 200
def send_email(name: str, address: str, subject: str, template: str, values: dict) -> bool: sg = SendGridClient(config.SENDGRID_API_USER, config.SENDGRID_API_KEY) mail = Mail() mail.set_from('tototo <*****@*****.**>') mail.add_to(name + ' <' + address + '>') mail.set_subject(subject) mail.set_html(render_template(template, **values)) status, _ = sg.send(mail) return status == 200
def get_sendgrid_request_message(cfg, keyid, hex, user_email): url_prefix = urljoin(cfg.config.megserver_hostname_url, os.path.join(cfg.config.meg_url_prefix, "revoke")) params = urlencode([("keyid", keyid), ("token", hex)]) parsed = list(urlparse(url_prefix)) parsed[4] = params revocation_link = urlunparse(parsed) message = Mail() message.add_to(user_email) message.set_from(cfg.config.sendgrid.from_email) message.set_subject(cfg.config.sendgrid.subject) message.set_html(EMAIL_HTML.format(keyid=keyid, link=revocation_link)) return message
def get_sendgrid_request_message(cfg, keyid, hex, user_email): url_prefix = urljoin( cfg.config.megserver_hostname_url, os.path.join(cfg.config.meg_url_prefix, "revoke") ) params = urlencode([("keyid", keyid), ("token", hex)]) parsed = list(urlparse(url_prefix)) parsed[4] = params revocation_link = urlunparse(parsed) message = Mail() message.add_to(user_email) message.set_from(cfg.config.sendgrid.from_email) message.set_subject(cfg.config.sendgrid.subject) message.set_html(EMAIL_HTML.format(keyid=keyid, link=revocation_link)) return message
def send_email(self, request): # make a secure connection to SendGrid sg = SendGridClient(get_mail_username(), get_mail_pass(), secure=True) # make a message object message = Mail() message.set_subject('message subject') message.set_html('<strong>HTML message body</strong>') message.set_text('plaintext message body') message.set_from('*****@*****.**') # add a recipient message.add_to('John Doe <*****@*****.**>') # use the Web API to send your message sg.send(message) return message_types.VoidMessage()
def send_email(self, request): # make a secure connection to SendGrid sg = SendGridClient(get_mail_username(), get_mail_pass(), secure=True) # make a message object message = Mail() message.set_subject('message subject') message.set_html('<strong>HTML message body</strong>') message.set_text('plaintext message body') message.set_from('*****@*****.**') # add a recipient message.add_to('John Doe <*****@*****.**>') # use the Web API to send your message sg.send(message) return message_types.VoidMessage()
def get(self): # make a secure connection to SendGrid #sg = SendGridClient('userid', 'password', secure=True) print sg # make a message object message = Mail() message.set_subject('message subject') message.set_html('<strong>HTML message body</strong>') message.set_text('plaintext message body') message.set_from('*****@*****.**') # add a recipient message.add_to('Name <email>') # use the Web API to send your message sg.send(message) self.response.write('email sent.')
def send_custom_email(user, subject, category, html, text, template_params=None): sendgrid = create_sendgrid() msg = Mail() msg.set_from('Wigo <*****@*****.**>') if user.first_name and user.last_name: msg.add_to('%s <%s>' % (user.full_name, user.email)) else: msg.add_to(user.email) # subject and body are required, for some reason, with the sendgrid templates msg.set_subject(Template(subject).render(user=user)) # turn on email opens tracking msg.add_filter('opentrack', 'enable', 1) msg.add_filter('subscriptiontrack', 'enable', 1) msg.add_filter('subscriptiontrack', 'replace', '-UNSUB-') params = {'user': user} if template_params: params.update(template_params) html = render_template('custom_email_inlined.html', html=Template(html).render(**params)) msg.set_html(html) if text: text = Template(text + '\n\n\nClick here to unsubscribe: -UNSUB-\n').render( **params) msg.set_text(text) msg.add_unique_arg('user_id', user.id) if category: msg.add_category(category) sendgrid.send(msg)
def test__build_body_unicode(self): """test _build_body() handles encoded unicode outside ascii range""" from_email = '\xd0\x9d\xd0\xb8\xd0\xba\xd0\[email protected]' from_name = '\xd0\x9a\xd0\xbb\xd0\xb0\xd0\xb2\xd0\xb4\xd0\xb8\xd1\x8f' subject = '\xd0\x9d\xd0\xb0\xd0\xb4\xd0\xb5\xd0\xb6\xd0\xb4\xd0\xb0' text = '\xd0\x9d\xd0\xb0\xd0\xb4\xd0\xb5\xd0\xb6\xd0\xb4\xd0\xb0' html = '\xd0\x9d\xd0\xb0\xd0\xb4\xd0\xb5\xd0\xb6\xd0\xb4\xd0\xb0' m = Mail() m.add_to('John, Doe <*****@*****.**>') m.set_subject(subject) m.set_html(html) m.set_text(text) m.set_from("%s <%s>" % (from_name, from_email)) url = self.sg._build_body(m) self.assertEqual(from_email, url['from']) self.assertEqual(from_name, url['fromname']) self.assertEqual(subject, url['subject']) self.assertEqual(text, url['text']) self.assertEqual(html, url['html'])
def test__build_body_unicode(self): """test _build_body() handles encoded unicode outside ascii range""" from_email = "\xd0\x9d\xd0\xb8\xd0\xba\xd0\[email protected]" from_name = "\xd0\x9a\xd0\xbb\xd0\xb0\xd0\xb2\xd0\xb4\xd0\xb8\xd1\x8f" subject = "\xd0\x9d\xd0\xb0\xd0\xb4\xd0\xb5\xd0\xb6\xd0\xb4\xd0\xb0" text = "\xd0\x9d\xd0\xb0\xd0\xb4\xd0\xb5\xd0\xb6\xd0\xb4\xd0\xb0" html = "\xd0\x9d\xd0\xb0\xd0\xb4\xd0\xb5\xd0\xb6\xd0\xb4\xd0\xb0" m = Mail() m.add_to("John, Doe <*****@*****.**>") m.set_subject(subject) m.set_html(html) m.set_text(text) m.set_from("%s <%s>" % (from_name, from_email)) url = self.sg._build_body(m) self.assertEqual(from_email, url["from"]) self.assertEqual(from_name, url["fromname"]) self.assertEqual(subject, url["subject"]) self.assertEqual(text, url["text"]) self.assertEqual(html, url["html"])
def send_alerts(score, threshold): sub_list = db.subscribers.find_one({"_id": 13722}) for email in sub_list['emails']: message = Mail() message.add_to(email) message.set_subject("Nice weather alert") message.set_html(alert_text.format(score, threshold)) message.set_text(alert_text.format(score, threshold)) message.set_from('Saunter <*****@*****.**>') try: status, msg = sg.send(message) debug_printer.pprint(status) debug_printer.pprint(msg) except SendGridClientError: debug_printer.pprint(SendGridClientError) except SendGridServerError: debug_printer.pprint(SendGridServerError)
def test__build_body_unicode(self): """test _build_body() handles encoded unicode outside ascii range""" from_email = '\xd0\x9d\xd0\xb8\xd0\xba\xd0\[email protected]' from_name = '\xd0\x9a\xd0\xbb\xd0\xb0\xd0\xb2\xd0\xb4\xd0\xb8\xd1\x8f' subject = '\xd0\x9d\xd0\xb0\xd0\xb4\xd0\xb5\xd0\xb6\xd0\xb4\xd0\xb0' text = '\xd0\x9d\xd0\xb0\xd0\xb4\xd0\xb5\xd0\xb6\xd0\xb4\xd0\xb0' html = '\xd0\x9d\xd0\xb0\xd0\xb4\xd0\xb5\xd0\xb6\xd0\xb4\xd0\xb0' m = Mail() m.add_to('John, Doe <*****@*****.**>') m.set_subject(subject) m.set_html(html) m.set_text(text) m.set_from("%s <%s>" % (from_name, from_email)) url = self.sg._build_body(m) self.assertEqual(from_email, url['from']) self.assertEqual(from_name, url['fromname']) self.assertEqual(subject, url['subject']) self.assertEqual(text, url['text']) self.assertEqual(html, url['html'])
def sendEmailWithSendGrid(sender, to, subject, body, html): # make a secure connection to SendGrid sg = SendGridClient(username, password, secure=True) # make a message object message = Mail() message.set_subject(subject) message.set_html(html) message.set_text(body) message.set_from(sender) # add a recipient message.add_to(to) logging.debug("message %s" % message) # use the Web API to send your message sg.send(message)
def run(self, schedule, content, recipient_queues, sharding_count_name): futures = [] for recipient in json.loads(recipient_queues.data): message = Mail() message.set_subject(schedule.subject) message.set_html( replace_edm_csv_property(content, recipient, schedule.replace_edm_csv_property)) message.set_from('%s <%s>' % (schedule.sender_name, schedule.sender_email)) if schedule.reply_to: message.set_replyto(schedule.reply_to) message.add_to(recipient.get('email')) message.add_category(schedule.category) status, msg = self._send(message, schedule.is_dry_run, schedule.dry_run_fail_rate) futures.append(dict(recipient=recipient, status=status, msg=msg)) send_success = filter(lambda f: f.get('status') == 200, futures) send_fail = filter(lambda f: f.get('status') != 200, futures) # save success send log if send_success: self.to_put.extend( self.success_log(schedule, send_success, self.sender)) # save fail send log if send_fail: self.to_put.extend( self.fail_log(schedule, send_fail, self.sender, content)) recipient_queues.status = 'executed' self.to_put.append(recipient_queues) if self.to_put: ndb.put_multi(self.to_put) self.to_put = [] general_counter.increment(sharding_count_name)
def threshold_not_met(petitioner_id, organization, election, threshold, signatures, position, petition_email, admin_emails): petitioner = netid2name(petitioner_id) # Make a secure connection to SendGrid sg = SendGridClient('harmonica1243', 'xqBrrnNqbGuvtEUpNRs3RePW', secure=True) # Replace with the message that we want to send, such as the name of the person and the position html_petition = '<p style="text-align: left;"><b>Dear ' + petitioner + ',</b></p><p style="text-align: center;">' \ '<p>Your petition no longer has the required number of signatures</p><ul><li><b>Organization:</b> ' \ + organization + '</li><li><b>Election:</b> ' + election + '</li><li><b>Position:</b> ' + position + '</li>' \ '<li><b>Number of Signatures Required:</b> ' + threshold + '</li><li><b>Number of Signatures Obtained:</b> ' \ + signatures + '</li></ul><p>Please contact the organization directly with any questions about the petition.' \ '</p></p><p style="text-align: left;"><b>Rice Apps Petitions</b></p>' html_admin = '<p style="text-align: left;"><b>Dear Organization Administrator,</b></p><p style="text-align: center;">' \ '<p>This petition no longer has the required number of signatures</p><ul><li><b>Petition Creator:</b> ' \ + petitioner + '</li><li><b>Organization:</b> ' + organization + '</li><li><b>Election:</b> ' + election + \ '</li><li><b>Position:</b> ' + position + '</li><li><b>Number of Signatures Required:</b> ' + threshold + \ '</li><li><b>Number of Signatures Obtained:</b> ' + signatures + '</li></ul><p>Please contact ' + ADMIN_ID + \ '@rice.edu with any questions.</p></p><p style="text-align: left;"><b>Rice Apps Petitions</b></p>' # Make a message Object message = Mail() # Message Subject, Body and To message.set_subject('Petition No Longer Has Enough Signatures') message.set_html(html_petition) message.set_from('*****@*****.**') message.add_to('<' + petition_email + '>') sg.send(message) # Make a message Object message = Mail() # Message Subject, Body and To message.set_subject('Petition No Longer Has Enough Signatures') message.set_html(html_admin) message.set_from('*****@*****.**') for email in admin_emails: message.add_to('<' + email + '>') sg.send(message)
def send_mail(): """ Send email with sendgrid sdk """ to_email = request.form.get('to') subject = request.form.get('subject') body = request.form.get('body') text_body = request.form.get('text_body') from_email = request.form.get('from') utf8_body = body.encode('utf-8') utf8_text_body = text_body.encode('utf-8') message = Mail() message.set_subject(subject) message.set_html(utf8_body) message.set_text(utf8_text_body) message.set_from(from_email) message.add_to(to_email) sg.send(message) logger.info("Email is sent to %s" % to_email) return ('', 204)
def send_email(subject='Test email', from_email='*****@*****.**', from_name='D. Goodwin', recipients=['*****@*****.**'], text_body='Hello from Flask', html_body='Hello from <b>Flask</b>'): from sendgrid import SendGridClient, Mail api_key = 'm3tr0pl4c3s' api_user = '******' sendgrid = SendGridClient(username=api_user, password=api_key, raise_errors=True, host='http://api.sendgrid.com', port=80, ) message = Mail() message.add_to(recipients) message.set_from(from_email) message.set_from_name(from_name) message.set_subject(subject) message.set_text(text_body) message.set_html(html_body) try: sendgrid.send(message) except Exception as e: print "fail: %s" %(e)
def resend(self, retries): futures = [] for fail_log in ndb.get_multi([retry.failEmail for retry in retries]): if not fail_log: continue sendgrid = settings.SENDGRID[fail_log.sendgrid_account] self.set_sendgrid_client(sendgrid['USERNAME'], sendgrid['PASSWORD']) log_mail = LogEmail.query(LogEmail.fails_link.IN([fail_log.key ])).get() if log_mail: logging.info('fail mail %s-%s has been retry success.' % (fail_log.subject, fail_log.to)) else: message = Mail() message.set_subject(fail_log.subject) message.set_html(fail_log.body) message.set_from('%s <%s>' % (fail_log.sender_name, fail_log.sender_email)) if fail_log.reply_to: message.set_replyto(fail_log.reply_to) message.add_to(fail_log.to) message.add_category(fail_log.category) status, msg = self._send(message, fail_log.is_dry_run, fail_log.dry_run_fail_rate) futures.append(dict(fail_log=fail_log, status=status, msg=msg)) # split log to another task to save send_success = filter(lambda f: f.get('status') == 200, futures) send_fail = filter(lambda f: f.get('status') != 200, futures) if send_success: def clear_body(send): send.get('fail_log').body = None return send self.to_put.extend( self.success_log_retry(map(clear_body, send_success))) # retry fail log has been send succes and need to remove keys = [ r.key for r in filter( lambda r: any(s == r.failEmail for s in map( lambda s: s.get('fail_log').key, send_success)), retries) ] if keys: self.to_delete.extend(keys) if send_fail: self.to_put.extend(self.fail_log_retry(send_fail)) if self.to_put: ndb.put_multi(self.to_put) self.to_put = [] if self.to_delete: ndb.delete_multi(self.to_delete) self.to_delete = []
def post(self): applicant = self.user application_key = ndb.Key(urlsafe=self.request.get('form-key')) application = application_key.get() if self._no_verify() or application.submit_time: logging.info("Attempt to modify verification by %s", applicant.email) self._serve_page() return task = self.request.get('task') if task != 'applicant': user_id = self.user.get_id() token = self.user_model.create_signup_token(user_id) verification_url = self.uri_for('verification', type='v', user_id=user_id, signup_token=token, _full=True) logging.info(verification_url) config = ndb.Key(Settings, 'config').get() sg = SendGridClient(config.sendgrid_username, config.sendgrid_password, secure=True) verification_email = Mail(from_name="NYDKC Awards Committee", from_email="*****@*****.**", subject="Distinguished Key Clubber Application Verification for %s %s" % (applicant.first_name, applicant.last_name) ) verifier = "" if task == 'ltg': application.verification_ltg_email = self.request.get('ltg-email') application.verification_ltg_token = token application.verification_ltg_sent = True verification_email.add_to(application.verification_ltg_email) verifier = "Lieutenant Governor " + applicant.ltg.title() elif task == 'club-president': application.verification_club_president_email = self.request.get('club-president-email') application.verification_club_president_token = token application.verification_club_president_sent = True verification_email.add_to(application.verification_club_president_email) verifier = "Club President " + applicant.club_president.title() elif task == 'faculty-advisor': application.verification_faculty_advisor_email = self.request.get('faculty-advisor-email') application.verification_faculty_advisor_token = token application.verification_faculty_advisor_sent = True verification_email.add_to(application.verification_faculty_advisor_email) verifier = "Faculty Advisor " + applicant.faculty_advisor.title() template_values = { 'applicant': applicant, 'verification_url': verification_url, 'verifier': verifier } verification_email.set_html(JINJA_ENVIRONMENT.get_template('verification-email.html').render(template_values)) htmlhandler = html2text.HTML2Text() verification_email.set_text(htmlhandler.handle(verification_email.html).encode("UTF+8")) verification_email.add_unique_arg('user_id', user_id) code, response = sg.send(verification_email) response = json.loads(response) if response["message"] == "error": logging.error(("Problem with sending email to %s: " % verification_email.to) + str(response["errors"])) self._serve_page() return else: application.verification_applicant = True application.verification_applicant_date = datetime.now() application.put() self._serve_page()
for t_group in t_groups: templates = t_group['versions'] for template in templates: if template['name'] == template_name: return t_group['id'] template_id = get_template_id_by_name(template_name) message = Mail() message.add_filter('templates', 'enable', '1') message.add_filter('templates', 'template_id', template_id) message.set_subject(None) for to_addr in to_addrs: message.add_to(to_addr) for key, value in context.items(): message.add_substitution("%{}%".format(key), value) message.set_from('Foo <*****@*****.**>') message.set_html(' ') message.set_text(' ') message.set_subject(' ') # scrapy # xpath selection # find td with `dc.identifier.uri` as text and get text of its sibling url = response.xpath( '//td[contains(., "dc.identifier.uri")]/following-sibling::td/text()')
for t_group in t_groups: templates = t_group['versions'] for template in templates: if template['name'] == template_name: return t_group['id'] template_id = get_template_id_by_name(template_name) message = Mail() message.add_filter('templates', 'enable', '1') message.add_filter('templates', 'template_id', template_id) message.set_subject(None) for to_addr in to_addrs: message.add_to(to_addr) for key, value in context.items(): message.add_substitution("%{}%".format(key), value) message.set_from('Foo <*****@*****.**>') message.set_html(' ') message.set_text(' ') message.set_subject(' ')
def post(self): applicant = self.user application_key = ndb.Key(urlsafe=self.request.get('form-key')) application = application_key.get() if self._no_verify() or application.submit_time: logging.info("Attempt to modify verification by %s", applicant.email) self._serve_page() return task = self.request.get('task') if task != 'applicant': user_id = self.user.get_id() token = self.user_model.create_signup_token(user_id) verification_url = self.uri_for('verification', type='v', user_id=user_id, signup_token=token, _full=True) logging.info(verification_url) config = ndb.Key(Settings, 'config').get() sg = SendGridClient(config.sendgrid_username, config.sendgrid_password, secure=True) verification_email = Mail( from_name="NYDKC Awards Committee", from_email="*****@*****.**", subject= "Distinguished Key Clubber Application Verification for %s %s" % (applicant.first_name, applicant.last_name)) verifier = "" if task == 'ltg': application.verification_ltg_email = self.request.get( 'ltg-email') application.verification_ltg_token = token application.verification_ltg_sent = True verification_email.add_to(application.verification_ltg_email) verifier = "Lieutenant Governor " + applicant.ltg.title() elif task == 'club-president': application.verification_club_president_email = self.request.get( 'club-president-email') application.verification_club_president_token = token application.verification_club_president_sent = True verification_email.add_to( application.verification_club_president_email) verifier = "Club President " + applicant.club_president.title() elif task == 'faculty-advisor': application.verification_faculty_advisor_email = self.request.get( 'faculty-advisor-email') application.verification_faculty_advisor_token = token application.verification_faculty_advisor_sent = True verification_email.add_to( application.verification_faculty_advisor_email) verifier = "Faculty Advisor " + applicant.faculty_advisor.title( ) template_values = { 'applicant': applicant, 'verification_url': verification_url, 'verifier': verifier } verification_email.set_html( JINJA_ENVIRONMENT.get_template( 'verification-email.html').render(template_values)) htmlhandler = html2text.HTML2Text() verification_email.set_text( htmlhandler.handle(verification_email.html).encode("UTF+8")) verification_email.add_unique_arg('user_id', str(user_id)) code, response = sg.send(verification_email) response = json.loads(response) if response["message"] == "error": logging.error(("Problem with sending email to %s: " % verification_email.to) + str(response["errors"])) self._serve_page() return else: application.verification_applicant = True application.verification_applicant_date = datetime.now() application.put() self._serve_page()