def promote_client(user: _User, source: str) -> None: """ Promote a user to Client role and change it's role on Mailchimp. Will not fail in case API call fails. Email welcome email is sent to user :param source: source of traffic :param user: :return: """ _core_facade.promote_to_client(user, source) try: _mailchimp_facade.create_or_update_client(user.first_name, user.email) except _MailChimpError: pass email_msg = render_to_string( 'payments/pytools_email.txt', { 'user': user, 'ty_url': build_absolute_uri(reverse('payments:pytools_thanks')) } ) _send_mail( 'Inscrição no curso Pytools realizada! Confira o link com detalhes.', email_msg, _settings.DEFAULT_FROM_EMAIL, [user.email] )
def promote_member(user: _User, source: str) -> _User: """ Promote a user to Member role and change it's role on Email Marketing. Will not fail in case API call fails. Email welcome email is sent to user :param source: source of traffic :param user: :return: """ _core_facade.promote_to_member(user, source) _cohorts_facade.subscribe_to_last_cohort(user) cohort = _cohorts_facade.find_most_recent_cohort() sync_user_on_discourse(user) try: _email_marketing_facade.create_or_update_member(user.first_name, user.email, id=user.id) _email_marketing_facade.tag_as(user.email, user.id, f'turma-{cohort.slug}') except _ActiveCampaignError: pass email_msg = render_to_string( 'payments/membership_email.txt', { 'user': user, 'cohort_detail_url': build_absolute_uri(cohort.get_absolute_url()) }) _send_mail( f'Inscrição na Turma {cohort.title} realizada! Confira o link com detalhes.', email_msg, _settings.DEFAULT_FROM_EMAIL, [user.email]) return user
def send_mail(template, context={}, **kwds): if not template.endswith((".html", ".htm")): raise TypeError(f"invalid template name: {template}") t = get_template(Path("email") / template) html = t.render(context) text = BeautifulSoup(html, "html.parser").text kwds["from_email"] = settings.DEFAULT_FROM_EMAIL kwds["html_message"] = html kwds["message"] = text _send_mail(**kwds)
def send_mail(subject, message, recipient, from_email=DEFAULT_SENDER, html_message=None): try: return _send_mail(subject, message, from_email, [recipient], html_message=html_message) except SMTPException as e: LOG.exception("Failed to send email", e)
def send_mail(subject, message, recipient_list, request): """ Wrapper for send_mail() with logging and error messaging :param subject: Message subject (string) :param message: Message body (string) :param recipient_list: Recipients of email (list) :param request: Request object :return: Amount of sent email (int) """ try: ret = _send_mail( subject=subject, message=message, from_email=EMAIL_SENDER, recipient_list=recipient_list, fail_silently=False, ) logger.debug('{} email{} sent to {}'.format(ret, 's' if ret != 1 else '', ', '.join(recipient_list))) return ret except Exception as ex: error_msg = 'Error sending email: {}'.format(str(ex)) logger.error(error_msg) if DEBUG: raise ex messages.error(request, error_msg) return 0
def send_mail(subject, message, from_email, recipient_list, fail_silently=False): """ Wrapper that forces sending mail through our connection. """ return _send_mail( subject, message, from_email, recipient_list, connection=get_connection(fail_silently=fail_silently) )
def promote_client(user: _User, email_msg: str) -> None: """ Promote a user to Client role and change it's role on Mailchimp. Will not fail in case API call fails. Email welcome email is sent to user :param email_msg: :param user: :return: """ _core_facade.promote_to_client(user) try: _mailchimp_facade.create_or_update_client(user.first_name, user.email) except _MailChimpError: pass _send_mail( 'Inscrição no curso Pytools realizada! Confira o link com detalhes.', email_msg, _settings.DEFAULT_FROM_EMAIL, [user.email])
def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None, connection=None, html_message=None, communication_type=None): """ Light wrapper over Django's send_mail which filters out recipients who opted out of using email via their `CommuncationPreference` object. If `mock_mail` is set to true, the email is sent to the `from_email` to allow for testing. """ if communication_type is not None: recipient_list = [ email for email in recipient_list if _can_email(communication_type, email) ] if settings.ORCHESTRA_MOCK_EMAILS: # Send the mail to as many people as we normally would recipient_list = [settings.ORCHESTRA_MOCK_TO_EMAIL ] * len(recipient_list) if len(recipient_list): return _send_mail(subject, message, from_email, recipient_list, fail_silently=fail_silently, auth_user=auth_user, auth_password=auth_password, connection=connection, html_message=html_message)
def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None, connection=None, html_message=None, communication_type=None): """ Light wrapper over Django's send_mail which filters out recipients who opted out of using email via their `CommuncationPreference` object. If `mock_mail` is set to true, the email is sent to the `from_email` to allow for testing. """ if communication_type is not None: recipient_list = [ email for email in recipient_list if _can_email(communication_type, email) ] if settings.ORCHESTRA_MOCK_EMAILS: # Send the mail to as many people as we normally would recipient_list = [ settings.ORCHESTRA_MOCK_TO_EMAIL] * len(recipient_list) if len(recipient_list): return _send_mail(subject, message, from_email, recipient_list, fail_silently=fail_silently, auth_user=auth_user, auth_password=auth_password, connection=connection, html_message=html_message)
def send_mail(email_name, subject, recipients, request=None, **kwargs): """ Send email. Wraps the default Django send_mail() function. Creates email bodies from templates. Can produce both text and HTML email bodies and send a multipart message. """ # Make recipients iterable if it is not already (allow caller to pass a # single recipient, or a list. if isinstance(recipients, str): recipients = (recipients, ) # A text template is required, if we can't load it, fail. try: text_template = select_template([ 'main/email/{}.txt'.format(email_name), 'main/email/{}.text'.format(email_name), ]) except TemplateDoesNotExist: raise ValueError('No template for email: %s' % email_name) # An HTML template is optional. try: html_template = get_template('main/email/{}.html'.format(email_name)) except TemplateDoesNotExist: html_template = None # Produce our message body(s) from our templates using supplied context # (if any). message = text_template.render(context=kwargs, request=request) if html_template: html_message = html_template.render(context=kwargs, request=request) else: html_message = None # Build the from address. email_from = '%s <%s>' % settings.EMAIL_FROM # Send the email using the Django send_mail() function. _send_mail(subject, message, email_from, recipients, html_message=html_message)
def send_mail(subject, message, from_email, recipient_list, fail_silently=False): """ Wrapper that forces sending mail through our connection. """ return _send_mail( subject, message, from_email, recipient_list, connection=get_connection(fail_silently=fail_silently), )
def send_mail(subject, message, to_list): ''' Send email via django's mail function. ''' sender = settings.EMAIL_HOST_USER if type(to_list) is not list: to_list = [to_list] return _send_mail(subject, message, sender, to_list)
def send_mail(product, to_list): # content = 'Type: ' + product.type + '\n' content = '' content += 'Item: ' + product.name + '\n' content += 'Sku: ' + product.uuid + '\n' content += 'Current Price: ' + str(product.current_price) + '\n' content += 'Reason: Price Drop\n' content += 'Item Link: ' + product.url + '\n' content += 'Amazon Link: ' \ + 'http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=' \ + product.name _send_mail(product.name + '\'s price been updated', content, '*****@*****.**', to_list, fail_silently=False) print content print to_list
def send(recipient_list, template_subject, template_message, context): # TODO add i18n templates for users prefered language # TODO use async mail queue site = context["site"] = Site.objects.get_current() sender = settings.DEFAULT_FROM_EMAIL subject = render_to_string(template_subject, context) # render subject subject = u" ".join(subject.splitlines()).strip() # remove newlines subject = u"[{site}] {subject}".format(site=site.name, subject=subject) # add prefix message = render_to_string(template_message, context).strip() return _send_mail(subject, message, sender, recipient_list)
def send_mail(recipient_list, template_subject, template_message, context): # TODO add i18n templates for users prefered language # TODO use async mail queue site = context["site"] = Site.objects.get_current() sender = settings.DEFAULT_FROM_EMAIL subject = render_to_string(template_subject, context) # render subject subject = u" ".join(subject.splitlines()).strip() # remove newlines subject = u"[{site}] {subject}".format(site=site.name, subject=subject) # add prefix message = render_to_string(template_message, context).strip() return _send_mail(subject, message, sender, recipient_list)
def send_mail(subject, body, to_emails, from_email=settings.EMAIL_FROM): text_content = strip_tags(body) if _send_mail( subject, text_content, from_email, to_emails, html_message=body, fail_silently=False, ): return True return False
def send_mail(to, email_template, request=None, context=None): from userflow import conf if to == conf.USERS_DUMMY_EMAIL: return template_context = { 'site_url': conf.get_site_url(request), 'site_name': conf.get_site_name(request), } template_context.update(context or {}) template = lambda frmt: frmt.format(email_template) render = lambda frmt: render_to_string(template(frmt), template_context.copy()) _send_mail(render('userflow/emails/{}-subject.txt').strip(), render('userflow/emails/{}.txt'), settings.SERVER_EMAIL, [to], fail_silently=False, html_message=render('userflow/emails/{}.html'))
def send_mail(subject, message, recipient_list, **kwargs): auth_user, auth_password = choice(settings.EMAIL_POOL) loginfo(p='Send with ' + auth_user, label='send_mail') try: flag = _send_mail( subject, message, auth_user, recipient_list, auth_user=auth_user, auth_password=auth_password, **kwargs) except Exception as e: logger.error(e) flag = False return flag
def promote_data_scientist(user: _User, source: str) -> _User: """ Promote a user to DataScientist role and change it's role on Email Marketing. Will not fail in case API call fails. Email welcome email is sent to user :param source: source of traffic :param user: :return: """ _core_facade.promote_to_data_scientist(user, source) sync_user_on_discourse(user) try: _email_marketing_facade.create_or_update_data_scientist( user.first_name, user.email, id=user.id) except _ActiveCampaignError: pass email_msg = render_to_string('checkout/data_scientist_email.txt', { 'user': user, }) _send_mail( 'Inscrição no Ciência de Dados realizada! Confira o link com detalhes.', email_msg, _settings.DEFAULT_FROM_EMAIL, [user.email]) return user
def send_mail(subject, message, recipient_list, **kwargs): auth_user, auth_password = choice(settings.EMAIL_POOL) loginfo(p='Send with ' + auth_user, label='send_mail') try: flag = _send_mail(subject, message, auth_user, recipient_list, auth_user=auth_user, auth_password=auth_password, **kwargs) except Exception as e: logger.error(e) flag = False return flag
def send_mail(to, email_template, request=None, context=None): from userflow import conf if to == conf.USERS_DUMMY_EMAIL: return template_context = { 'site_url': conf.get_site_url(request), 'site_name': conf.get_site_name(request), } template_context.update(context or {}) template = lambda frmt: frmt.format(email_template) render = lambda frmt: render_to_string(template(frmt), template_context.copy()) _send_mail( render('userflow/emails/{}-subject.txt').strip(), render('userflow/emails/{}.txt'), settings.SERVER_EMAIL, [to], fail_silently=False, html_message=render('userflow/emails/{}.html') )
def send_email(to_addresses, subject, messages): """ Basic function to send email according to the four required string inputs. Let Django send the message; it takes care of opening and closing the connection, as well as locking for thread safety. If ``messages`` is a list and ``to_addresses`` is a list and both are of the same length, then it uses Django's mass emailing function, where the subject is re-used for all messages. """ from_address = email_from to_list = [] if from_address is None: from_address = settings.SERVER_EMAIL if isinstance(to_addresses, list) and isinstance(messages, list): if len(to_addresses) == len(messages): data = [] for idx, message in enumerate(messages): if settings.DEBUG: data.append((subject, message, from_address, [ '*****@*****.**', ])) to_list.append('*****@*****.**') else: data.append((subject, message, from_address, [ to_addresses[idx], ])) to_list.append(to_addresses[idx]) use_mass_email = True else: use_mass_email = False if settings.DEBUG: logger.debug('Overwriting the email: sending to @example.com.') # Overwrite sender address in debug mode to_addresses = [ '*****@*****.**', ] to_list.append('*****@*****.**') out = None if use_mass_email: try: out = send_mass_mail(tuple(data), fail_silently=False) except Exception as e: logger.error( ('An error occurred when sending mass emails [%s]' % str(e))) else: if subject and messages and from_address: try: out = _send_mail(subject, messages, from_address, to_addresses, fail_silently=False) except Exception as e: logger.error(('An error occurred when sending email to %s, ' 'with subject [%s]. Error = %s') % (str(to_addresses), subject, str(e))) return out, to_list
def send_mail(*args, **kwargs): return _send_mail(*args, **kwargs)
if settings.DEBUG or settings.TESTING: # Overwrite sender address in debug mode to_addresses = ['*****@*****.**',] to_list.append('*****@*****.**') out = None if use_mass_email: try: out = send_mass_mail(tuple(data), fail_silently=False) except Exception, e: logger.error(('An error occurred when sending mass emails [%s]' % str(e))) else: if subject and messages and from_address: try: out = _send_mail(subject, messages, from_address, to_addresses, fail_silently=False) except Exception, e: logger.error(('An error occurred when sending email to %s, ' 'with subject [%s]. Error = %s') % ( str(to_addresses), subject, str(e))) return out, to_list def generate_random_token(token_length=16, base_address='', easy_use=False): import random if easy_use: # use characters from a restricted range, that are easy to write and read # unambiguously
def send_mail(subject, msg, _from, to): msg = textwrap.fill(msg, 70) return _send_mail(subject, msg, _from, to)
def send_mail(*args, **kwargs): del kwargs["priority"] return _send_mail(*args, **kwargs)