def send_mass_html_mail(datatuple, fail_silently=False, user=None, password=None, connection=None): """ Given a datatuple of (subject, text_content, html_content, from_email, recipient_list), sends each message to each recipient list. Returns the number of emails sent. If from_email is None, the DEFAULT_FROM_EMAIL setting is used. If auth_user and auth_password are set, they're used to log in. If auth_user is None, the EMAIL_HOST_USER setting is used. If auth_password is None, the EMAIL_HOST_PASSWORD setting is used. """ connection = connection or get_connection( username=user, password=password, fail_silently=fail_silently) messages = [] for subject, text, html, from_email, recipient in datatuple: message = EmailMultiAlternatives(subject, text, from_email, recipient, headers=headers) html_content = prepare_html(html, 'marketing/funcs/Sapir.json', '1') new_html_email_text = adapt_html(html_content, extra_metadata={"customer_id": 1}, click_tracking=True, open_tracking=True) message.attach_alternative(new_html_email_text, 'text/html') messages.append(message) return connection.send_messages(messages)
def test_adapt_html_full(): new_html = tracking_html.adapt_html( TEST_HTML_EMAIL, DEFAULT_METADATA, **DEFAULT_SETTINGS) tree = html.fromstring(new_html) _test_open_tracking(tree) _test_click_tracking(tree)
def test_adapt_html_full(): new_html = tracking_html.adapt_html(TEST_HTML_EMAIL, DEFAULT_METADATA, **DEFAULT_SETTINGS) tree = html.fromstring(new_html) _test_open_tracking(tree) _test_click_tracking(tree)
def test_adapt_html_click_only(): new_html = tracking_html.adapt_html( TEST_HTML_EMAIL, DEFAULT_METADATA, open_tracking=False, **DEFAULT_SETTINGS) tree = html.fromstring(new_html) assert len(tree.xpath("//img")) == 1 _test_click_tracking(tree)
def test_adapt_html_open_only(): new_html = tracking_html.adapt_html( TEST_HTML_EMAIL, DEFAULT_METADATA, click_tracking=False, **DEFAULT_SETTINGS) tree = html.fromstring(new_html) _test_open_tracking(tree) links = tree.xpath("//a") assert links[0].attrib["href"] ==\ "http://www.example.com/foo/?question=response" assert links[1].attrib["href"] == "mailto:[email protected]" assert links[2].attrib["href"] == "http://www.domain2.com"
def add_tracking_info(email: post_office_models.Email, click_tracking: bool = False, open_tracking: bool = False) -> post_office_models.Email: msg_id = email.headers.get('Message-ID', None) # TODO: add expectation html_message = adapt_html( '<html><body>%s</body></html>' % email.html_message, extra_metadata={ 'tenant': db_connection.tenant.id, 'id': msg_id, }, click_tracking=click_tracking, open_tracking=open_tracking, configuration=get_configuration_from_settings(), ) email.html_message = html_message if email.id: email.save() return email
print(webhook) while True: line = input() if line: emailCont.append(line) else: break htmlBody = "" for line in emailCont: htmlBody = htmlBody + "<p>" + line + "</p> " htmlBody = htmlBody + webhook root = etree.HTML(htmlBody) htmlText = etree.tostring(root) pytrackHtml = adapt_html(htmlText, extra_metadata={"customer_id": 1}, click_tracking=True, open_tracking=True) emailBod = MIMEText(pytrackHtml, 'html') open_tracking_url = pytracking.get_open_tracking_url( {"customer_id": 1}, configuration=configuration) tracking_result = pytracking.get_open_tracking_result( open_tracking_url, base_open_tracking_url="http://mehltrej.pythonanywhere.com/") msg.attach(emailBod) #Send email try: print("Attempting to connect") smtpObj = smtplib.SMTP(emailHost, emailPort) print("Successful connection")
def send_ab_email(html, email_address, list_id, segment_id, template_id, ab_campaign_id, subject): email_result_obj = email_result.EmailResultHelper() ready_to_send_emails_helper_obj = ready_to_send_email_helper.ReadyToSendEmailsHelper( ) list_segment_id = segment_id try: email_address = email_address logging.warning('[celery email Address] %s' % email_address) email_tracking_obj = EmailTracking() open_url = email_tracking_obj.email_encoding(template_id, ab_campaign_id, segment_id) configuration = pytracking.Configuration( base_open_tracking_url=EMAIL_OPEN_URL, base_click_tracking_url=EMAIL_CLICK_URL, include_webhook_url=False) html = adapt_html(html, extra_metadata={ "template_id": template_id, "segment_id": segment_id, "ab_campaign_id": ab_campaign_id, "campaign_id": None }, click_tracking=True, open_tracking=True, configuration=configuration) unsubscribe_url = "%s/%s/%s/" % (EMAIL_UNSUBSCRIBE, segment_id, ab_campaign_id) html = html.replace("EMAIL_UNSUBSCRIBE_URL", unsubscribe_url) if USE_AWS_EMAIL_SERVER: mail_obj = Email(email_address, subject, html) mail_obj.send() # else: # # set up the SMTP server # smtp_email_obj = SmtpEmail(email_address, subject, html) # smtp_email_obj.send() logging.warning('mail is sent successfully') email_result_obj.create_email_result(list_id, list_segment_id, template_id, "SENT", "SUCCESS", campaign_id=None, ab_campaign_id=ab_campaign_id) ready_to_send_emails_helper_obj.update_ab_ready_to_send_status( ab_campaign_id, email_address, "SENT") # my_sql_cursor.commit() logging.warning('Mail is sent successfully') except Exception as e: # my_sql_cursor.rollback() try: email_result_obj.create_ab_email_result( list_id, list_segment_id, template_id, "ERROR", "ERROR", campaign_id=None, ab_campaign_id=ab_campaign_id) ready_to_send_emails_helper_obj. \ update_ready_to_send_status(ab_campaign_id, email_address, "ERROR") # my_sql_cursor.commit() except: pass logging.warning('[Tasks] :: send_email() :: Got exception: %s' % e) logging.warning(traceback.format_exc())