A way to send 1 email once done reading from CSV - python

I wrote a script to send an email if the values match a certain criteria. I'm wanting to send 1 email instead of multiple emails upon every check. I thought I can mitigate by throwing in another function but I can't figure out how to do it. Any ideas on how to accomplish this?
import csv, requests, xmltodict, smtplib, email.utils
from email.mime.text import MIMEText
def sendEmail(host, value, devicename):
# Create the message
msg = MIMEText('This is the body of the message.')
msg['To'] = email.utils.formataddr(('Recipient', 'XXXXXX'))
msg['From'] = email.utils.formataddr(('Author', 'XXXXX'))
msg['Subject'] = 'Simple test message'
server = smtplib.SMTP('XXXXXXX')
server.set_debuglevel(True) # show communication with the server
try:
server.sendmail('XXXXXX', ['XXXXXX'], msg.as_string())
finally:
server.quit()
def check(hostIP, value):
xml = """<?xml version="1.0" encoding="iso-8859-1"?>"""
headers = {'Content-Type': 'application/xml'}
response = requests.post('http://' + hostIP + '/RPC2', data=xml, headers=headers).text
doc = xmltodict.parse(response)
uptime = str(doc['response'])
maxtime = '300'
time = str(uptimeValue)
day = time // (24 * 3600)
if day >= maxtime:
print 'it is'
sendEmail(str(hostIP), str(value), str(devicename))
else:
print "it is not!"
def main():
try:
with open('list.csv', 'r') as file:
reader = csv.DictReader(file)
for row in reader:
check(row['Host'], row['Value'])
except Exception as error:
print ValueError("Could not properly read the csv file")
sys.exit(0)
if __name__ == "__main__":
main()

Replace
def sendEmail(message):
and
if day >= maxtime:
print 'it is'
return (str(hostIP), str(value), str(devicename))
and
device_list = []
for row in reader:
result = check(row['Host'], row['Value'])
if result:
device_list.append(', '.join(result))
sendEmail('\n'.join(device_list))

Instead of using check to send the email, use it to add an item to go into the message body, probably in an array or dictionary. Then, after you are done processing your file, you can use that collected information to build the body of your email to give to a single call to sendEmail.
That means you don't call sendEmail from inside of check; you call it from main once you are done with the file.

Related

How to get emails received from gmail python?

