passing string from one file to another

K

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?
 
K

Kun

Kun said:
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
 
I

I V

Kun said:
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.
 
K

Kun

I said:
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>"""
 
I

I V

Kun said:
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() .
 
K

Kun

I said:
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.
 
A

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.
 
L

Lawrence D'Oliveiro

Kun <[email protected]> said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,293
Messages
2,571,501
Members
48,189
Latest member
StaciLgf76

Latest Threads

Top