DEV Community

hub
hub

Posted on

sending mails with Python using a spreadsheet

Top comments (1)

Collapse
 
digital_hub profile image
hub

hi there good day dear experts,

i am currentliy attempting to send an email, but wait - i want to gather the mail-data from a spreadsheet;

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

email = "myname@gmail.com" # the email where you sent the email
password = "my_Password"
send_to_email = "myname@gmail.com" # for whom
subject = "Gmail"
message = "test email - lets meet each other today?!"

msg = MIMEMultipart()
msg["From"] = email
msg["To"] = send_to_email
msg["Subject"] = subject

msg.attach(MIMEText(message, 'plain'))

server = smtplib.SMTP("smtp.gmail.com", 465)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.sendmail(email, send_to_email, text)
server.quit()

....but wait - i want to gather the mail-data from a spreadsheet;

the steps i need to do therefore:

Step 0: importing all the libraries i need for the job
Step 1: choosing the connection ssl or not - this will have consequences for the port.. see below
Step 2: now we want to establish connection with our gmail account using smtplib library - working with port 465
Step 3: in this step we extract the names and email addresses from the spreadsheet: a very important step
Step 4: now we run a loop and for every record send an email.
Step 5: afer all - were done and then we close the smtp server.

in other words:

Creating a SMTP session: well we can use here the following ports: 587 with TLS, 465 SSL and furtermore 25

server = smtplib.SMTP('smtp.gmail.com', 465)

working with encryption of the email - aaaargh thererfore i need to use 587 - and probably not 465 aaaaargh

i have to work with the context = ssl.create_default_context()
server.starttls(context=context)

and then afterwards trying to log in into our Google account

server.login(sender_email, password)

getting contact to the spreadsheet-Extracting all the names and email addresses from the spreadsheet.

Sending email from sender, to receiver with the email body

server.sendmail with the following arguments (sender_email, receiver_email, email_body)
print('Email sent!')

print('Closing the server...')
server.quit()

aaaaaaaaaaaaaaa

important the step 3: Extract the names and email addresses from the spreadsheet.
regarding the getting of the data for the mail: i have a ecxel-sheet with only two coölums the

the format of the mails:
name: mike batt
mail-Adress: mike@yahoo.com

note i have stored the mailadresses in a spreadsheet

can i verify that the email address is syntactically valid, i'll still need to check that
it was not mistyped, and that it actually goes to the person we think it does.
Something like: it has exactly one @ sign, and at least one. in the part after the @:

[^@]+@[^@]+.[^@]+

what about all the damend whitespace -- there are probably valid email addresses with whitespace in them,
but I've never seen one, so the odds of this being a user error are on your side.

i want the full check, so i could use any such regex:

import re :: if not re.match(r"... regex here ...", email): # whatever
by the way Python =3.4 has got a nice re.fullmatch which is preferable to re.match.
If we have a large number of regexes to check, it might be faster to compile the regex first:

import re

EMAIL_REGEX = re.compile(r"... regex here ...")

if not EMAIL_REGEX.match(email):

more checks to validaqte the emails that i have stored in the excel-spreadsheet:

Another option is to use the validate_email package, which actually contacts the SMTP server
to verify that the address exists. This still doesn't guarantee that it belongs to the right person, though.

We'll install 'xlrd' first. 'Xlrd' is a library to extract data from Microsoft Excel (tm) spreadsheet files.
And afterwards we will import 'smtplib'. The smtplib module defines an SMTP client session object that can be
used to send or e-mail to any Internet-machine on the whole world that has got a SMTP or ESMTP listener daemon.
Here we go;

again: if we have a large number of regexes to check, it might be faster to compile the regex first: here we go

email_pattern= re.compile("^.+@.+..+$")
for row in email_data:
if( email_pattern.search(row[1]) ):
del msg['To']
msg['To'] = row[1]
try:
server.sendmail('test@gmail.com', [row[1]], msg.as_string())
except SMTPException:
print "An error occured."
server.quit()