passing string from one file to another

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kun

    #1

    passing string from one file to another

    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?
  • Kun

    #2
    Re: passing string from one file to another

    Kun wrote:[color=blue]
    > 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?[/color]

    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

    Comment

    • I V

      #3
      Re: passing string from one file to another

      Kun wrote:[color=blue]
      > 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.[/color]

      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.

      Comment

      • Kun

        #4
        Re: passing string from one file to another

        I V wrote:[color=blue]
        > Kun wrote:[color=green]
        >> 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.[/color]
        >
        > 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.
        >[/color]
        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.e du" size=20>
        </TD></TR>
        <TR><TD>Usernam e:</TD>
        <TD><input type="text" name="Username" size=20>
        </TD></TR>
        <TR><TD>Passwor d:</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.FieldStorag e() #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(ke y):
        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("not hing entered")


        ##mail = open('mail.txt' ,'rb')
        ##msg = MIMEText(mail.r ead())
        ##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(fo rm['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>"""

        Comment

        • I V

          #5
          Re: passing string from one file to another

          Kun wrote:[color=blue]
          > mail currently gets the body from an input box.
          >
          > this is where mail.py gets invoked:[/color]

          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() .

          Comment

          • Kun

            #6
            Re: passing string from one file to another

            I V wrote:[color=blue]
            > Kun wrote:[color=green]
            >> mail currently gets the body from an input box.
            >>
            >> this is where mail.py gets invoked:[/color]
            >
            > 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() .
            >[/color]
            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.

            Comment

            • Ant

              #7
              Re: passing string from one file to another

              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_p ass_on"
              value="bob">) element in the HTML which gets populated by the initial
              cgi script and is then read by mail.py.

              Comment

              • Lawrence D'Oliveiro

                #8
                Re: passing string from one file to another

                In article <e1v58g$3npc$1@ netnews.upenn.e du>, Kun <neurogasm@gmai l.com>
                wrote:
                [color=blue]
                >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.[/color]

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

                Comment

                Working...