Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
# Function to read the contacts from a given contact file and return a
# list of names and email addresses
def get_contacts(filename):
names = []
emails = []
with open(filename, mode='r', encoding='utf-8') as contacts_file:
for a_contact in contacts_file:
names.append(a_contact.split()[0])
emails.append(a_contact.split()[1])
return names, emails
from string import Template
def read_template(filename):
with open(filename, 'r', encoding='utf-8') as template_file:
template_file_content = template_file.read()
return Template(template_file_content)
# import the smtplib module. It should be included in Python by default
import smtplib
# set up the SMTP server
MY_ADDRESS = 'fortestingonly0071@gmail.com'
PASSWORD = 'fortesting1@'
s = smtplib.SMTP(host='smtp.gmail.com', port=587)
s.starttls()
s.login(MY_ADDRESS, PASSWORD)
names, emails = get_contacts('mycontacts.txt') # read contacts
message_template = read_template('message.txt')
# import necessary packages
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# For each contact, send the email:
for name, email in zip(names, emails):
msg = MIMEMultipart() # create a message
body = """<html>
<body>
<button style="padding:20px;background:green;color:white;">Unsubscribe</button>
</body>
</html>
"""
#msg.attach(MIMEText(body, 'html'))
# add in the actual person name to the message template
message = message_template.substitute(PERSON_NAME=name.title())
# setup the parameters of the message
msg['From']=MY_ADDRESS
msg['To']=email
msg['Subject']="Thank you for subscribing to our mailing service"
# add in the message body
msg.attach(MIMEText(message, 'plain'))
msg.attach(MIMEText(body, 'html'))
#msg.attach(Text,html)
# send the message via the server set up earlier.
s.send_message(msg)
del msg