Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP Form Mail Script is not sending all of the data submitted

Posted on 2014-03-20
4
Medium Priority
?
744 Views
Last Modified: 2014-03-25
Two things are happening here:

http://domgarofalo.com/mail4.php

1. The page says, 'Your mail was sent successfully' even when the page loads and no mail has been sent.

2. Not all of my data that I enter is submitted in the email.

Attached is the form.
mail4.php
0
Comment
Question by:dgarofalo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 27

Assisted Solution

by:Cornelia Yoder
Cornelia Yoder earned 400 total points
ID: 39944232
I don't know that this is wrong, but I've never seen mail() used in this way.  Have you tried simply

<?php
 $to = "domgarofalo@gmail.com";
 $subject = "Contact Us";
 $name = $_REQUEST['name'] ;
 $email = $_REQUEST['email'] ;
 $message = $_REQUEST['message'] ;
 mail($to, $subject, $name, $email, $message) ;



<form action="mail4.php" name="MYFORM" id="MYFORM">
<input name="name" size="30" type="text" id="cname" class="span12" placeholder="Name" required><br />
<input name="email" size="30" type="text" id="email" class="span12" placeholder="Email" required><br />
<textarea id="message" name="message" class="span12" placeholder="Message" rows="8"></textarea><br />
<input value="Send" type="submit" id="Send" class="btn btn-primary top10">
 </form>


As far as I know, there is no error return from this function, so your checking $sent is probably not doing anything.

What data is missing?
0
 

Author Comment

by:dgarofalo
ID: 39944256
Name comes through and so does email, but the text I enter for the message does not.
0
 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 400 total points
ID: 39944266
You're formatting it wrong.  mail() has only 4 parameters.  This is what it should look like.  $message is the third parameter.  The 'From:' goes in the headers.
<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

Open in new window


http://us2.php.net/manual/en/function.mail.php
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1200 total points
ID: 39945467
Email seems simple, but it is not.  Don't try to do this sort of thing yourself.  The internet is littered with failed PHP+email projects, and it looks like you may have copied one of the failures.

If all you want is to send an email to yourself, the examples here will show you what you need to know.  You can probably get away with just using three variables.
http://php.net/manual/en/function.mail.php

If you want to send emails to a broader audience, consider using something like this:
http://search.constantcontact.com/

If you want a teaching example of a form-to-email script, this may be helpful.  It is untested code, but should be about right, at least in principle.

<?php // demo/form_to_email.php
error_reporting(E_ALL);


// SEND MAIL FROM A FORM


// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from = 'NoReply@Your.org';
$subj = 'Contact Form';

// THIS IS AN ARRAY OF RECIPIENTS - CHANGE THESE FOR YOUR WORK
$to[] = 'You@Your.org';
$to[] = 'Her@Your.org';
$to[] = 'Him@Your.org';
$to[] = 'Ray.Paseur@Gmail.com';


// IF THE DATA HAS BEEN POSTED (AT LEAST SOMETHING IN THE EMAIL INPUT CONTROL)
if (!empty($_POST['e']))
{
    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
    $safe_mail = clean_string($_POST['e']);
    $safe_name = clean_string($_POST['n']);
    $safe_fone = clean_string($_POST['t']);
    $safe_idea = clean_string($_POST['i']);

    // CONSTRUCT THE MESSAGE THROUGH STRING CONCATENATION
    $content = NULL;
    $content .= "You have a New Query From $safe_name" . PHP_EOL . PHP_EOL;
    $content .= "Tel No: $safe_fone" . PHP_EOL;
    $content .= "Email: $safe_mail" . PHP_EOL;
    $content .= "Idea: $safe_idea" . PHP_EOL;

    // SEND MAIL TO EACH RECIPIENT
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo PHP_EOL . "<br/>MAIL FAILED FOR $recipient";
        }
        else
        {
            echo PHP_EOL . "<br/>MAIL WORKED FOR $recipient";
        }
    }

    // PRODUCE THE THANK-YOU PAGE
    echo '<p>THANK YOU</p>' . PHP_EOL;
    die('TASK COMPLETE');
}


// A FORM TO TAKE CLIENT INPUT FOR THIS SCRIPT
$form = <<<ENDFORM
<form method="post">
Please enter your contact information
<br/>Email: <input name="e" />
<br/>Phone: <input name="t" />
<br/>Name:  <input name="n" />
<br/>Ideas? <textarea name="i"></textarea>
<br/><input type="submit" />
</form>
ENDFORM;

echo $form;


// A FUNCTION TO CLEAN UP THE DATA - AVOID BECOMING AN OPEN-RELAY FOR SPAM
function clean_string($str)
{
    // IF MAGIC QUOTES IS ON, WE NEED TO REMOVE SLASHES
    $str = stripslashes($str);

    // REMOVE EXCESS WHITESPACE
    $rgx
    = '#'                // REGEX DELIMITER
    . '\s'               // MATCH THE WHITESPACE CHARACTER(S)
    . '\s+'              // MORE THAN ONE CONTIGUOUS INSTANCE OF WHITESPACE
    . '#'                // REGEX DELIMITER
    ;
    $str = preg_replace($rgx, ' ', $str);

    // REMOVE UNWANTED CHARACTERS
    $rgx
    = '#'                // REGEX DELIMITER
    . '['                // START OF A CHARACTER CLASS
    . '^'                // NEGATION - MATCH NONE OF THE CHARACTERS IN THIS CLASS
    . 'A-Z0-9'           // KEEP LETTERS AND NUMBERS
    . '"'                // KEEP DOUBLE QUOTES
    . "'"                // KEEP SINGLE QUOTES
    . '@&+:?_.,/\-'      // KEEP SOME SPECIAL CHARACTERS (ESCAPED HYPHEN)
    . ' '                // KEEP BLANKS
    . ']'                // END OF THE CHARACTER CLASS
    . '#'                // REGEX DELIMITER
    . 'i'                // CASE-INSENSITIVE
    ;
    $str = preg_replace($rgx, NULL, $str);

    // REMOVE EXCESSIVE INPUT
    $str = substr($str, 0, 255);

    return trim($str);
}

Open in new window

Best regards, ~Ray
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

688 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