sending mail with Python : gather the mail-data from a spreadsheet;

hi there good day dear experts,

sending mail with Python : gather the mail-data from a spreadsheet

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 = "" # the email where you sent the email
password = "my_Password"
send_to_email = "" # 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("", 465)
server.login(email, password)
text = msg.as_string()
server.sendmail(email, send_to_email, text)

....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('', 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()

  • 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...')

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 

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:


mport 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([x]) ):
    del msg['To']
    msg['To'] = row[x]
      server.sendmail('', [row[x]], msg.as_string())
    except SMTPException:
      print "An error occured."

well - what do you day - is this a good method to do so"?

look forward to hear from you

@apollodriver, I think you have posted this in the wrong Forum by mistake - this is the Arduino Forum, not a Python Forum.


hello dear Robin
many many thanks for the quick response - glad to hear from you
well - i guess youre right. But i like Arduino so much .- and i am a big big admirer of this forum. -it is so great

but - yes. youre right

i need to have a closer look where i can post these ideas.

have a great day