Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » Python

Sending e-mail

 
Jump to:  
 
Guest
PostPosted: Thu Aug 28, 2008 6:52 pm    Post subject: Sending e-mail
       
I work at a training center and I would like to use Python to generate
a number of certificates and then e-mail them. The certificates are a
problem for another day - right now I just want to figure out how to
send an e-mail.

I confess I don't know much about the protocol(s) for e-mail. In PHP
using CodeIgniter, the same task was amazingly easy. I think this is a
bit harder because I'm not executing code on a remote machine that has
its own SMTP server. According to (http://www.devshed.com/c/a/Python/
Python-Email-Libraries-SMTP-and-Email-Parsing/), I need to use the
SMTP object found in the smtplib module to initiate a connection to a
server before I can send. I want to use Gmail, but on the following
input it stalls and then raises smtplib.SMTPServerDisconnected:

server = SMTP("smtp.gmail.com")

I also pinged smtp.gmail.com and tried it with the dotted quad IP as a
string. Am I on the right track? Is this a problem with gmail, or have
I gotten an assumption wrong?

Here's a thought: Could I use SMTPServer (http://docs.python.org/lib/
node620.html) to obviate the need to have anything to do with Gmail?
What would be the limitations on that? Could I successfully do this
and wrap the whole thing up in a black box? What modules would I need?

Thanks.
 

 
Waldemar Osuch
PostPosted: Thu Aug 28, 2008 6:59 pm    Post subject: Re: Sending e-mail
       
On Aug 28, 12:52 pm, peter.jones....@gmail.com wrote:
Quote:
I work at a training center and I would like to use Python to generate
a number of certificates and then e-mail them. The certificates are a
problem for another day - right now I just want to figure out how to
send an e-mail.

I confess I don't know much about the protocol(s) for e-mail. In PHP
using CodeIgniter, the same task was amazingly easy. I think this is a
bit harder because I'm not executing code on a remote machine that has
its own SMTP server. According to (http://www.devshed.com/c/a/Python/
Python-Email-Libraries-SMTP-and-Email-Parsing/), I need to use the
SMTP object found in the smtplib module to initiate a connection to a
server before I can send. I want to use Gmail, but on the following
input it stalls and then raises smtplib.SMTPServerDisconnected:

server = SMTP("smtp.gmail.com")

I also pinged smtp.gmail.com and tried it with the dotted quad IP as a
string. Am I on the right track? Is this a problem with gmail, or have
I gotten an assumption wrong?

Here's a thought: Could I use SMTPServer (http://docs.python.org/lib/
node620.html) to obviate the need to have anything to do with Gmail?
What would be the limitations on that? Could I successfully do this
and wrap the whole thing up in a black box? What modules would I need?

Thanks.

Gmail SMTP server needs authentication.
I little googling found this example.
I did not test it if it works but it could be starting point.

LINK
 

 
gordyt
PostPosted: Thu Aug 28, 2008 7:23 pm    Post subject: Re: Sending e-mail
       
Peter here is an example. I just tried it and it works fine.

from smtplib import SMTP
HOST = "smtp.gmail.com"
PORT = 587
ACCOUNT = "" # put your gmail email account name here
PASSWORD = "" # put your gmail email account password here

def send_email(to_addrs, subject, msg):
server = SMTP(HOST,PORT)
server.set_debuglevel(1) # you don't need this
server.ehlo()
server.starttls()
server.ehlo()
server.login(ACCOUNT, PASSWORD)
server.sendmail(ACCOUNT, to_addrs,
"""From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s\r\n.\r\n""" % (
ACCOUNT, ",".join(to_addrs), subject, msg
)
)
server.quit()

if __name__ == "__main__":
send_email( ['someone@somewhere.com'], 'this is just a test',
"hello world!" )
 

 
Guest
PostPosted: Fri Aug 29, 2008 4:44 pm    Post subject: Re: Sending e-mail
       
On Aug 28, 3:23 pm, gordyt <gor...@gmail.com> wrote:
Quote:
Peter here is an example. I just tried it and it works fine.

from smtplib import SMTP
HOST = "smtp.gmail.com"
PORT = 587
ACCOUNT = "" # put your gmail email account name here
PASSWORD = "" # put your gmail email account password here

def send_email(to_addrs, subject, msg):
server = SMTP(HOST,PORT)
server.set_debuglevel(1) # you don't need this
server.ehlo()
server.starttls()
server.ehlo()
server.login(ACCOUNT, PASSWORD)
server.sendmail(ACCOUNT, to_addrs,
"""From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s\r\n.\r\n""" % (
ACCOUNT, ",".join(to_addrs), subject, msg
)
)
server.quit()

if __name__ == "__main__":
send_email( ['some...@somewhere.com'], 'this is just a test',
"hello world!" )

Thanks to everyone who's replied. gordyt, I didn't dare dream anyone
would hand me fully functional source code, so thank you very much for
that. Unfortunately, it doesn't work for me, likely because of some
complication from my company's firewall.

All things considered, going through Gmail is an unnecessary step if I
can run a server on my own PC. Is there any hope of this working? Can
it be done easily? Is there anything I should know about the
SMTPServer object, and are there any other modules I'd need?

Thanks again for all the help.
 

 
Mike Driscoll
PostPosted: Fri Aug 29, 2008 5:59 pm    Post subject: Re: Sending e-mail
       
On Aug 29, 11:44 am, peter.jones....@gmail.com wrote:
Quote:
On Aug 28, 3:23 pm, gordyt <gor...@gmail.com> wrote:



Peter here is an example.  I just tried it and it works fine.

from smtplib import SMTP
HOST = "smtp.gmail.com"
PORT = 587
ACCOUNT = ""  # put your gmail email account name here
PASSWORD = ""  # put your gmail email account password here

def send_email(to_addrs, subject, msg):
    server = SMTP(HOST,PORT)
    server.set_debuglevel(1)    # you don't need this
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(ACCOUNT, PASSWORD)
    server.sendmail(ACCOUNT, to_addrs,
        """From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s\r\n.\r\n""" % (
            ACCOUNT, ",".join(to_addrs), subject, msg
        )
    )
    server.quit()

if __name__ == "__main__":
    send_email( ['some...@somewhere.com'], 'this is just a test',
        "hello world!" )

Thanks to everyone who's replied. gordyt, I didn't dare dream anyone
would hand me fully functional source code, so thank you very much for
that. Unfortunately, it doesn't work for me, likely because of some
complication from my company's firewall.

All things considered, going through Gmail is an unnecessary step if I
can run a server on my own PC. Is there any hope of this working? Can
it be done easily? Is there anything I should know about the
SMTPServer object, and are there any other modules I'd need?

Thanks again for all the help.

I would recommend looking at the email module too as it is a little
bit more flexible:

LINK

You could also see how I do it in wxPython: LINK

My script is Windows only at the moment.

Mike
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

wizz air upominki Shazza moja miłość - Big Cyc opisy gg Angielskie cytaty