Posterous
Daniele is using Posterous to post everything online. Shouldn't you?
Unknown35
 

Daniel’strae

Is cuma cá mhinice a théann tú ar strae; is é is tábhachtaí gurb áil leat do bhealach a aimsiú arís.

Where is the phpMailer php class equivalent for Python?

vote up 2 vote down
star
1

Hi guys, i'm new with python.. Actually, i'm trying to send featured email with python: html body, text alternative body, and attachment.

So, i've found this tutorial and adapted it with the gmail authentication (tutorial found here)

The code i have atm, is that:

def createhtmlmail (html, text, subject):
"""Create a mime-message that will render HTML in popular
  MUAs, text in better ones"""

import MimeWriter
import mimetools
import cStringIO
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import os

out = cStringIO.StringIO() # output buffer for our message
htmlin
= cStringIO.StringIO(html)
txtin
= cStringIO.StringIO(text)

writer
= MimeWriter.MimeWriter(out)
#
# set up some basic headers... we put subject here
# because smtplib.sendmail expects it to be in the
# message body
#
writer
.addheader("Subject", subject)
writer
.addheader("MIME-Version", "1.0")
#
# start the multipart section of the message
# multipart/alternative seems to work better
# on some MUAs than multipart/mixed
#
writer
.startmultipartbody("alternative")
writer
.flushheaders()
#
# the plain text section
#
subpart
= writer.nextpart()
subpart
.addheader("Content-Transfer-Encoding", "quoted-printable")
pout
= subpart.startbody("text/plain", [("charset", 'us-ascii')])
mimetools
.encode(txtin, pout, 'quoted-printable')
txtin
.close()
#
# start the html subpart of the message
#
subpart
= writer.nextpart()
subpart
.addheader("Content-Transfer-Encoding", "quoted-printable")
#
# returns us a file-ish object we can write to
#
pout
= subpart.startbody("text/html", [("charset", 'us-ascii')])
mimetools
.encode(htmlin, pout, 'quoted-printable')
htmlin
.close()


#
# Now that we're done, close our writer and
# return the message body
#
writer
.lastpart()
msg
= out.getvalue()
out.close()
return msg

import smtplib
f
= open("/path/to/html/version.html", 'r')
html
= f.read()
f
.close()
f
= open("/path/to/txt/version.txt", 'r')
text
= f.read()
subject
= "Prova email html da python, con allegato!"
message
= createhtmlmail(html, text, subject)
gmail_user
= "thegmailaccount@gmail.com"
gmail_pwd
= "thegmailpassword"
server
= smtplib.SMTP("smtp.gmail.com", 587)
server
.ehlo()
server
.starttls()
server
.ehlo()
server
.login(gmail_user, gmail_pwd)
server
.sendmail(gmail_user, "example@example.com", message)
server
.close()

and that works.. now only miss the attachment.. And i am not able to add the attachment (from this post)

So, why there is not a python class like phpMailer for php? Is it because, for a medium-able python programmer sending a html email with attachment and alt text body is so easy that a class is not needed? Or is because i just didn't find it?

If i'll be able to wrote a class like that, when i'll be enough good with python, would that be useful for someone?

S.Lott
41.7k328104
asked Apr 30 at 14:48
DaNieL
4189

4 Answers

vote up 4 vote down
check

If you can excuse some blatant self promotion, I wrote a mailer module that makes sending email with Python fairly simple. No dependencies other than the Python smtplib and email libraries.

Here's a simple example for sending an email with an attachment:

from mailer import Mailer
from mailer import Message

message
= Message(From="me@example.com",
                 
To=["you@example.com", "him@example.com"])
message
.Subject = "Kitty with dynamite"
message
.Body = """Kitty go boom!"""
message
.attach("kitty.jpg")

sender
= Mailer('smtp.example.com')
sender
.login("username", "password")
sender
.send(message)

Edit: Here's an example of sending an HTML email with alternate text. :)

from mailer import Mailer
from mailer import Message

message
= Message(From="me@example.com",
                 
To="you@example.com",
                  charset
="utf-8")
message
.Subject = "An HTML Email"
message
.Html = """This email uses <strong>HTML</strong>!"""
message
.Body = """This is alternate text."""

sender
= Mailer('smtp.example.com')
sender
.send(message)

Edit 2: Thanks to one of the comments, I've added a new version of mailer to pypi that lets you specify the port in the Mailer class.

answered Apr 30 at 16:56
Ryan Ginstrom
99019

 
 
add in an example of alternate text and you've got a winner! – YHVH Apr 30 at 18:32

