首页 > 解决方案 > Sending a mass-email with Python using data from several .txt files

问题描述

I am currently writing a small program to ease my workload sending a couple hundred of emails daily. I have edited code I found somewhere and came up with this so far:

import smtplib
from email.mime.text import MIMEText
from time import sleep
from random import choice
import re
import io
user_email = input('Enter your E-mail: ')

user_password = input('Enter your Password: ')


try:
        s = smtplib.SMTP_SSL('smtp.gmail.com', 587)

        # re-identify ourselves as an encrypted connection
        s.ehlo()
        # If using TLS, uncomment the line below.
        #s.starttls()

    s.login(user_email, user_password)
    s.set_debuglevel(1)
except IOError:
        print (IOError)

SUBJECT = open('1subject.txt', 'r')
variation0 = [SUBJECT.read(), SUBJECT.read(), SUBJECT.read()]
SUBJECT.close()

GREETING = open('2greeting.txt', 'r')

variation1 = [GREETING.read(), GREETING.read(), GREETING.read()]
GREETING.close()

BODY = open('3body.txt', 'r')
variation2 = [BODY.read(), BODY.read(), BODY.read()]
BODY.close()

OUTRO = open('4outro.txt', 'r')
variation3 = [OUTRO.read(), OUTRO.read(), OUTRO.read()]
OUTRO.close()

# Where the {}'s are, is where a variation(0-3) will be substituted in. 
template = """Insert your {}
Multiline email body
HERE {} {}
-transposed messenger
"""


sender = 'sender@email.com'
recipients = []
names = []


mailTxt = open("listOfRecipients.txt", 'r')

for line in mailTxt:
        line = line.replace('\n',"")
        names.append(re.split(':', line)[0])
        recipients.append(re.split(':',line)[1])



for k in range(len(recipients)):
        msg = MIMEText(template.format(choice(variation1), names[k]+",",  recipients[k], choice(variation2), choice(variation3)))
        msg['Subject'] = choice(variation0)
        msg['From'] = sender
        msg['To'] = recipients[k]
        print ("Sending...")
        print (msg)


        try:
                s = smtplib.SMTP_SSL('smtp.gmail.com', 587)
                s.sendmail(sender, recipients[k], msg.as_string())
        except Exception as e:
                str(e)
                print ((e, "error: logging in and cont with nxt address..."))

                s = smtplib.SMTP_SSL('smtp.gmail.com', 587)
                s.ehlo()
                s.login(user_email, user_password)
                s.set_debuglevel(1)
                continue

        with io.open('log.txt', 'a', encoding='utf-8') as f:
          try:
                f.write(unicode(msg))

          except:
                  f.write("Error handling ASCII encoded names: "+ Unicode
                  (recipients[k]))


print ("Messages have been sent.")
f.close()
s.quit()

This is the first time I'm adding code on here so I don't know if I've done that right. However whatever I do I keep receiving errors. The files that are being opened are files containing email addresses and names (like mail@email.com:mailman Johnson and then a new line continuing like this) the other files are containing text for the email that is sent.

my ridiculous question is: how do I get this to work? What am I doing wrong.

My current error is

ssl.SSLError: [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:847)

Again, my apologies if this isn't a real question!

标签: pythonsmtpmass-emails

解决方案


推荐阅读