passing string from one file to another

Kun neurogasm at gmail.com
Mon Apr 17 01:23:14 EDT 2006


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>"""



More information about the Python-list mailing list