I want to get the last 10 received gmails with python.
Currently I have this code but it only returns a limited number of emails and it manipulates pop3 directly, which makes it unnecessary long.
Source of the code: https://www.code-learner.com/python-use-pop3-to-read-email-example/
import poplib
import smtplib, ssl
def guess_charset(msg):
# get charset from message object.
charset = msg.get_charset()
# if can not get charset
if charset is None:
# get message header content-type value and retrieve the charset from the value.
content_type = msg.get('Content-Type', '').lower()
pos = content_type.find('charset=')
if pos >= 0:
charset = content_type[pos + 8:].strip()
return charset
def decode_str(s):
value, charset = decode_header(s)[0]
if charset:
value = value.decode(charset)
return value
# variable indent_number is used to decide number of indent of each level in the mail multiple bory part.
def print_info(msg, indent_number=0):
if indent_number == 0:
# loop to retrieve from, to, subject from email header.
for header in ['From', 'To', 'Subject']:
# get header value
value = msg.get(header, '')
if value:
# for subject header.
if header=='Subject':
# decode the subject value
value = decode_str(value)
# for from and to header.
else:
# parse email address
hdr, addr = parseaddr(value)
# decode the name value.
name = decode_str(hdr)
value = u'%s <%s>' % (name, addr)
print('%s%s: %s' % (' ' * indent_number, header, value))
# if message has multiple part.
if (msg.is_multipart()):
# get multiple parts from message body.
parts = msg.get_payload()
# loop for each part
for n, part in enumerate(parts):
print('%spart %s' % (' ' * indent_number, n))
print('%s--------------------' % (' ' * indent_number))
# print multiple part information by invoke print_info function recursively.
print_info(part, indent_number + 1)
# if not multiple part.
else:
# get message content mime type
content_type = msg.get_content_type()
# if plain text or html content type.
if content_type=='text/plain' or content_type=='text/html':
# get email content
content = msg.get_payload(decode=True)
# get content string charset
charset = guess_charset(msg)
# decode the content with charset if provided.
if charset:
content = content.decode(charset)
print('%sText: %s' % (' ' * indent_number, content + '...'))
else:
print('%sAttachment: %s' % (' ' * indent_number, content_type))
# input email address, password and pop3 server domain or ip address
email = 'yourgmail#gmail.com'
password = 'yourpassword'
# connect to pop3 server:
server = poplib.POP3_SSL('pop.gmail.com')
# open debug switch to print debug information between client and pop3 server.
server.set_debuglevel(1)
# get pop3 server welcome message.
pop3_server_welcome_msg = server.getwelcome().decode('utf-8')
# print out the pop3 server welcome message.
print(server.getwelcome().decode('utf-8'))
# user account authentication
server.user(email)
server.pass_(password)
# stat() function return email count and occupied disk size
print('Messages: %s. Size: %s' % server.stat())
# list() function return all email list
resp, mails, octets = server.list()
print(mails)
# retrieve the newest email index number
#index = len(mails)
index = 3
# server.retr function can get the contents of the email with index variable value index number.
resp, lines, octets = server.retr(index)
# lines stores each line of the original text of the message
# so that you can get the original text of the entire message use the join function and lines variable.
msg_content = b'\r\n'.join(lines).decode('utf-8')
# now parse out the email object.
from email.parser import Parser
from email.header import decode_header
from email.utils import parseaddr
import poplib
# parse the email content to a message object.
msg = Parser().parsestr(msg_content)
print(len(msg_content))
# get email from, to, subject attribute value.
email_from = msg.get('From')
email_to = msg.get('To')
email_subject = msg.get('Subject')
print('From ' + email_from)
print('To ' + email_to)
print('Subject ' + email_subject)
for part in msg.walk():
if part.get_content_type():
body = part.get_payload(decode=True)
print_info(msg, len(msg))
# delete the email from pop3 server directly by email index.
# server.dele(index)
# close pop3 server connection.
server.quit()
I also tried this code but it didn't work:
import imaplib, email, base64
def fetch_messages(username, password):
messages = []
conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
conn.login(username, password)
conn.select()
typ, data = conn.uid('search', None, 'ALL')
for num in data[0].split():
typ, msg_data = conn.uid('fetch', num, '(RFC822)')
for response_part in msg_data:
if isinstance(response_part, tuple):
messages.append(email.message_from_string(response_part[1]))
typ, response = conn.store(num, '+FLAGS', r'(\Seen)')
return messages
and this also didn't work for me...
import poplib
from email import parser
pop_conn = poplib.POP3_SSL('pop.gmail.com')
pop_conn.user('#gmail.com')
pop_conn.pass_('password')
messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
# Concat message pieces:
messages = ["\n".join(mssg[1]) for mssg in messages]
#Parse message intom an email object:
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
for message in messages:
print(message['subject'])
print(message['body'])
I managed to solve it, the only issue is that it marks as read every unread email, here is the code I used:
import imaplib
mail = imaplib.IMAP4_SSL('imap.gmail.com')
email = input('Email: ')
password = input('Password: ')
mail.login(email+'#gmail.com', password)
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.
result, data = mail.search(None, "ALL")
ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string
latest_email_id = id_list[-1] # get the latest
# fetch the email body (RFC822) for the given ID
result, data = mail.fetch(latest_email_id, "(RFC822)")
raw_email = data[0][1] # here's the body, which is raw text of the whole email
# including headers and alternate payloads
import email
email_message = email.message_from_string(str(raw_email))
print (email_message['To'])
print (email.utils.parseaddr(email_message['From'])) # for parsing "Yuji Tomita" <yuji#grovemade.com>
print (email_message.items()) # print all headers
# note that if you want to get text content (body) and the email contains
# multiple payloads (plaintext/ html), you must parse each message separately.
# use something like the following: (taken from a stackoverflow post)
def get_first_text_block(self, email_message_instance):
maintype = email_message_instance.get_content_maintype()
if maintype == 'multipart':
for part in email_message_instance.get_payload():
if part.get_content_maintype() == 'text':
return part.get_payload()
elif maintype == 'text':
return email_message_instance.get_payload()
https://developers.google.com/gmail/api/quickstart/python is the preferred way:
from gmail.gmail import gmail_auth, ListThreadsMatchingQuery
service = gmail_auth()
threads = ListThreadsMatchingQuery(service, query=query)
where:
def ListThreadsMatchingQuery(service, user_id='me', query=''):
"""List all Threads of the user's mailbox matching the query.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
query: String used to filter messages returned.
Eg.- 'label:UNREAD' for unread messages only.
Returns:
List of threads that match the criteria of the query. Note that the returned
list contains Thread IDs, you must use get with the appropriate
ID to get the details for a Thread.
"""
try:
response = service.users().threads().list(userId=user_id, q=query).execute()
threads = []
if 'threads' in response:
threads.extend(response['threads'])
while 'nextPageToken' in response:
page_token = response['nextPageToken']
response = service.users().threads().list(userId=user_id, q=query,
pageToken=page_token).execute()
threads.extend(response['threads'])
return threads
except errors.HttpError as error:
raise error
You should try easyimap lib to get a list of e-mails, I'm not sure if works with pop3.
Code example:
import easyimap
host = 'imap.gmail.com'
user = 'you#example.com'
password = 'secret'
mailbox = 'INBOX.subfolder'
imapper = easyimap.connect(host, user, password, mailbox)
email_quantity = 10
emails_from_your_mailbox = imapper.listids(limit=email_quantity)
imapper.quit()

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!