Wow, that's great! Does your module allow send mail througt the Gmail smtp? Where should i specify the smtp port (such as server = smtplib.SMTP("smtp.gmail.com", 587) with smtplib) – DaNieL May 4 at 7:50 
 
 
@DaNiel: Very good point, thanks. I've added an updated version that lets you specify the port in the Mailer class (`sender = Mailer('localhost', port=587)') pypi.python.org/pypi/mailer/0.4 – Ryan Ginstrom May 7 at 10:47

Hi Ryan, i tried the 0.4 version with gmail but it raise an error: File "C:\Python26\lib\smtplib.py", line 522, in login raise SMTPException("SMTP AUTH extension not supported by the server") Maybe its becose gmail requires the TLS? – DaNieL May 11 at 7:00 
add comment

vote up 1 vote down
check

Django includes the class you need in core, docs here

from django.core.mail import EmailMultiAlternatives

subject
, from_email, to = 'hello', 'from@example.com', 'to@example.com'
text_content
= 'This is an important message.'
html_content
= '<p>This is an <strong>important</strong> message.</p>'
msg
= EmailMultiAlternatives(subject, text_content, from_email, [to])
msg
.attach_alternative(html_content, "text/html")
msg
.attach_file('/path/to/file.jpg')
msg
.send()

In my settings I have:

#GMAIL STUFF
EMAIL_USE_TLS
= True
EMAIL_HOST
= 'smtp.gmail.com'
EMAIL_HOST_USER
= 'name@gmail.com'
EMAIL_HOST_PASSWORD
= 'password'
EMAIL_PORT
= 587
answered Apr 30 at 15:22
YHVH
16817


That's what i was looking for! Thanks mate. Last thing: to use that class we must download and install django, obvisiuly.. Cold be fine a class that run without the django framework, but just running the class file (and the natural python modules)) – DaNieL Apr 30 at 15:30 

Fom Stackoverflow.com

Loading mentions Retweet
Filed under  //   Email   Python  
Posted June 5, 2009
// 0 Comments

Does anyone knows any Train-table-api service?

vote up 2 vote down
star

Hi guys, im wondering if there is any online service who provide API about the train timetables (arrivals, departure, etch..), at least for the european stations.

I know www.bahn.de, who provide the accurated timetables for many european countries, but i didin't find any similar to a api service.

My goal (well, just a future-project) is to develope an application like gmaps, but instead of giving the car-trip i would like to give the train-trip.. you know, the user set the departure day, time and station, then the first arrival, maybe then add another one, and so on.

So, there is something like, that can be queryed by php/python/javascript?

Edit: if can help, im wondering to build a service to plan an inter-rail trip! (and any help will be really appreciate)

asked May 28 at 7:04
DaNieL
4189

 
 
wouldn't you duplicate the functionality of bahn.de? they have the exact route planner you describe. – micha2305 May 28 at 13:32

Yes.. well, i dont want to reinvent the wheel, but what i'm trying to figure out is an web application chained to gmaps, panoramio and wikimage, where people would be able to build-edit-then share with friends they'rs trip's... i cant redirect to bahn.de for paln the trip, then get back for the locations, suggested places, etc.. ;) – DaNieL May 28 at 14:00 
add comment

2 Answers

vote up 1 vote down
check

Something in a common format like Google Transit Feed?, there are some US based ones, as well as one I found for London.

answered May 28 at 7:33
garrow
1,14210


Yes, something like GTF but worldwide -or at least- to the europe... Well, seem like google is 'waiting' that the public agency give he the data, so should be just a matter of time. – DaNieL May 28 at 7:58 


From StackOverflow.com

Loading mentions Retweet
Filed under  //   API   Google   onmyway  
Posted June 5, 2009
// 0 Comments

Best way to retrieve variable values from a text file - Python - Json

vote up 1 vote down
star

Referring on this question, i have a similar -but not the same- problem..

On my way, i'll have some text file, structured like:

var_a: 'home'
var_b
: 'car'
var_c
: 15.5

And i need that python read the file and then create a variable named var_a with value 'home', and so on.

Example:

#python stuff over here
getVarFromFile
(filename) #this is the function that im lookin for
print var_b
#output: car, as string
print var_c
#output 15.5, as number.

Is this possible, i mean, even keep the var type?

Notice that i have the full freedom to the text file structure, i can use the format i like if the one i proposed isn't the best.

EDIT: the ConfigParser can be a solution, but i dont like it so much, becose in my script i'll have then to refer to the variables in the file with

config.get("set", "var_name")

But what i'll love is to refer to the variable direclty, as i declared it in the python script..

There is a way to impoer the file as a python dictionary?

Oh, last thing, keep in mind that i dont know exactly how many variables would i have in the text file

Edit 2: i'm very interessing to the stephan json solution, becose in that way the text file could be readed simply with others languages (php, then via ajax javascript, for example), but i fail in something while acting that solution:

#for the example, i dont load the file but create a var with the supposed file content
file_content
= "'var_a': 4, 'var_b': 'a string'"
mydict
= dict(file_content)
#Error: ValueError: dictionary update sequence element #0 has length 1; 2 is required
file_content_2
= "{'var_a': 4, 'var_b': 'a string'}"
mydict_2
= dict(json.dump(file_content_2, True))
#Error:
#Traceback (most recent call last):
#File "<pyshell#5>", line 1, in <module>
#mydict_2 = dict(json.dump(file_content_2, True))
#File "C:\Python26\lib\json\__init__.py", line 181, in dump
#fp.write(chunk)
#AttributeError: 'bool' object has no attribute 'write'

In what kind of issues can i fall with the Json format? And, how can i read a json array in a text file, and transform it in a python dict?

p.s: i dont like the solution using .py files, i'll prefer .txt, .inc, .whatever is not restrictive to one language

asked May 29 at 6:48
DaNieL
4189

 
 
You can't manage without any import modules... you could manage with just the sys module, but it wouldn't be as nice as the other solutions suggested :) – workmad3 May 29 at 7:12

ok, its not a big problem – DaNieL May 29 at 7:16 
 
 
regarding your Edit2: you want the_dict = json.loads('{"var_a": 4, "var_b": "a string"}'). Pls note that I have switched " and '. – stephan May 29 at 9:13

And that's exactly what i want. Thanks man! – DaNieL May 29 at 9:33 
add comment

6 Answers

vote up 2 vote down
check

Load your file with JSON or PyYAML into a dictionary the_dict (see doc for JSON or PyYAML for this step, both can store data type) and add the dictionary to your globals dictionary, e.g. using globals().update(the_dict).

If you want it in a local dictionary instead (e.g. inside a function), you can do it like this:

for x in the_dict.items():
   
exec('%s=%s' % x)

as long as it is safe to use exec. If not, you can use the dictionary directly.

answered May 29 at 7:42
stephan
5007


Can you please show a basilar example? – DaNieL May 29 at 7:58 
1
 
