Email Sending in Python

def test_mail(self,cr,uid,ids,context=None):

        model_obj=self.pool.get('hr.employee.personal_info')
        model_ids=model_obj.search(cr,uid,[('active','=',True)])
        current_day=datetime.now().date()
        # print current_day.month,current_day.day
        for record in model_obj.browse(cr,uid,model_ids):
            if record.birthday:
                till_datetime = datetime.strptime(record.birthday,"%Y-%m-%d").date()
                # print till_datetime.month,till_datetime.day               
                if till_datetime.month==current_day.month and till_datetime.day==current_day.day:
                    print record.name
                    email_ids=self.search(cr,uid,[('id','=',ids[0])])
                    sender = 'skshivammahajan@gmail.com'
                    receivers = 'shivam.mahajan@hytechpro.com'
                    SUBJECT="Wish You Happy Birthday :%s :%s"%(record.name,record.birthday)
                   
                    # message = """Test Mail"""
                    # TEXT = "<h1>Happy Birthday To You !</h1>"
                    # TEXT=MIMEText(TEXT, 'html')
                    # message = 'Subject: %s\n\n%s' % (SUBJECT, TEXT)
                    # smtpObj = smtplib.SMTP(host='smtp.gmail.com', port=587)
                    # smtpObj.ehlo()
                    # smtpObj.starttls()
                    # smtpObj.ehlo()
                    # smtpObj.login(user="skshivammahajan@gmail.com", password="**********")
                    # smtpObj.sendmail(sender, receivers, message)        
                    # print "Successfully sent email"


                    msg = MIMEMultipart()
                    msg['From'] = sender
                    msg['To'] = receivers
                    msg['Subject'] = SUBJECT
                    body="""<html><head></head>request
                                                <body>
                        <p>
                          <b>
                           <a href="http://127.0.0.1:8000/">Verify Email Address</a></b>
                        </p>
                      </body>
                    </html>
                    """
                    msg.attach(MIMEText(body, 'html'))
                    server = smtplib.SMTP('smtp.gmail.com', 587)
                    server.starttls()
                    server.login(sender, "*********")
                    text = msg.as_string()
                    server.sendmail(sender, receivers, text)
        return True

1 comments:

Anna Schafer said...

This is really a nice and informative, containing all information and also has a great impact on the new technology. Thanks for sharing it, hotmail sign in

Copyright © 2013 SoftKul