python poplib get headers from emails

this is my actula code :
import poplib, sys, re, threading, time, Queue
from email import parser
Mailbox = poplib.POP3(pop3, '110')
Mailbox.user(username)
Mailbox.pass_(password)
numMessages = len(Mailbox.list()[1])
for i in range(numMessages):
fullemail = ''
fullemail = '\n'.join([msg for msg in Mailbox.retr(i+1)[1]])
msg = parser.Parser().parsestr(fullemail)
for part in msg.walk():
print part.get_payload(decode=True)
print part.get_payload(decode=True) print body of email, how i can print header of email ?
the method items() of email.message.Message/email.message.EmailMessage object may be useful:
for t in msg.items():
print(t)
t is a tuple like this (key,value).

email.message_from_string() and imaplib adding '<' '>'

Alright so I load the email in from gmail with imaplib and then when I'm trying to parse the email it does not separate anything out in a usable format. I suspect this is because somewhere in the process '<' or '>' are being added to the raw email.
Here is what the debugger is showing me after I have called the method:
As you can see it hasn't really parsed anything into a usable format.
Here is the code I'm using: (NOTE: the .replace('>', '') seems to have no effect on the end result.)
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('myEmail#gmail.com', 'password')
mail.list()
mail.select('inbox')
typ, data = mail.search(None, 'ALL')
ids = data[0]
id_list = ids.split()
# get the most recent email id
latest_email_id = int( id_list[-1] )
# iterate through 15 messages in descending order starting with latest_email_id
# the '-1' dictates reverse looping order
for i in range( latest_email_id -10, latest_email_id-15, -1 ):
typ, data = mail.fetch( str(i), '(RFC822)' )
for response_part in data:
if isinstance(response_part, tuple):
msg = str(response_part[1]).replace('<', '')
msg = msg.replace('>', '')
msg = email.message_from_string(msg)
#msg = feedparser.parse(response_part[1])
varSubject = msg['subject']
varFrom = msg['from']
python email.message_from_string() parse problems and Parsing email with Python both had very similar and identical problems to me (I think) and they solved it by altering their email, however I'm reading my email straight from Google's servers so I'm not sure exactly what to do to the email to fix it up since removing all '<' and '>' obviously won't work.
So, how do I fix the email that is read from imaplib so that it can be easily read with email.message_from_string()? (Or any other improvements/possible solutions as I'm not 100% certain the '<' and '>' are actually the problem, I'm only guessing based off of those other questions asked.)
Cheers
You shouldn't parse <, > and data between them - it is like parsing HTML, but much more complicated. There are existing solutions to do it.
Here is my code that can read mail with attachments, extract data that can be used for further use and process it to human and code readable format. As you can see, all tasks are being made by third-party modules.
from datetime import datetime
import imaplib
import email
import html2text
from os import path
class MailClient(object):
def __init__(self):
self.m = imaplib.IMAP4_SSL('your.server.com')
self.Login()
def Login(self):
result, data = self.m.login('login#domain.com', 'p#s$w0rd')
if result != 'OK':
raise Exception("Error connecting to mailbox: {}".format(data))
def ReadLatest(self, delete = True):
result, data = self.m.select("inbox")
if result != 'OK':
raise Exception("Error reading inbox: {}".format(data))
if data == ['0']:
return None
latest = data[0].split()[-1]
result, data = self.m.fetch(latest, "(RFC822)")
if result != 'OK':
raise Exception("Error reading email: {}".format(data))
if delete:
self.m.store(latest, '+FLAGS', '\\Deleted')
message = email.message_from_string(data[0][1])
res = {
'From' : email.utils.parseaddr(message['From'])[1],
'From name' : email.utils.parseaddr(message['From'])[0],
'Time' : datetime.fromtimestamp(email.utils.mktime_tz(email.utils.parsedate_tz(message['Date']))),
'To' : message['To'],
'Subject' : email.Header.decode_header(message["Subject"])[0][0],
'Text' : '',
'File' : None
}
for part in message.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get_content_maintype() == 'text':
# reading as HTML (not plain text)
_html = part.get_payload(decode = True)
res['Text'] = html2text.html2text(_html)
elif part.get_content_maintype() == 'application' and part.get_filename():
fname = path.join("your/folder", part.get_filename())
attachment = open(fname, 'wb')
attachment.write(part.get_payload(decode = True))
attachment.close()
if res['File']:
res['File'].append(fname)
else:
res['File'] = [fname]
return res
def __del__(self):
self.m.close()

