473,395 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 2741
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
6
by: csvka | last post by:
Hello, I wonder if I could pick your brains. I'm beginning to learn about C++. I have opened a file in my program and I want to read lines from it. I would like this to be done in a separate...
7
by: Ken Allen | last post by:
I have a .net client/server application using remoting, and I cannot get the custom exception class to pass from the server to the client. The custom exception is derived from ApplicationException...
19
by: Jaime Stuardo | last post by:
Hi all.. I have created a business logic component that is used from my ASP.NET webform. It works, but connection string to the database is hard coded, as in this method : public DataSet...
5
by: Sakharam Phapale | last post by:
Hi All, I am using an API function, which takes file path as an input. When file path contains special characters (@,#,$,%,&,^, etc), API function gives an error as "Unable to open input file"....
0
by: Iain McIntosh | last post by:
Hello if anyone can help me with this I will be very grateful. I have a working version of this program as a windows application, when I try to port it over to c# ASP.NET I can't make it work. ...
7
by: =?Utf-8?B?YmVyaWNr?= | last post by:
New to this, I used to pass an array like this function BytesToString(byref myarray() as byte, somethingelse as long) as long and m = BytesToString(fooBar(), bluenose) This would send...
13
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper in C# for an SDK that has been supplied as a .LIB file and a .h header file. I have got most of the functions to work but am really struggling with the...
2
by: 1qaz2wsx | last post by:
Hello reader, On my site i pass variables from one page to another, this is no problem. But when i'll get a string with a + or & sign for example this NE SO 1.1 + 1.2 string I will lose the +...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.