You Are Here:

Community: Developer Discussion Boards

#1 Old Lightbulb Sending an image over email - 2005-01-15, 15:07

Join Date: Jan 2005
Posts: 7
jeclarim
Offline
Registered User
A proper way to do this would be to use smtplib and email packages but as they are not included in the default Python for Series 60 release, here is a quick hack which does it, using only the base modules:

import os, rfc822, socket, time, whrandom

# parameters
orig = 'origname@orighost.org'
dest = 'destname@desthost.net'
server = '123.123.123.123' # SMTP server address for dest
subject = 'your image'
text = 'here you go...'
image = 'c:\\nokia\\images\\photo.jpg'
mimetype = 'image/jpeg'
domain = 'pynokia'

# send mail with image attached
filename = os.path.basename(image)
data = open(image, 'rb').read()
now = time.time()
date = rfc822.formatdate(now)
number = whrandom.randint(0, 0xffffff)
msgid = '<%X.%X@%s>' % (now, number, domain)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'connecting to SMTP server...'
s.connect((server, 25))
print 'sending headers...'
f = s.makefile()
assert f.readline().startswith('2')
print >>f, 'helo', domain
f.flush()
assert f.readline().startswith('2')
print >>f, 'mail from:', orig
f.flush()
assert f.readline().startswith('2')
print >>f, 'rcpt to:', dest
f.flush()
assert f.readline().startswith('2')
print >>f, 'data'
f.flush()
assert f.readline().startswith('3')
print >>f, 'Message-ID:', msgid
print >>f, 'Date:', date
print >>f, 'From:', orig
print >>f, 'MIME-Version: 1.0'
print >>f, 'To:', dest
print >>f, 'Subject:', subject
boundary = '------' + date.encode('hex')
print >>f, 'Content-Type: multipart/mixed; boundary="%s"' % boundary
print >>f
print >>f, 'This is a multi-part message in MIME format.'
print >>f, '--' + boundary
print >>f, 'Content-Type: text/plain; charset=utf-8'
print >>f
print >>f, text
print >>f, '--' + boundary
print >>f, 'Content-Type: %s; name="%s"' % (mimetype, filename)
print >>f, 'Content-Transfer-Encoding: base64'
print >>f, 'Content-Disposition: attachment; filename="%s"' % filename
print >>f
print 'sending data...'
print >>f, data.encode('base64')
print >>f, '--' + boundary + '--'
print >>f
print >>f, '.'
f.flush()
assert f.readline().startswith('2')
print >>f, 'quit'
f.flush()
s.close()
print '%d bytes image sent in %d s' % (len(data), time.time() - now)
Reply With Quote

#2 Old 2005-01-26, 00:28

Join Date: Jul 2004
Posts: 76
eriksmartt
Offline
Regular Contributor
nice!
Reply With Quote

#3 Old Re: Sending an image over email - 2006-12-15, 21:18

Join Date: Aug 2004
Posts: 146
krtrivdi's Avatar
krtrivdi
Offline
Forum Nokia Champion
would you pleaes give the properly formatted code, i m not able to understand it. please any one can write code so that i can test and understand it..I am really learning a lot from Nokia Forum..


||Kiran ||
Nokia E-65
Nokia 7610
Nokia N95 8GB
Nokia E90
Nokia 5110 !!
Nokia 6110i !!
Reply With Quote

#4 Old Re: Sending an image over email - 2006-12-15, 21:25

Join Date: Aug 2004
Posts: 146
krtrivdi's Avatar
krtrivdi
Offline
Forum Nokia Champion
what should i write for server? is it "www.xyz.com" or "smtp.xyz.com"?
and what should i write for domain ? Again "www.xyz.com"?


||Kiran ||
Nokia E-65
Nokia 7610
Nokia N95 8GB
Nokia E90
Nokia 5110 !!
Nokia 6110i !!
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomE2fforumE2fshowthreadE2ephpE3ftE3d81449X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE44iscussionQ qdcZtypeQUqfntypeZE44iscussionContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZpythonQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE44iscussionQ qfnZtypeQUqfntypeZE44iscussionContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZuserE5ftagQSxpythonX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE44iscussionQ qrdfZtypeQUqfntypeZE44iscussionContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