Reliable way of sending email of a file containing HTML code

I have a HTML code in a text file emailtext.txt # http://pastie.org/8276028 ,currently I am using the following code to send an email(via outlook) in HTML format but the formatting seems messed up sometimes... can someone provide inputs on how to send this email in a reliable way?
from email.mime.text import MIMEText
from subprocess import check_call,Popen,PIPE
def email (body,subject,to=None):
msg = MIMEText("%s" % body)
msg['Content-Type'] = "text/html; charset=UTF8"
msg["From"] = "userid#qualcomm.com"
if to!=None:
to=to.strip()
msg["To"] = to
else:
msg["To"] = "userid2#company.com"
msg["Subject"] = '%s' % subject
p = Popen(["/usr/sbin/sendmail", "-t", "-f" + msg["From"]], stdin=PIPE)
p.communicate(msg.as_string())
print "Done"
def main ():
with open('emailtext.txt', 'r') as f:
body = f.read()
Subject ="test email"
email(body, Subject, "userid3#company.com")
if __name__ == '__main__':
main()
Try using smtplib:
from smtplib import SMTP
s = SMTP('localhost',25)
s.sendmail('Me <me#example.com>', ['You <you#example.com>'],msg=msg.as_string())
If that doesn't work then your HTML is probably bad.

Categories