def _build_message(self, message): """A helper method to convert a PMEmailMessage to a PMMail""" if not message.recipients(): return False recipients = ','.join(message.to) recipients_cc = ','.join(message.cc) recipients_bcc = ','.join(message.bcc) text_body = message.body html_body = None if isinstance(message, EmailMultiAlternatives): for alt in message.alternatives: if alt[1] == "text/html": html_body = alt[0] break elif getattr(message, 'content_subtype', None) == 'html': # Don't send html content as plain text text_body = None html_body = message.body reply_to = ','.join(message.reply_to) custom_headers = {} if message.extra_headers and isinstance(message.extra_headers, dict): if 'Reply-To' in message.extra_headers: reply_to = message.extra_headers.pop('Reply-To') if len(message.extra_headers): custom_headers = message.extra_headers attachments = [] if message.attachments and isinstance(message.attachments, list): if len(message.attachments): for item in message.attachments: if isinstance(item, tuple): (f, content, m) = item content = base64.b64encode(content) # b64decode returns bytes on Python 3. PMMail needs a # str (for JSON serialization). Convert on Python 3 # only to avoid a useless performance hit on Python 2. if not isinstance(content, str): content = content.decode() attachments.append((f, content, m)) else: attachments.append(item) postmark_message = PMMail(api_key=self.api_key, subject=message.subject, sender=message.from_email, to=recipients, cc=recipients_cc, bcc=recipients_bcc, text_body=text_body, html_body=html_body, reply_to=reply_to, custom_headers=custom_headers, attachments=attachments) postmark_message.tag = getattr(message, 'tag', None) postmark_message.track_opens = getattr(message, 'track_opens', False) return postmark_message
def send_service(users): service_page = ServicePage.current_service_page() guest_next_service_link = User.get_guest_next_service_link() attachments = [] for f in service_page.email_attachments: with f.file as file: data = file.read() encoded_file = base64.b64encode(data).decode() attachments.append(encoded_file) for user in users: # Skip sending to users without emails if not user.email: continue m = PMMail( to=f"{user.first_name} {user.last_name} <{user.email}>", sender="Lynn Jackson [email protected]", template_id="19602506", template_model=dict( company_name="Crossroads Church", company_address="520 Westney Rd S, Ajax, ON L1S 6W6", first_name=user.first_name, last_name=user.last_name, date=service_page.date.strftime("%A %B %d, %Y"), stream_link=user.get_next_service_link(), guest_stream_link=guest_next_service_link, services_link=user.get_services_link(), foreword=service_page.description, # Note that this is HTML ), ) m.send()
def send_email(data, template_id=POSTMARK_APPLICATION_DECLINE_TEMPLATE, sender=POSTMARK_SENDER): pm = PMMail(to=data['email'], sender=POSTMARK_SENDER, template_id=template_id, template_model=data) pm.send()
def email_reminder(venue, user, last_checkin, checkin): template = env.get_template('core/email.txt') postmark_message = PMMail(api_key=app.config.get('POSTMARK_API_KEY'), subject='You just checked-in @ %s' % venue.get('venue_name'), sender=app.config.get('POSTMARK_SENDER'), to=user.get('email'), text_body=template.render(venue=venue, last_checkin=last_checkin, checkin=checkin)) postmark_message.send() scale_celery(0)
def contact(): pm = PMMail(api_key="0e1604ec-e20c-438f-b7c0-3ea34878b849", to='*****@*****.**', sender='*****@*****.**', template_id=17926943, template_model={ 'name': 'Bob', 'product_name': 'Catland', 'login_url': 'meow moew', 'username': '******', 'trial_start': '358357', 'trial_end': '5767373', 'sender_name': 'Kim' }) pm.send()
def send_notifications(community, email_dict): for email, vals in email_dict.items(): n_dict_list = [] c_dict_list = [] for n in vals[1]: n_dict_list.append(generate_notif_dict(n)) for c in vals[0]: c_dict_list.append( generate_chat_dict(c.chatroom, c.chatroom.descriptive_name(c.person))) subject = "[" + community.display_name + "] " if len(n_dict_list) > 0: subject += "New Comment on " + n_dict_list[0]["post_title"] elif len(c_dict_list) > 0: subject += "New Messages from " + c_dict_list[0]["room_name"] else: raise Exception("how?") template_model = { "community": community.display_name, "subject": subject, "logo": community.photo.url if community.photo else None, "domain": community.get_domain(), "notifications": n_dict_list, "chats": c_dict_list, } try: pm = PMMail( to=email, sender=community.display_name + " Notifications [email protected]", template_id=settings.POSTMARK_NOTIFICATION_TEMPLATE_ID, template_model=template_model, ) pm.send() except Exception as e: capture_exception(e) for n in vals[1]: n.should_send_email = False n.save() for pcrm in vals[0]: pcrm.last_email = pcrm.chatroom.last_message pcrm.save()
def accreditation_page(request): if request.method == 'POST': first_name = request.POST['first_name'] last_name = request.POST['last_name'] email = request.POST['email'] type_of_accreditation = request.POST['type_of_accreditation'] application_text = request.POST['application'] application = AccreditatonApplication.objects.create( first_name=first_name, last_name=last_name, email=email, type_of_accreditation=type_of_accreditation, application=application_text) try: application.full_clean() data = { 'first_name': first_name, 'last_name': last_name, 'email': email, } pm = PMMail(to=email, sender=POSTMARK_SENDER, template_id=POSTMARK_APPLICATION_SENT_TEMPLATE, template_model=data) pm.send() except ValidationError as e: application.delete() data = {'error': e} except PMMailSendException as e: application.delete() data = {'error': e} data['application'] = application_text data['type_of_accreditation'] = type_of_accreditation return render(request, 'application_accepted.html', data) else: return render(request, 'accreditation.html')