python script to fetch gmail ids and script to send out mail with attachment


-----------------------------------------------------------------------------------------------------------------
script to fetch gmail ids
------------------------------------------------------------------------------------------------------------------
import imaplib, sys, email

mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('gmail@gmail.com', 'password')
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.


result, data = mail.search(None, '(HEADER From "info@monsterindia.com")')

for num in reversed(data[0].split()):
    rv, data = mail.fetch(num, '(RFC822)')
    raw_email = data[0][1] # here's the body, which is raw text of the whole email
# including headers and alternate payloads

    email_message = email.message_from_string(raw_email)
    if email_message['Reply-To'] in ('naukrialerts@naukri.com', None):
        continue
    print email_message['Reply-To']

-----------------------------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------------------------------
script to send out mail with attachment 
----------------------------------------------------------------------------------------------------------------
#!/usr/bin/python

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os,re

gmail_user = "some@gmail.com"
gmail_pwd = "passowrd"

def mail(to, subject, attach, recruitername ):
   msg = MIMEMultipart()

   msg['From'] = gmail_user
   msg['To'] = to
   msg['Subject'] = subject
   text = """
Hi  %s

balah 
balah 
balah 

balah 

Regards 

balah 
balah 

""" %  recruitername

   msg.attach(MIMEText(text))

   part = MIMEBase('application', 'octet-stream')
   part.set_payload(open(attach, 'rb').read())
   Encoders.encode_base64(part)
   part.add_header('Content-Disposition',
           'attachment; filename="%s"' % os.path.basename(attach))
   msg.attach(part)

   mailServer = smtplib.SMTP("smtp.gmail.com", 587)
   mailServer.ehlo()
   mailServer.starttls()
   mailServer.ehlo()
   mailServer.login(gmail_user, gmail_pwd)
   mailServer.sendmail(gmail_user, to, msg.as_string())
   # Should be mailServer.quit(), but that crashes...
   mailServer.close()


myemailfile = "/scripts/cyber_send"
emaillist =  open( myemailfile, 'r')


for i in emaillist.readlines():
    match = re.search(r'[\w\.-]+@[\w\.-]+', i)
    if not  match:
        continue
    print "%s " % match.group(0)
    hrname =  str(re.split("\.|_|-", match.group(0).split('@')[0] )[0])
    #hrname = ''
    mail( match.group(0) , "Sr Engineer ( information technology )",
   "/scripts/resume_.pdf" , hrname )
-------------------------------------------------------------------------------------------------------------------

No comments:

Other Articles

Enter your email address: