Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

Passing Variable Issue

the mail() function

Moderators: macek, egami, gesf

Passing Variable Issue

Postby ehauser » Mon Oct 03, 2011 5:09 pm

I am having trouble passing variables between two pages and can not see the error myself. Can someone please tell me what I'm doing wrong?

Form.html Page
Code: Select all
<form name="apply" id="apply" method="post" action="email.php">
<table width="100%" border="1">
  <tr>
    <td width="48%"><p><strong>Date of Application:</strong>
      <input name="date2" type="text" id="date2" value="mm/dd/yyyy" size="50" />
      </p>
      <p><strong>Address of Organization:</strong><br />
        <textarea name="address" cols="60" rows="4">123 Road
Suite 1A
City, STATE, Zip</textarea>
      </p>
      <p><strong>Organization Name:</strong>
        <input name="orgname" type="text" id="orgname" size="75" />
      </p>
      <p><strong>Website: </strong>
        <input name="website" type="text" id="website" value="http://www.example.com" size="50" />
        <br />
    </p></td>
    <td width="52%"><p><strong><u>Contact Information:</u></strong></p>
      <p><strong>Primary Contact: </strong><br />
        <input name="NameLast" type="text" id="NameLast" value="Last Name" size="35" />
        <input name="NameFirst" type="text" id="NameFirst" value="First Name" size="35" />
        <input name="Title" type="text" id="Title" value="Title" size="35" />
        <br />
        <input name="Email" type="text" id="Email" value="Email" size="35" />
        <input name="Phone" type="text" id="Phone" value="Phone" size="35" />
        <input name="Fax" type="text" id="Fax" value="Fax" size="35" />
      </p>
      <p><strong>Secondary Contact:<br />
      </strong>
        <input name="NameLast2" type="text" id="NameLast2" value="Last Name" size="35" />
        <input name="NameFirst2" type="text" id="NameFirst2" value="First Name" size="35" />
        <input name="Title2" type="text" id="Title2" value="Title" size="35" />
        <br />
        <input name="Email2" type="text" id="Email2" value="Email" size="35" />
        <input name="Phone2" type="text" id="Phone2" value="Phone" size="35" />
        <input name="Fax2" type="text" id="Fax2" value="Fax" size="35" />
        <strong><u><br />
        <br />
        <br />
    </u></strong></p></td>
  </tr>
</table>
<br />
  <hr />
  <p><strong><u>Project Information:</u></strong></p>
  <table width="100%" border="1">
    <tr>
      <td width="48%"><p><strong>Explain background of this project and why funds are needed.</strong></p>
        <p><strong>
          <textarea name="projbkgrnd2" cols="75" rows="10" id="projbkgrnd2"></textarea>
      </strong></p></td>
      <td width="52%"><p><strong>What is the objective of this project?</strong></p>
        <p><strong>
          <textarea name="projobjective2" cols="75" rows="10" id="projobjective2"></textarea>
      </strong></p></td>
    </tr>
    <tr>
      <td><p><strong>Please summarize the intention of this donation.</strong></p>
        <p><strong>
          <textarea name="intention2" cols="75" rows="10" id="intention2"></textarea>
      </strong></p></td>
      <td><p><strong>How will you determine that this project was successful?</strong></p>
        <p><strong>
          <textarea name="projsuccess2" cols="75" rows="10" id="projsuccess2"></textarea>
      </strong></p></td>
    </tr>
    <tr>
      <td><p><strong>What is your organization's formal process in determining grant requests?</strong></p>
        <p><strong>
          <textarea name="determinerequests" cols="75" rows="10" id="determinerequests"></textarea>
      </strong></p></td>
      <td><p><strong>How did you hear about our foundation?</strong></p>
        <p><strong>
          <textarea name="hearaboutus" cols="75" rows="10" id="hearaboutus"></textarea>
      </strong></p></td>
    </tr>
  </table>
  <hr />
  <p><strong><u>Application Information:</u></strong><br />
    <br />
  </p>
  <table width="100%" border="1">
    <tr>
      <td width="48%"><strong>Total Annual Budget:</strong>
      <input name="Budget" type="text" id="Budget" value="$" size="30" />
      <br />
      <strong>Amount Being Requested: </strong>
      <input name="Request3" type="text" id="Request" value="$" size="30" />
      <br />
      <strong>Administrative Costs</strong>:
      <input name="AdminCosts" type="text" id="AdminCosts" value="$" size="30" />
      <br /></td>
      <td width="52%"><strong>Next Year's Projected Operating Budget:</strong>
      <input name="OperatingBudget" type="text" id="OperatingBudget" value="$" size="30" />
      <br />
      <strong>Fundraising Costs:</strong>
      <input name="FundCosts" type="text" id="FundCosts" value="$" size="30" />
      <br />
      <strong>Have you received previous funding from R&amp;RFFCF?</strong>
      <label>
        <select name="previousfunding" id="previousfunding">
        <option selected></option>
          <option value="Y">Yes</option>
          <option value="N">No</option>
          </select>
      </label>
