469,610 Members | 1,554 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

passing string from one file to another

Kun
I have a python-cgi form whose sole purpose is to email.

It has the fields 'to', 'from', 'subject', 'body', etc. and if the user
fills them out and clicks submit, it will invoke another file called
mail.py which uses smtplib to send the message.

This works fine but instead of typing in a 'body', i would like the
initial python program to just send a string as the body of the email.
now normally i'd just set the msg in the mail.py file equal to the
string, however, i do not know how to link a string from another python
file to the mail.py file.

does anyone know a solution to this?
Apr 17 '06 #1
7 2560
Kun
Kun wrote:
I have a python-cgi form whose sole purpose is to email.

It has the fields 'to', 'from', 'subject', 'body', etc. and if the user
fills them out and clicks submit, it will invoke another file called
mail.py which uses smtplib to send the message.

This works fine but instead of typing in a 'body', i would like the
initial python program to just send a string as the body of the email.
now normally i'd just set the msg in the mail.py file equal to the
string, however, i do not know how to link a string from another python
file to the mail.py file.

does anyone know a solution to this?


i am aware that i could write the string to a text file and invoke it
with the second python program, however, unfortunately i do not have
write permission on the server i am working with, thus we need to find
some other way...

your help would be greatly appreciated
Apr 17 '06 #2
I V
Kun wrote:
This works fine but instead of typing in a 'body', i would like the
initial python program to just send a string as the body of the email.
now normally i'd just set the msg in the mail.py file equal to the
string, however, i do not know how to link a string from another python
file to the mail.py file.


Where does mail.py get the body from at the moment? And how are you
invoking mail.py? The obvious would be to import mail.py and call a
function in it; then, you would just need to change the string you pass
to the function. But presumably that's not how you've got it set up or
you wouldn't be asking the question. If you can explain a bit more how
your program works that would be helpful, maybe post an exerpt of the
code that shows where mail.py gets invoked from the main program, and
the bit of mail.py that accesses the body that gets sent.

Apr 17 '06 #3
Kun
I V wrote:
Kun wrote:
This works fine but instead of typing in a 'body', i would like the
initial python program to just send a string as the body of the email.
now normally i'd just set the msg in the mail.py file equal to the
string, however, i do not know how to link a string from another python
file to the mail.py file.


Where does mail.py get the body from at the moment? And how are you
invoking mail.py? The obvious would be to import mail.py and call a
function in it; then, you would just need to change the string you pass
to the function. But presumably that's not how you've got it set up or
you wouldn't be asking the question. If you can explain a bit more how
your program works that would be helpful, maybe post an exerpt of the
code that shows where mail.py gets invoked from the main program, and
the bit of mail.py that accesses the body that gets sent.

mail currently gets the body from an input box.

this is where mail.py gets invoked:

<h1>Email Results</h1>
<p>
<Table>
<FORM METHOD="post" ACTION="mail.py">

<TR><TD>SMTP Server:</TD>
<TD><input type="text" name="SMTP Server"
value="webmail.wharton.upenn.edu" size=20>
</TD></TR>
<TR><TD>Username:</TD>
<TD><input type="text" name="Username" size=20>
</TD></TR>
<TR><TD>Password:</TD>
<TD><input type="password" name="Password" size=20>
</TD></TR>
<TR><TD>From:</TD>
<TD><input type="text" name="From" size=20>
</TD></TR>
<TR><TD>To:</TD>
<TD><input type="text" name="To" size=20>
</TD></TR>
<TR><TD>Subject:</TD>
<TD><input type="text" name="Subject" size=20>
</TD></TR>
<TR><TD>Message:</TD>
<TD><TEXTAREA wrap="virtual" name="Message" cols=40 rows=5>
</TEXTAREA></TD></TR>
<TR><TD></TD><TD><input type="submit" value="Submit"><input type="reset">
</form></TD></TR></Table></HTML>"""


this is mail.py


#!/usr/bin/env python
import cgi
import smtplib
import os
import sys
import urllib
import re
from email.MIMEText import MIMEText

print "Content-type: text/html\n"

form = cgi.FieldStorage() #Initializes the form dictionary to take data
from html form
key = [ 'SMTP Server', 'Username', 'Password', 'From', 'To', 'Subject',
'Message' ]

def get(form, key):
if form.has_key(key):
return form[key].value
else:
return ""

if get(form, "SMTP Server") or get(form, "Username") or get(form,
"Password") or get(form, "From") or get(form, "To") or get(form,
"Subject") or get(form, "Message"):
print ''
else:
print 'Error: You did not enter any Email parameters'
print '<br>'
print '<a
raise ValueError("nothing entered")
##mail = open('mail.txt','rb')
##msg = MIMEText(mail.read())
##mail.close()
msg = MIMEText(form['Message'].value) #mime text is a method that takes
in a text variable and creates a dictionary whose contects are
inatialized according to the text.
##print MIMEText(form['Message'].value)
##msg = msg.as_string() + mail

msg['Subject'] = form['Subject'].value
msg['From'] = form['From'].value
msg['To'] = form['To'].value

# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP(form['SMTP Server'].value)
s.login(form['Username'].value,form['Password'].value)
s.sendmail(form['From'].value, [form['To'].value], msg.as_string())
s.close()

print """<HTML><Head><Title>Email Confirmation
Page</Title></Head><br><Body>Your email has been sent.<br></Body></HTML>"""
Apr 17 '06 #4
I V
Kun wrote:
mail currently gets the body from an input box.

this is where mail.py gets invoked:


OK, I'm a bit confused. Where is the "initial python program" in all
this? You seem to have an one python program (mail.py) and an HTML
form. As it stands, I don't see why you can't change mail.py so that it
refers to your string instead of msg.as_string() .

Apr 17 '06 #5
Kun
I V wrote:
Kun wrote:
mail currently gets the body from an input box.

this is where mail.py gets invoked:


OK, I'm a bit confused. Where is the "initial python program" in all
this? You seem to have an one python program (mail.py) and an HTML
form. As it stands, I don't see why you can't change mail.py so that it
refers to your string instead of msg.as_string() .

the html i pasted is part of a python-cgi file that is the 'initial'
file. i can't just tell mail.py to use the string because the string is
defined in the first python profile, not mail.py.
Apr 17 '06 #6
Ant
I assume that you are trying to pass data from one 'standalone' cgi
script to another cgi script (mail.py). Depending on what exactly you
are trying to do, you could either set the information in a cookie, or
simply have a hidden input (<input type='hidden' name="data_to_pass_on"
value="bob">) element in the HTML which gets populated by the initial
cgi script and is then read by mail.py.

Apr 17 '06 #7
In article <e1***********@netnews.upenn.edu>, Kun <ne*******@gmail.com>
wrote:
I have a python-cgi form whose sole purpose is to email.

It has the fields 'to', 'from', 'subject', 'body', etc. and if the user
fills them out and clicks submit, it will invoke another file called
mail.py which uses smtplib to send the message.


Why do you need 2 Python scripts? Why can't the first one use smtplib to
send the message directly?
Apr 17 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by domeceo | last post: by
7 posts views Thread by Ken Allen | last post: by
reply views Thread by Iain McIntosh | last post: by
7 posts views Thread by =?Utf-8?B?YmVyaWNr?= | last post: by
13 posts views Thread by Andy Baker | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.