Mail via php

Posted on 2011-10-22
Last Modified: 2012-05-12
I'm trying to pass parameters between two php pages and then send email.  I get a parse error on the $subject line on the 2nd page and I'm not sure the parameters from the 1st page are being picked up.  What's my problem????

Here's my code:

From Page1.php:
<a href="Page2.php?ID=$ServiceRequest[ID]&Neighbor=$ServiceRequest[Neighbor]&Date=$ServiceRequest[SvcDate]">I'll Do This!</a>

To  Page2.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Thank You for Volunteering!</title>


$to = "";
$subject = "I'll do " . $ID . " for " . $Neighbor;       
$message = "volunteer for service";
echo "Mail Sent";  
Question by:JudithARyan
    LVL 82

    Accepted Solution

    "HTTP_GET_VARS" is deprecated, should be $_GET[] these days although I would use $_POST myself.  Below is my PHP email demo.  Save it as 'Email.php'.  It post to itself but when you understand how it works, it's easy to split it into two separate files if you want.
    # some settings of POST vars
    if (!isset($_POST['send']))  $send = ''; else $send = $_POST['send'];
    if (!isset($_POST['toText'])) $toText = ''; else $toText = $_POST['toText'];
    if (!isset($_POST['ccText'])) $ccText = ''; else $ccText = $_POST['ccText'];
    if (!isset($_POST['subjectText'])) $subjectText = ''; else $subjectText = $_POST['subjectText'];
    if (!isset($_POST['msgText'])) $msgText = ''; else $msgText = $_POST['msgText'];
    if (!isset($_POST['ccText'])) $ccTexth = ''; else $ccText = $_POST['ccText'];
    if (!isset($_POST['bccText'])) $bccText = ''; else $bccText = $_POST['bccText'];
    if (!isset($_POST['nameText'])) $nameText = ''; else $nameText = $_POST['nameText'];
    if (!isset($_POST['fromText'])) $fromText = ''; else $fromText = $_POST['fromText'];
    if ($send == "") {
        $title="Test Email Page";
    else {
    	if($fromText === "") die("No name!");
    	$title="Test Email Page";
      $announce="Your Message has been Sent!";
    	$header = "From: ".$fromText."\r\n";
    //	$header .= "Cc: ".$ccText."\n";
    	$header .= "Reply-To : ".$fromText."\r\n";
    	$header .= "Return-Path : ".$fromText."\r\n";
    	$header .= "X-Mailer: PHP\r\n";
    	$header .= "MIME-Version: 1.0\r\n";
    	$header .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
    //	ini_set(sendmail_from,$fromText);  
    	mail($toText, $subjectText, $msgText, $header, '-f'.$fromText);
    //	ini_restore(sendmail_from);
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title><?php echo($title)?></title>
    <style type="text/css">
    A:link { color: #999999; }
    A:visited { color: #999999; }
    A:hover {color: #0099ff;}
    <script type="text/javascript">
    function check()
    var at=document.getElementById("fromText").value.indexOf("@");
    var eml=document.getElementById("fromText").value;
    var nam=document.getElementById("nameText").value;
    var alerttxt="";
    var submitOK="true";
    if (eml.length < 5 || at == -1)
        alerttxt=alerttxt+"Please enter a valid e-mail address!\r\n";
        //return false;
    if (nam.length < 3)
        alerttxt=alerttxt+"Please enter your name.\r\n";
        //return false;
    if (submitOK=="false")
        return false;
    // -->
    <body bgcolor="#ddeedd">
    <div align="center">
    <table border="0" cellpadding="0" cellspacing="0" summary="" width="580">
    <tr><td align="center">
    if ($send != "") {
       	echo ("To: ".$toText."<br>\r\nSubject: ".$subjectText."<br>\r\n".$msgText."<br>\r\n".$header);
    <p><b><font color="#000000" size="5">Test Email</font></b></p>
    <font size="4" color="#000000">
      <form method="POST" action="Email.php" onsubmit="return check();">
        <p><font size="3"><b>Name: <input type="text" name="nameText" id="nameText" size="46"></b></font></p>
        <p><font size="3"><b>Email: <input type="text" name="fromText" id="fromText" size="46"></b></font></p>
        <input type="hidden" name="subjectText" value="Web Mail">
        <p><font face="Arial" size="3"><b>Message Text:</b></font></p>
        <p><font face="Arial" size="3"><b><textarea rows="6" name="msgText" cols="60"></textarea></b></font></p>
        <p><font size="3"><b><input type="submit" value="Send" name="send" style="font-family: Arial; font-size: 12pt; font-weight: bold"></b></font></p>
        <input type="hidden" name="state" value="1">
      <b><font face="Arial" size="4" color="#e00000"><?php echo($announce)?></font></b><br><br>

    Open in new window

    LVL 12

    Expert Comment

    This line
    <a href="Page2.php?ID=$ServiceRequest[ID]&Neighbor=$ServiceRequest[Neighbor]&Date=$ServiceRequest[SvcDate]">I'll Do This!</a>

    Open in new window

    looks funky.  Are the three variables buried in the query string?  If so, it should be
    <a href="Page2.php?ID=<?php echo $ServiceRequest['ID']; ?>&Neighbor=<?php echo $ServiceRequest['Neighbor']; ?>&Date=<?php echo $ServiceRequest['SvcDate']; ?>">I'll Do This!</a>

    Open in new window

    Whenever you want to use PHP code, it has to be between <?php  ?>

    In the code for page 2, this line

    Open in new window

    should be

    Open in new window


    Author Comment

    Thanks for your help, I'll try both suggestions and let you know how I did.


    Author Comment

    DaveBaldwin, since the first section of your demo isn't in a <html  .... </html>  page, am I supposed to insert the first section in one of my own pages?  Sorry for my ignorance, I'm really new to php.

    Thanks for your help.  Judith
    LVL 82

    Expert Comment

    by:Dave Baldwin
    No, that is a complete page on it's own.  The PHP runs on the server and the HTML including any generated by PHP gets sent to the browser.  My demo has a form on it that posts the info back to itself on the server where it gets processed and the results sent to your browser.

    Author Closing Comment

    Thanks so much for your help.  Sorry for the delay

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction In this tutorial, I'll explain how to create an animated progress meter in a wireframe prototype developed using Axure RP 7.0 - a leading prototyping tool for designing web sites and software. (For more information about Axure and gett…
    Objective of This Article In 1990’s, when I was a budding software professional, I had a lot of confusion about which stream or technology, I had to choose to build my career. In those days, I had lot of confusion like whether to choose System so…
    The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now