<br /><input type="hidden" name="FORM2" id="FORM2" /></td>
    </tr>
  </table>
<p>
          <input type="submit" name="Submit" value="Submit" />
          <input name="Reset" type="reset" id="Reset" value="Reset" /></p>
        </form>


Email.php Page
Code: Select all
// DECLARE VARIABLES
 //$date2 = $_POST['date2'];
 $address = $_POST['address'];
 $orgname = $_POST['orgname'];
 $website = $_POST['website'];
 $NameLast = $_POST['NameLast'];
 $NameFirst = $_POST['NameFirst'];
 $Title = $_POST['Title'];
 $Email = $_POST['Email'];
 $Phone = $_POST['Phone'];
 $Fax = $_POST['Fax'];
 $NameLast2 = $_POST['NameLast2'];
 $NameFirst2 = $_POST['NameFirst2'];
 $Title2 = $_POST['Title2'];
 $Email2 = $_POST['Email2'];
 $Phone2 = $_POST['Phone2'];
 $Fax2 = $_POST['Fax2'];
 $projbkgrnd2 = $_POST['projbkgrnd2'];
 $projobjective2 = $_POST['projobjective2'];
 $intention2 = $_POST['intention2'];
 $projsuccess2 = $_POST['projsuccess2'];
 $determinerequests = $_POST['determinerequests'];
 $hearaboutus = $_POST['hearaboutus'];
 $Budget = $_POST['Budget'];
 $Request3 = $_POST['Request3'];
 $AdminCosts = $_POST['AdminCosts'];
 $OperatingBudget = $_POST['OperatingBudget'];
 $FundCosts = $_POST['FundCosts'];
 $previousfunding = $_POST['previousfunding'];
 // END DECLARE
 
 $to = "erik@erikhauser.com";
 $subject = "GRANT APPLICATION FORM SUBMISSION";
 $email = $_POST['Email'];
 $message = 'Date: '.$_POST['date2'];
 //$headers = "From: $email"; $sent = mail($to, $subject, $message, $headers); if($sent) {print "Your mail was sent successfully.";} else {print "We encountered an error sending your mail";} echo '<a href="http://www.foosfoundation.com">Back Home</a>'; 
ehauser
New php-forum User
New php-forum User
 
Posts: 39
Joined: Fri Sep 23, 2011 5:02 am
Location: U.S.A.

Re: Passing Variable Issue

Postby KelvinLee18 » Mon Oct 03, 2011 11:56 pm

What are you trying to do actually? I've just tested your code in my local env and it works great.
Are you trying to send an email consisting of the inputs?
KelvinLee18
New php-forum User
New php-forum User
 
Posts: 36
Joined: Sat Oct 01, 2011 4:44 pm

Re: Passing Variable Issue

Postby ehauser » Tue Oct 04, 2011 2:55 am

Yes I want it to send an email with all of the users inputs. I have included only one of them here for testing purposes.
ehauser
New php-forum User
New php-forum User
 
Posts: 39
Joined: Fri Sep 23, 2011 5:02 am
Location: U.S.A.

Re: Passing Variable Issue

Postby KelvinLee18 » Tue Oct 04, 2011 2:58 am

Your code looks fine. Any errors show up when you execute the code?
KelvinLee18
New php-forum User
New php-forum User
 
Posts: 36
Joined: Sat Oct 01, 2011 4:44 pm

Re: Passing Variable Issue

Postby ehauser » Tue Oct 04, 2011 4:54 am

When I tested it before, the email sent fine but the message was blank. I then commented out the send mail function and echoed just one variable that I was putting into the message to see if I was getting data through the variable. When I test it now with something like:

Code: Select all
echo $message;
where $message equals something like
Code: Select all
$message = 'Date: '.$_POST['date2']
ehauser
New php-forum User
New php-forum User
 
Posts: 39
Joined: Fri Sep 23, 2011 5:02 am
Location: U.S.A.

Re: Passing Variable Issue

Postby KelvinLee18 » Tue Oct 04, 2011 5:32 am

Try getting rid of the $header
KelvinLee18
New php-forum User
New php-forum User
 
Posts: 36
Joined: Sat Oct 01, 2011 4:44 pm

Re: Passing Variable Issue

Postby ehauser » Tue Oct 04, 2011 6:03 am

I haven't tried getting rid of the header yet, but here is a more updated version of the code I am using for the php mail functionality...

$to = "erik@erikhauser.com";
$subject = "GRANT APPLICATION FORM SUBMISSION";
$email = $_POST['Email'];
$message = 'Date: '.$_POST['date2'];
$headers = 'From: '.$_POST['Email'];
$sent = mail($to, $subject, $message, $headers); if($sent) {print "Your mail was sent successfully.";} else {print "We encountered an error sending your mail";} echo '<a href="http://www.foosfoundation.com">Back Home</a>';


It sends an email correctly. All I receive in the body of the email is the word "Date:" as I have above. The date the user input should be showing up after that like "Date: 10/4/11" but it shows up like "Date:".

Besides taking out the header like previously asked, am I missing something here still?
ehauser
New php-forum User
New php-forum User
 
Posts: 39
Joined: Fri Sep 23, 2011 5:02 am
Location: U.S.A.

Re: Passing Variable Issue

Postby KelvinLee18 » Tue Oct 04, 2011 6:26 am

Well, forget about the $header thingy. Would you mind doing a var_dump($_POST) and post the results for me here?

** EDIT **
Ah, now i see the problem.
Uncomment the line $date2 = $_POST['date2']; :D
KelvinLee18
New php-forum User
New php-forum User
 
Posts: 36
Joined: Sat Oct 01, 2011 4:44 pm

Re: Passing Variable Issue

Postby ehauser » Tue Oct 04, 2011 6:53 am

Thanks so much for all of your help! I actually got it working with one variable. Now the problem I am running into is how to pass multiple variables in the $message section. I can't use a comma to seperate them so I am wondering what to do. Do I use an array?

Here is the code I have so far:
Code: Select all
// MAIL VARIABLES
 $to = "erik@erikhauser.com";
 $subject = "GRANT APPLICATION FORM SUBMISSION";
 $message = 'Date: '.$_POST['date'].'\r\n';
 $headers = 'From: '.$_POST['Email'];
 $sent = mail($to, $subject, $message);
 if($sent) {print "Your mail was sent successfully.";} else {print "We encountered an error sending your mail";} echo '<a href="http://www.foosfoundation.com">Back Home</a>';

// VARIABLES TO GO IN MESSAGE SECTION BELOW"

 'Address: '.$_POST['address'].'\r\n',
 'Organization Name: '.$_POST['orgname'].'\r\n',
 'Website: '.$_POST['website'].'\r\n',
 'PRIMARY CONTACT SECTION\r\n'
 'Last Name: '.$_POST['NameLast'].'\r\n',
 'First Name: '.$_POST['NameFirst'].'\r\n',
 'Title: '.$_POST['Title'].'\r\n',
 'Email: '.$_POST['Email'].'\r\n',
 'Phone: '.$_POST['Phone'].'\r\n',
 'Fax: '.$_POST['Fax'].'\r\n',
 'SECONDARY CONTACT SECTION\r\n'
 'Last Name 2: '.$_POST['NameLast2'].'\r\n',
 'First Name 2: '.$_POST['NameFirst2'].'\r\n',
 'Title 2: '.$_POST['Title2'].'\r\n',
 'Email 2: '.$_POST['Email2'].'\r\n',
 'Phone 2: '.$_POST['Phone2'].'\r\n',
 'Fax 2: '.$_POST['Fax2'].'\r\n',
 'PROJECT INFORMATION SECTION\r\n'
 'Project Background: '.$_POST['projbkgrnd2'].'\r\n',
 'Project Objective: '.$_POST['projobjective2'].'\r\n',
 'Intention: '.$_POST['intention2'].'\r\n',
 'Project Success: '.$_POST['projsuccess2'].'\r\n',
 'Determine Requests: '.$_POST['determinerequests'].'\r\n',
 'Hear About Us: '.$_POST['hearaboutus'].'\r\n',
 'APPLICATION INFORMATION SECTION\r\n'
 'Budget: '.$_POST['Budget'].'\r\n',
 'Request: '.$_POST['Request3'].'\r\n',
 'Administrative Costs: '.$_POST['AdminCosts'].'\r\n',
 'Operating Budget: '.$_POST['OperatingBudget'].'\r\n',
 'Funding Costs: '.$_POST['FundCosts'].'\r\n',
 'Previous Funding: '.$_POST['previousfunding'].'\r\n';
ehauser
New php-forum User
New php-forum User
 
Posts: 39
Joined: Fri Sep 23, 2011 5:02 am
Location: U.S.A.

Re: Passing Variable Issue

Postby egami » Tue Oct 04, 2011 7:21 am

$message = "Hello";
$message .= " ";
$message .= "World!";

echo $message
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Passing Variable Issue

Postby ehauser » Tue Oct 04, 2011 7:26 am

Ahh... I was unaware that you can set a variable like that to different things.

Oh I see you're concatenating it...

Thanks!
ehauser
New php-forum User
New php-forum User
 
Posts: 39
Joined: Fri Sep 23, 2011 5:02 am
Location: U.S.A.

Re: Passing Variable Issue

Postby ehauser » Tue Oct 04, 2011 10:44 am

Is there another way to set the MIME-Version and the Content-type without using $headers? My email code works fine, until I add in the headers...

Code: Select all
$headers = 'From: '.$_POST['Email'];
 $headers .= "MIME-Version: 1.0"."\r\n";
 $headers .= "Content-type:text/html;charset=iso-8859-1"."\r\n";
 $sent = mail($to, $subject, $message, $headers);


I even tried commenting out the "From" part of the headers. It tells me the mail sent successfully but I never get the email.
ehauser
New php-forum User
New php-forum User
 
Posts: 39
Joined: Fri Sep 23, 2011 5:02 am
Location: U.S.A.

Re: Passing Variable Issue

Postby egami » Tue Oct 04, 2011 12:21 pm

you don't have a return / new-line at the end of your FROM.
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Passing Variable Issue

Postby egami » Tue Oct 04, 2011 12:24 pm

I would also try to follow the RFC 2822 standards of SMTP protocol, and put the 'from' after the other headers.

For further help, try http://www.php.net/mail

:)

enjoy.
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Passing Variable Issue

Postby ehauser » Tue Oct 04, 2011 12:34 pm

Thanks so much! I've tried putting the "From" after the other headers... but I try putting the new line at the end of the "From" and see what happens. The form sends just fine if I comment/take out the headers. We will see what happens.

Thanks again!
ehauser
New php-forum User
New php-forum User
 
Posts: 39
Joined: Fri Sep 23, 2011 5:02 am
Location: U.S.A.

Re: Passing Variable Issue

Postby ehauser » Tue Oct 04, 2011 12:49 pm

I have tried the suggestions and the script says the email has been sent successfully but I never receive the email. I have not checked my spam but I'm sending the email internally on my server so I would think it would come straight to me.

Again, if I take out the headers, the email sends just fine.

Here is the full code to send the email:

