Friday, July 23, 2021

Send email using SMTP (with attachments); Python

      

import smtplib
#import urllib2
import wget
import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.mime.base import MIMEBase 
from email import encoders
import time 

   cc=['user1@gmail.com''user2@gmail.com','user3@gmail.com']

   toaddr=['user1@gmail.com''user2@gmail.com','user3@gmail.com']
   fromaddr = "vijaysadhu@hotmail.com"
       
       msg = MIMEMultipart('alternative'
  
       # storing the senders email address   
       msg['From'] = fromaddr 
  
       # storing the receivers email address  
       msg['To'] = ','.join(toaddr
      
       msg['Cc'] = ','.join(cc)
       # storing the subject  
       msg['Subject'] = "Incomplete submission; PROJECTZ Express"
       
       lineFailed="<table> "
       c.execute("select line_number,to_char(accounting_date, 'mm/dd/yyyy'),period_name,segment1,segment3,entered_dr,entered_cr from  PROZ.PROZ_BALANCES_CONVERSION")
       rowsLeftout = c.fetchall()
       lineFailed=lineFailed+" <tr>    <th>Line Number</th><th>Accounting Date</th><th>Period Name</th><th>Segment1</th><th>Segment3</th><th>Entered_Dr</th><th>Entered_Cr</th></tr>"
       for rowL in rowsLeftout:
          
          lineFailed=lineFailed"<tr>    <td>"+str(rowL[0])+"</td><td>"+str(rowL[1])+"</td><td>"+str(rowL[2])+"</td><td>"+str(rowL[3])+"</td><td>"+str(rowL[4])+"</td><td>"+str(rowL[5])+"</td><td>"+str(rowL[6])+"</td></tr>"
       lineFailed=lineFailed+"</table> "
       print lineFailed
       # string to store the body of the mail 
       body =  """<!-- #######  THIS IS A COMMENT - Visible only in the source editor #########-->  
                                            <p>The <strong> """+str(row[1])+""" submission</strong> for period <strong>"""+str(row[0])+"""</strong> PROJECTZ detected exception for MRC <strong>"""+str(row[3])+"""</strong>.&nbsp; </p>  
                                            """+lineFailed+"""
                                            <p>Thank you,</p>  
                                            <p>PROJECTZ Support</p>"""
  
       # attach the body with the msg instance 
       msg.attach(MIMEText(body'html')) 
  
       # open the file to be sent  
       filename = f
       attachment = open("/PROJECTZ/jython2.7.0/bin/PHC/"+f, "rb"
  
       # instance of MIMEBase and named as p 
       p = MIMEBase('application''octet-stream')
  
       # To change the payload into encoded form 
       p.set_payload((attachment).read()) 
  
       # encode into base64 
       encoders.encode_base64(p
   
       p.add_header('Content-Disposition'"attachment; filename= %s" % filename
  
       # attach the instance 'p' to instance 'msg' 
       msg.attach(p
  
       # creates SMTP session 
       s = smtplib.SMTP('smtp.em.ibcb.com'25)
  
       # start TLS for security 
       #s.starttls() 
  
       # Authentication 
       #s.login(fromaddr, "Password_of_the_sender") 
  
       # Converts the Multipart msg into a string 
       text = msg.as_string() 
  
       # sending the mail 
       s.sendmail(fromaddrtoaddr+cctext)
  
       # terminating the session 
       s.quit() 

No comments: