tags:

views:

100

answers:

4

Possible Duplicate:
Receive and send emails in python

I tried searching but couldn't find a simple way to send an email.

I'm looking for something like this:

from:"[email protected]"#email sender
To:"[email protected]"# my email
content:open('x.txt','r')

Everything I've found is complicated really: my project doesn't need so many lines.

Please, I like to learn: leave comments in each code and explain

+1  A: 
import smtplib

def prompt(prompt):
    return raw_input(prompt).strip()

fromaddr = prompt("From: ")
toaddrs  = prompt("To: ").split()
print "Enter message, end with ^D (Unix) or ^Z (Windows):"

# Add the From: and To: headers at the start!
msg = ("From: %s\r\nTo: %s\r\n\r\n"
       % (fromaddr, ", ".join(toaddrs)))
while 1:
    try:
        line = raw_input()
    except EOFError:
        break
    if not line:
        break
    msg = msg + line

print "Message length is " + repr(len(msg))

server = smtplib.SMTP('localhost')
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
Stuart Powers
`email` is a wrapper for the low-level `smtplib` module that saves you having to e.g. hardcode headers.
katrielalex
Don't forget to give credit: http://docs.python.org/library/smtplib.html
Nathon
haha, that's amusing. I knew I had this python code buried somewhere in my homedir, I'd long since forgotten its source. Thanks :)
Stuart Powers
+7  A: 

The docs are pretty darned simple:

# Import smtplib for the actual sending function
import smtplib

# Import the email modules we'll need
from email.mime.text import MIMEText

# Open a plain text file for reading.  For this example, assume that
# the text file contains only ASCII characters.
fp = open(textfile, 'rb')
# Create a text/plain message
msg = MIMEText(fp.read())
fp.close()

# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['To'] = you

# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP()
s.sendmail(me, [you], msg.as_string())
s.quit()

What bit don't you understand?

katrielalex
A: 

guy i make that code it's very pretty good but there problem

import smtplib server = smtplib.SMTP('smtp.gmail.com', 587) server.set_debuglevel(1) server.ehlo() server.starttls() server.ehlo() msg1=open('test.txt','r').read() AA='[email protected]' BB='!234567*' server.login(AA,BB) server.sendmail("[email protected]", "[email protected]", msg1) server.quit()

it's pretty good code but problem in (No Subject)‏ so how to make subject?

Hamoud-Oz
Reguardless of your rep, you can always comment on your own questions. To whom do you address this comment?
wheaties
Don't do this: the `email` module is there so that you don't have to worry about the details of the `SMTP` protocol.
katrielalex
A: 

A simple example, which works for me, using smtplib:

#!/usr/bin/env python

import smtplib                           # Brings in the smtp library

smtpServer='smtp.yourdomain.com'         # Set the server - change for your needs
fromAddr='you@yourAddress'               # Set the from address - change for your needs
toAddr='you@yourAddress'                 # Set the to address - change for your needs

# In the lines below the subject and message text get set up
text='''Subject: Python send mail test

Hey!

This is a test of sending email from within Python.  

Yourself!
'''

server = smtplib.SMTP(smtpServer)        # Instantiate server object, making connection
server.set_debuglevel(1)                 # Turn debugging on to get problem messages
server.sendmail(fromAddr, toAddr, text)  # sends the message
server.quit()                            # you're done

This is code I found a while back at http://xahlee.org/perl-python/sending_mail.html and modified.

GreenMatt