Code: Select all
// MAIL VARIABLES
 $to = "erik@mysite.com";
 $subject = "GRANT APPLICATION FORM SUBMISSION";
 $message = 'FORM INFORMATION'."\r\n";
 $message .= ' '."\r\n";
 $message .= 'Date Submitted: '.$_POST['date']."\r\n";
 $message .= 'Address: '.$_POST['address']."\r\n";
 $message .= 'Organization Name: '.$_POST['orgname']."\r\n";
 $message .= 'Website: '.$_POST['website']."\r\n";
 $message .= ' '."\r\n";
 $message .= 'PRIMARY CONTACT SECTION'."\r\n";
 $message .= ' '."\r\n";
 $message .= 'Last Name: '.$_POST['NameLast']."\r\n";
 $message .= 'First Name: '.$_POST['NameFirst']."\r\n";
 $message .= 'Title: '.$_POST['Title']."\r\n";
 $message .= 'Email: '.$_POST['Email']."\r\n";
 $message .= 'Phone: '.$_POST['Phone']."\r\n";
 $message .= 'Fax: '.$_POST['Fax']."\r\n";
 $message .= ' '."\r\n";
 $message .= 'SECONDARY CONTACT SECTION'."\r\n";
 $message .= ' '."\r\n";
 $message .= 'Last Name 2: '.$_POST['NameLast2']."\r\n";
 $message .= 'First Name 2: '.$_POST['NameFirst2']."\r\n";
 $message .= 'Title 2: '.$_POST['Title2']."\r\n";
 $message .= 'Email 2: '.$_POST['Email2']."\r\n";
 $message .= 'Phone 2: '.$_POST['Phone2']."\r\n";
 $message .= 'Fax 2: '.$_POST['Fax2']."\r\n";
 $message .= ' '."\r\n";
 $message .= 'PROJECT INFORMATION SECTION'."\r\n";
 $message .= ' '."\r\n";
 $message .= 'Project Background: '.$_POST['projbkgrnd2']."\r\n";
 $message .= 'Project Objective: '.$_POST['projobjective2']."\r\n";
 $message .= 'Intention: '.$_POST['intention2']."\r\n";
 $message .= 'Project Success: '.$_POST['projsuccess2']."\r\n";
 $message .= 'Determine Requests: '.$_POST['determinerequests']."\r\n";
 $message .= 'Hear About Us: '.$_POST['hearaboutus']."\r\n";
 $message .= ' '."\r\n";
 $message .= 'APPLICATION INFORMATION SECTION'."\r\n";
 $message .= ' '."\r\n";
 $message .= 'Budget: '.$_POST['Budget']."\r\n";
 $message .= 'Request: '.$_POST['Request3']."\r\n";
 $message .= 'Administrative Costs: '.$_POST['AdminCosts']."\r\n";
 $message .= 'Operating Budget: '.$_POST['OperatingBudget']."\r\n";
 $message .= 'Funding Costs: '.$_POST['FundCosts']."\r\n"; 
 $message .= 'Previous Funding: '.$_POST['previousfunding']."\r\n";
 
// HEADER FIELDS
 $headers  = 'MIME-Version: 1.0' . "\r\n";
 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 $headers .= 'From: <email@email.com>' . "\r\n";

// SEND THE MESSAGE AND GIVE FEEDBACK TO USER
 $sent = mail($to, $subject, $message, $headers);
 if($sent) {print "Thank you, your application has been submitted. Applications can take up to six weeks to process. You will be notified by email of the final status.";} else {print "We encountered an error sending your mail";} echo '<br /><a href="http://www.foosfoundation.com">Back Home</a>';
ehauser
New php-forum User
New php-forum User
 
Posts: 39
Joined: Fri Sep 23, 2011 5:02 am
Location: U.S.A.

Re: Passing Variable Issue

Postby egami » Wed Oct 05, 2011 4:03 am

The sniplet you have sends mail through my system, however; I would encourage you to follow the HTML mail examples on PHP.net
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Passing Variable Issue

Postby ehauser » Wed Oct 05, 2011 5:35 am

Thanks, I had thought about that originally and am now playing with that code.

Just to double-check, am I passing the variables through correctly? I can't test it until tonight, and I wast to make sure I am going about it the right way before I get done with it...

Code: Select all
// multiple recipients
$to  = 'test@example.com' . ', '; // note the comma
$to .= 'test2@example.com';

// subject
$subject = 'APPLICATION FORM SUBMISSION';

// message
$message = '
<html>
<head>
  <title>APPLICATION FORM SUBMISSION</title>
</head>
<body>
  <p><b>SUBMISSION DATA:</b></p>
  <br /><br />
  <p><b><u>Form Information</u></b></p><br />
  <p><b>Date Submitted:</b> <? $_POST["date"] ?><br />
  <b>Address:</b> <? $_POST["address"] ?><br />
  <b>Organization Name:</b> <? $_POST["orgname"] ?><br />
  <b>Website:</b> <? $_POST["website"] ?><br /></p><br />
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Erik <erik@test.com>, Erik Hauser <ehauser@testemail.com>' . "\r\n";
$headers .= 'From: Form Submission <webmaster@test.com>' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
ehauser
New php-forum User
New php-forum User
 
Posts: 39
Joined: Fri Sep 23, 2011 5:02 am
Location: U.S.A.

Re: Passing Variable Issue

Postby egami » Wed Oct 05, 2011 7:50 am

That should do it
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Passing Variable Issue

Postby ehauser » Wed Oct 05, 2011 11:23 am

Thanks for all of your help, much appreciated!
ehauser
New php-forum User
New php-forum User
 
Posts: 39
Joined: Fri Sep 23, 2011 5:02 am
Location: U.S.A.


Return to PHP coding => Mail

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.