Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8786271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:34:38+00:00 2026-06-13T21:34:38+00:00

I am developing an application using python where I need to send a file

  • 0

I am developing an application using python where I need to send a file through mail. I wrote a program to send the mail but dont know there’s something wrong. The code is posted below. Please any one help me with this smtp library. Is there’s anything i m missing? And also can someone please tell me what will be the host in smtp! I am using smtp.gmail.com.
Also can any one tell me how can i email a file (.csv file). Thanks for the help!

#!/usr/bin/python

import smtplib

sender = 'someone@yahoo.com'
receivers = ['someone@yahoo.com']

message = """From: From Person <someone@yahoo.com>
To: To Person <someone@yahoo.com>
Subject: SMTP e-mail test

This is a test e-mail message.
"""

try:
   smtpObj = smtplib.SMTP('smtp.gmail.com')
   smtpObj.sendmail(sender, receivers, message)         
   print "Successfully sent email"
except:
   print "Error: unable to send email"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:34:39+00:00Added an answer on June 13, 2026 at 9:34 pm

    You aren’t logging in. There are also a couple reasons you might not make it through including blocking by your ISP, gmail bouncing you if it can’t get a reverse DNS on you, etc.

    try:
       smtpObj = smtplib.SMTP('smtp.gmail.com', 587) # or 465
       smtpObj.ehlo()
       smtpObj.starttls()
       smtpObj.login(account, password)
       smtpObj.sendmail(sender, receivers, message)         
       print "Successfully sent email"
    except:
       print "Error: unable to send email"
    

    I just noticed your request to be able to attach a file. That changes things since now you need to deal with encoding. Still not that tough to follow though I don’t think.

    import os
    import email
    import email.encoders
    import email.mime.text
    import smtplib
    
    # message/email details
    my_email = 'myemail@gmail.com'
    my_passw = 'asecret!'
    recipients = ['jack@gmail.com', 'jill@gmail.com']
    subject = 'This is an email'
    message = 'This is the body of the email.'
    file_name = 'C:\\temp\\test.txt'
    
    # build the message
    msg = email.MIMEMultipart.MIMEMultipart()
    msg['From'] = my_email
    msg['To'] = ', '.join(recipients)
    msg['Date'] = email.Utils.formatdate(localtime=True)
    msg['Subject'] = subject
    msg.attach(email.MIMEText.MIMEText(message))
    
    # build the attachment
    att = email.MIMEBase.MIMEBase('application', 'octet-stream')
    att.set_payload(open(file_name, 'rb').read())
    email.Encoders.encode_base64(att)
    att.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file_name))
    msg.attach(att)
    
    # send the message
    srv = smtplib.SMTP('smtp.gmail.com', 587)
    srv.ehlo()
    srv.starttls()
    srv.login(my_email, my_passw)
    srv.sendmail(my_email, recipients, msg.as_string())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a dictionary kind of application using python. In my code, there is
I am developing a desktop application using Qt (Pyside) and Python. Is there any
I am developing a GPL-licensed application in Python and need to know if the
I'm developing an application using python gtk+ and glade. My glade file format is
First, some background: I'm developing a web application using Python. All of my (text)
Currently developing an application using the newest version of symfony, obtained through PEAR. This
I am developing a web application using Python, Django and MySql. I have a
im a novice into developing an application using backend as Python (2.5) and Qt(3)
I am developing an application on the Google App Engine using Python (and Django,
i am developing an application using python that requires to be notified when the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.