Sending mail through python script using IITB proxy

1,308 views
Skip to first unread message

Jay Parikh

unread,
Jun 18, 2014, 5:41:22 AM6/18/14
to wncc...@googlegroups.com
Hi All,

I am trying to send mail using python script and IITB network to outside(gmail). But it is not working out.
I have tried several code but didn't work.
 
One of my sample code :

import smtplib
 
to = 'a...@gmail.com'
user = 'username'
pwd = 'pwd'
smtpserver = smtplib.SMTP("smtp-auth.iitb.ac.in",25)
smtpserver.ehlo()
smtpserver.login(user, pwd)
header = 'To:' + to + '\n' + 'From: ' + user + '@iitb.ac.in\n' + 'Subject:testing \n'
msg = header + '\n this is a test message\n\n'
smtpserver.sendmail(user, to, msg)
smtpserver.close()



Could anyone please help me out solving the problem ?

--
Thanks and Regards,
Jay.
PowerAnser Lab,
IIT Bombay.

Dilawar

unread,
Jun 18, 2014, 5:51:20 AM6/18/14
to wncc...@googlegroups.com
Following was working for me.
 

           
# Now construct the mail msg
            HOST
= 'smtp-auth.iitb.ac.in'
            message
= MIMEMultipart()
            FROM
= 'som...@eee.iitb.ac.in'
            COMMMASPACE
= ', '
            TO
= [email_id]
            CC
= ['dila...@iitb.ac.in'']


            message
["From"] = "dila...@iitb.ac.in"
            message
["To"] = unicode(TO)
            message
["Subject"] = "Attached files are very similar. Meet your instructor!"
            message
["Date"] = formatdate(localtime=True)


            f
= open('msg.txt', 'r')
            text
= f.read()
            text
= text + msg
            text
= text +'\n--\n' \
                   
+'\nThis email is system-generated. You need not reply.'


            message
.attach(MIMEText(text))
           
# attach a file
            part
= MIMEBase('application', "octet-stream")
            part
.set_payload( open(tarfile_name,"rb").read() )
           
Encoders.encode_base64(part)
            part
.add_header('Content-Disposition', 'attachment; filename="%s"'\
                   
% os.path.basename(tarfile_name))
            message
.attach(part)
         
           
# get username and password. Or hardcode it.
            username
= os.getenv('proxy_username')
            password
= os.getenv('proxy_password')


            server
= smtplib.SMTP(HOST, 25)
            server
.starttls()
            server
.set_debuglevel(2)
            server
.login(username, password) # optional
           
try:
               
print 'Sending email.'
                TO
= TO + CC
                failed
= server.sendmail(FROM, TO, message.as_string())
                server
.close()
           
except Exception, e:
                errorMsg
= "Unable to send email. Error: %s" % str(e)


On Wednesday, June 18, 2014 3:11:22 PM UTC+5:30, Jay Parikh wrote:

Pritam Baral

unread,
Jun 18, 2014, 5:54:46 AM6/18/14
to wncc...@googlegroups.com
Jay, you need to use CRLF, '\r\n' as separators on your header string.

I'd recommend using a library to construct messages, but for academic purposes, knowing the down-and-dirty details is always good.

 

Regards,
Chhatoi Pritam Baral



--
--
The website for the club is http://wncc-iitb.org/
To post to this group, send email to wncc...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "Web and Coding Club IIT Bombay" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wncc_iitb+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pritam Baral

unread,
Jun 18, 2014, 5:56:53 AM6/18/14
to wncc...@googlegroups.com
Errata: ... '\r\n' as separators on your header string

Amol Mandhane

unread,
Jun 18, 2014, 10:54:49 AM6/18/14
to Web and Coding Club IITB
Adding 3 lines (highlighted) to your code. This should work.

import smtplib
 
to = 'a...@gmail.com'
user = 'username'
pwd = 'pwd'
smtpserver = smtplib.SMTP("smtp-auth.iitb.ac.in",25)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.esmtp_features['auth']='LOGIN DIGEST-MD5 PLAIN'

smtpserver.login(user, pwd)
header = 'To:' + to + '\n' + 'From: ' + user + '@iitb.ac.in\n' + 'Subject:testing \n'
msg = header + '\n this is a test message\n\n'
smtpserver.sendmail(user, to, msg)
smtpserver.close()




AM

Jay Parikh

unread,
Jun 18, 2014, 3:30:28 PM6/18/14
to wncc...@googlegroups.com
Thanks Amol and everyone for suggestions and answers...

@Amol : Those 3 lines made my life. ;)  It worked. Thanks a lot.


--
--
The website for the club is http://wncc-iitb.org/
To post to this group, send email to wncc...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "Web and Coding Club IIT Bombay" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wncc_iitb+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages