×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    7

    Lightbulb Sending an image over email

    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)

  2. #2
    Regular Contributor
    Join Date
    Jul 2004
    Posts
    76
    nice!

  3. #3
    Regular Contributor
    Join Date
    Aug 2004
    Posts
    156

    Re: Sending an image over email

    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 !!

  4. #4
    Regular Contributor
    Join Date
    Aug 2004
    Posts
    156

    Re: Sending an image over email

    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 !!

Posting Permissions

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