Solved

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

Posted on 2014-03-20
4
707 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
4 Comments
 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 100 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 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 100 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 108

Accepted Solution

by:
Ray Paseur earned 300 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Centering Vertically 3 27
HTML Relative path 9 29
Form Submit falis 6 25
document.getElementById not worj with 2 IDs with the same name 10 16
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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…

914 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

16 Experts available now in Live!

Get 1:1 Help Now