A shorter idiom is globals().update(the_dict) or locals().update(the_dict). – Jouni K. Seppänen May 29 at 10:12
 
 
Thanks for pointing this out for globals(). Amended my answer. locals() is however read-only (see docs.python.org/library/functions.html#locals/…). – stephan May 29 at 10:29
 
 
It's not really read-only (I have used it myself) but apparently there are circumstances in which writing to it doesn't work. Thanks for pointing this out. – Jouni K. Seppänen May 31 at 9:59
add comment

vote up 5 vote down
check

But what i'll love is to refer to the variable direclty, as i declared it in the python script..

Assuming you're happy to change your syntax slightly, just use python and import the "config" module.

# myconfig.py:

var_a
= 'home'
var_b
= 'car'
var_c
= 15.5

Then do

from myconfig import *

And you can reference them by name in your current context.

answered May 29 at 7:54
yangyang
963

 
 
+1, This is absolutely THE best way to do config files. No need to grow your own config syntax, config parsers, config loaders etc. when you can just re-use python's core parts! – Simon May 29 at 8:13
 
 
+1 yes, as long as you can trust your config file and don't need portability – stephan May 29 at 8:26
 
 
Convenience and flexibility ++ vs. security -- : in some situations it's great, in others... less so. – mavnn May 29 at 8:28

I have to discard this solution becose i dont want to restric the file to python only, i think is better to use a generic file format (.txt, .inc, .whatever) that can be accessed by others languages too (with all the safe measure that this solution will mean). Anyway i'll keep in mind this way for the future – DaNieL May 29 at 9:37 
add comment

vote up 5 vote down
check

Use ConfigParser.

Your config:

[myvars]
var_a
: 'home'
var_b
: 'car'
var_c
: 15.5

Your python code:

import ConfigParser

config
= ConfigParser.ConfigParser()
config
.read("config.ini")
var_a
= config.get("myvars", "var_a")
var_b
= config.get("myvars", "var_b")
var_c
= config.get("myvars", "var_c")
answered May 29 at 6:57
Igor Krivokon
1,3939


Just edited - this way is fine, but if possible im lookin for another one who would let me using the variables in a usefull way – DaNieL May 29 at 7:17 
 
 
I've updated the answer to show you how to use these as variables. – Igor Krivokon May 29 at 8:26
 
 
Or you could take the Bunch class from code.activestate.com/recipes/52308/ and hack it to be recursive, so you could refer to config.myvars.var_a directly. – Jouni K. Seppänen May 29 at 10:16
add comment

vote up 2 vote down
check

How reliable is your format? If the seperator is always exactly ': ', the following works. If not, a comparatively simple regex should do the job.

As long as you're working with fairly simple variable types, Python's eval function makes persisting variables to files surprisingly easy.

(The below gives you a dictionary, btw, which you mentioned was one of your prefered solutions).

def read_config(filename):
    f
= open(filename)
    config_dict
= {}
   
for lines in f:
        items
= lines.split(': ', 1)
        config_dict
[items[0]] = eval(items[1])
   
return config_dict
answered May 29 at 8:27
mavnn
3129


I can wrote the text file exaclty as i want, the format var: value is just for example, if others formats are better, fell free to suggest – DaNieL May 29 at 8:37 
 
 
As long as it's consistant, and the value is of the form generated by repr(variable) it doesn't really matter for this method. Just pick a seperator that will never be part of the variable name and use that in the split method. – mavnn May 29 at 10:47
add comment

vote up 1 vote down
check

What you want appear to want is the following, but this is NOT RECOMMENDED:

>>> for line in open('dangerous.txt'):
...     exec('%s = %s' % tuple(line.split(':', 1)))
...
>>> var_a
'home'

This creates somewhat similar behavior to PHP's register_globals and hence has the same security issues. Additionally, the use of exec that I showed allows arbitrary code execution. Only use this if you are absolutely sure that the contents of the text file can be trusted under all circumstances.

You should really consider binding the variables not to the local scope, but to an object, and use a library that parses the file contents such that no code is executed. So: go with any of the other solutions provided here.

(Please note: I added this answer not as a solution, but as an explicit non-solution.)

answered May 29 at 7:55
community wiki



Yes, thanks for the raccomandation, i've banned the exec function both from my php and python scripts time ago ;) – DaNieL May 29 at 7:59 
add comment

vote up 0 vote down
check

You can treat your text file as a python module and load it dynamically:

import

Loading mentions Retweet
Filed under  //   PHP   Python  
Posted June 5, 2009
// 0 Comments

Using Your Own URL as Your OpenID

One of the cooler features of OpenID is delegation. This means, instead of having your OpenID identifier be

yourname.myopenid.com

it can be

yourdomain.com

Much easier to remember, right? And it’s really easy to do, too! Here’s how I did it.

First, sign up for an OpenID with the provider of your choice (note that Google, Yahoo, and Microsoft are all OpenID providers now, so you may already have one). I like myopenid.com, so I originally signed up there. My OpenID identifier is codinghorror.myopenid.com.

To enable my domain to act as a delegate to the OpenID provider, I added these two HTML header tags to the homepage of codinghorror.com.

<html>
<head>
<title>Coding Horror</title>
<link rel="shortcut icon" href="/favicon.ico" />
<link rel="openid.server" href="http://www.myopenid.com/server">
<link rel="openid.delegate" href="http://codinghorror.myopenid.com/">
</head>

Then I used our new multiple OpenID feature to tell Stack Overflow about my new OpenID. Here on my user page, I can see that I already have two OpenIDs attached to my account.

openid-alternate-new-loginopenid-alternate-new-login

In case you’re wondering, the OpenID fields here are only visible because I’m logged in as myself. The OpenID identifiers aren’t normally visible to anyone else.

I want to attach a new OpenID to my account, so I click New Login. Note that I must be logged in first to change my OpenIDs, so I need to be sure I click New Login!

openid-alternate-login-entry

I enter codinghorror.com as my OpenID identifier, then click the Login button (or just press Enter).

When I’m returned to the page, I had a brand new OpenID identifier in my primary slot!

openid-alternate-overwrite-primary

Now I can log in to any OpenID enabled website using codinghorror.com!

One minor technical note: when logging in via the “New Login” link, your alternate OpenID will be filled first, and all subsequent new logins will overwrite your primary OpenID. Use the new “Swap” link to switch them around, so you can overwrite whichever one you want (thanks Stewart Johnson for that suggestion).

Posted by Jeff Atwood on January 4th, 2009
Filed under design

From StackOverflow.com

Loading mentions Retweet
Filed under  //   Auth   OpenID  
Posted June 5, 2009
// 0 Comments