Esempio n. 1
0
def notify_participants(recipients_f, email_settings_f, dry_run=True):
    """Sends an email to each participant in the study.

    Arguments:
        recipients_f - file containing email recipients (see
            parse.parse_recipients for more details)
        email_settings_f - file containing settings for sending emails (see
            parse.parse_email_settings for more details)
        dry_run - if True, no emails are sent and information of what would
            have been done is printed to stdout. If False, no output is printed
            and emails are sent
    """
    recipients = parse_recipients(recipients_f)
    email_settings = parse_email_settings(email_settings_f)

    sender = email_settings['sender']
    email_password = email_settings['password']
    server = email_settings['smtp_server']
    port = email_settings['smtp_port']

    if dry_run:
        num_recipients = len(recipients)

        print("Running script in dry-run mode. No emails will be sent. Here's "
              "what I would have done:\n")
        print("Sender information:\n\nFrom address: %s\nPassword: %s\nSMTP "
              "server: %s\nPort: %s\n" % (sender, email_password, server,
                                          port))
        print "Sending emails to %d recipient(s)." % num_recipients

        if num_recipients > 0:
            # Sort so that we will grab the same recipient each time this is
            # run over the same input files.
            sample_recipient = sorted(recipients.items())[0]
            personal_id = sample_recipient[0]
            password, addresses = sample_recipient[1]

            print "\nSample email:\n"
            print "To: %s" % ', '.join(addresses)
            print "From: %s" % sender
            print "Subject: %s" % notification_email_subject
            print "Body:\n%s\n" % get_personalized_notification_email_text(
                    personal_id, password)
    else:
        for personal_id, (password, addresses) in recipients.items():
            personalized_text = \
                    get_personalized_notification_email_text(personal_id,
                                                             password)
            print "Sending email to %s (%s)... " % (personal_id,
                                                    ', '.join(addresses)),
            send_email(server, port, sender, email_password, addresses,
                       notification_email_subject, personalized_text)
            print "success!"
Esempio n. 2
0
 def test_parse_email_settings_standard(self):
     """Test parsing a standard email settings file."""
     exp = {'smtp_server': 'some.smtp.server', 'smtp_port': '42',
             'sender': '*****@*****.**', 'password': '******'}
     obs = parse_email_settings(self.email_settings1)
     self.assertEqual(obs, exp)