[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 507
  • Last Modified:

PHP Send Mail Help

I have a form that collects all the data but it seems like the send mail part ain;t sending it to my email. Does this look good to send mail using php.


<?php


$name = $POST['name'];
$email = $POST['email'];
$city = $POST['city'];
$state = $POST['state'];
$phone = $['phone'];
$dancer = $POST['dancer'];
$dancer2 = $POST['dancer2'];
$maledancer = $POST['maledancer'];
$comment = $POST['comment'];


$headers    = "Content-Type: text/plain; charset=iso-8859-1\n";


$recipient  = "someemail@gmail.com";

$subject    = "Booking";

$message    = wordwrap($name, $email, $city, $state, $phone, $dancer, $dancer2, $maledancer, $comment);





mail($recipient, $subject, $message, $headers);






?>
0
smphil
Asked:
smphil
  • 6
  • 5
  • 4
  • +2
1 Solution
 
gr8gonzoConsultantCommented:
1. You don't need the $headers parameter if you're just sending plain-text email.

2. Your wordwrap() syntax is wrong. If you want one element on each line, you don't need wordwrap() at all. Instead, do this:

$message = $name . "\n" . $email . "\n" . $city . "\n" . $state . "\n" . $phone . "\n" . $dancer . "\n" . $dancer2 . "\n" . $maledancer . "\n" . $comment;
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
What are the values coming from $_POST GLOBAL ?
They may be causing a problem .Try setting them directly for testing.


If i don't remember wrong charset should be in double quotes so;

Change this line
$headers    = "Content-Type: text/plain; charset=iso-8859-1\n";
to
$headers    = 'Content-Type: text/plain; charset="iso-8859-1" \r\n ';

Other posiblities are ;

- Your server's mail configuration is faulty.
- Your php.ini settings are wrong
- Your recipient is blocking your mail
0
 
smphilAuthor Commented:
What could be the setting problem on the php.ini
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Dave BaldwinFixer of ProblemsCommented:
I'm sorry but many SMTP servers Require the headers in order to send email.  Here is my PHP email demo that has worked everywhere that has a SMTP server configured.  Save it as 'Email.php' so it can POST to itself.  Put your email address in $toText and try it.

<?php
error_reporting(E_ALL);
ini_set('display_errors','1');

# 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'])) $ccText = ''; 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";
    $announce="---";
}
else {
	if($fromText === "") die("No name!");
  $toText="youremail@yourdomain.com";
	$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);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<head>
<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;}
-->
</style>
<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";
    submitOK="false"
    //return false;
    }
if (nam.length < 3)
    {
    alerttxt=alerttxt+"Please enter your name.\r\n";
    submitOK="false"
    //return false;
    }
if (submitOK=="false")
    {
    alert(alerttxt);
    return false;
    }

}
// -->
</script>
</head>

<body bgcolor="#ddeedd">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" summary="" width="580">
<tr><td align="center">

<?php
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">
  </form>
  <b><font face="Arial" size="4" color="#e00000"><?php echo($announce)?></font></b><br><br>

</font>
</td></tr>
</table> 
</div>

</body>
</html>

Open in new window

0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
default config mail  section should seem like this - for windows
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

Open in new window


maybe you had changed these , just check
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
DaveBaldwin : i think the second $_POST['ccText'] line is repeating the one above without a reason , is there anything that i cant see ?

BTW nice workaround :)
0
 
gr8gonzoConsultantCommented:
@Dave, I'm not sure if I agree with "many SMTP servers require the headers..." - I've used header-less mail() calls with just about every major SMTP server out there, from Exchange 2000 - 2007 to qmail to exim to postfix to standard sendmail. I've never had any of them require any additional headers beyond what PHP requires and injects by default.

Don't get me wrong, headers are a good thing - I'm just trying to eliminate complexity here. The OP has a pretty simple mail call, and it doesn't look like he needs the additional headers for what he's doing.

@smphil - please just try my suggestions first before trying to dive into the php.ini file or other more complex solutions. If you're still not getting email, then try using a different email address (like a free one from hotmail or gmail) and see if that works.  That should give you an idea of what the real problem is if there still isn't email after that.
0
 
Dave BaldwinFixer of ProblemsCommented:
@gr8gonzo, several PHP email problems here have been solved by adding the correct headers.

@erdincgc, my demo is several years old, has been posted here maybe 100 times, and you're the first to mention that duplicate line.!
0
 
gr8gonzoConsultantCommented:
@Dave - just for the sake of my own curiosity and learning, do you have any links to those questions? I've been doing a lot of PHP and emailing work for years and for all of my most basic emails (the ones that don't go out to customers), the mail() command has worked flawlessly without the parameter for plaintext emails. I've only added it when turning something into an HTML email or when I needed to specify the character set.

(Since tone and body language is lost over email, I am being sincere about being curious and in asking my question.)
0
 
Dave BaldwinFixer of ProblemsCommented:
No, no links, I can never find anything in my question lists anymore.  I don't think the site programmers ever have to do any searching here.
0
 
smphilAuthor Commented:
Dave
I tried your email script and it worked fine the only problem is I need to 3 drop down menus dancer,dancer2 and maledancer in your script and 3 more fields for for city and state and phone along with the email, name and comment field you have.My problem is I have no clue how to edit that script with the validations. If you could edit it I would appreciate it and if I could have permission to use it.

Phil
0
 
Dave BaldwinFixer of ProblemsCommented:
@smphil, maybe you need to hire a professional developer at this point.  This is not a complicated problem but as usual, you do need to know how to handle the details.  You can use whatever you want from my demo code but I have too much other work to re-write this for you.
0
 
Ray PaseurCommented:
<?php // RAY_temp_smphil.php
error_reporting(E_ALL);
session_start();

// YOUR EMAIL ADDRESS
$you = 'You@Your.org';

// THESE ARE THE FIELDS WE COLLECT
$fields = array
( 'name'       => 'Name?'
, 'email'      => 'Email?'
, 'city'       => 'City?'
, 'state'      => 'State?'
, 'phone'      => 'Phone?'
, 'dancer'     => 'First Dancer?'
, 'dancer2'    => 'Second Dancer?'
, 'maledancer' => 'Male Dancer?'
, 'comment'    => 'Comments?'
)
;

// IF THE FORM WAS SUBMITTED
if (!empty($_POST))
{
    if ($_POST['color'] == $_SESSION['color'])
    {
        // COLLECT THE POST VARIABLES
        $out = NULL;
        foreach ($fields as $key => $value)
        {
            $out .= $value . ': ' . $_POST[$key] . PHP_EOL;
        }

        // SEND THE MESSAGE
        mail($you, 'Booking', $out);
        echo 'Thanks!';
        die();
    }
    else
    {
        sleep(4);
        die('Please try again.  The color did not match');
    }
}

// CREATE THE FORM
$colors = array
( 'red'
, 'orange'
, 'yellow'
, 'green'
, 'blue'
, 'indigo'
, 'violet'
)
;
$x = $colors;
shuffle($x);
$_SESSION['color'] = current($x);

// CREATE THE FORM
$form = 'Please fill in this form: <form method="post">';

foreach ($fields as $key => $value)
{
    $form
    .= $value
    . '<input name="'
    . $key
    . '" />'
    . '<br/>'
    . PHP_EOL
    ;
}

// ADD THE COLOR CAPTCHA TEST
$form .= 'Choose "' . $_SESSION['color'] . '" here:<br/>' . PHP_EOL;
foreach ($colors as $color)
{
    $form
    .= '<input name="color" type="checkbox" value="'
    . $color
    . '" />'
    . $color
    . '<br/>'
    . PHP_EOL
    ;
}

// ADD THE SUBMIT BUTTON
$form .= '<input type="submit" />';
$form .= '</form>';
echo $form;

Open in new window

HTH, ~Ray
0
 
smphilAuthor Commented:
RAY
Wow thank you very much could you just add in a select menu and I will populate it just remove the first dancer and make it a select menu. And I will award you the points if you can. Thank you very much.

Phil
0
 
Ray PaseurCommented:
What would the select menu be?  What selections and should it allow for multiples?
0
 
smphilAuthor Commented:
The select menu should have 10 selections and yes it can allow for multiple.
0
 
Ray PaseurCommented:
OK, and what would those selections be?
0
 
smphilAuthor Commented:
The selections are
arrianna
alexia
taylor
jade
jenna
allison
denise
dana
0
 
Ray PaseurCommented:
Maybe something like this.  I have not tested this very thoroughly, but I think it may work.

<?php // RAY_temp_smphil.php

error_reporting(E_ALL);
session_start();

// YOUR EMAIL ADDRESS
$you = 'Ray.Paseur@Gmail.com';

// THESE ARE THE FIELDS WE COLLECT BY SELECTION
$dancers = array
( 'Choose Dancer' => 'No Choice'
, 'arrianna'      => 'arrianna'
, 'alexia'        => 'alexia'
, 'taylor'        => 'taylor'
, 'jade'          => 'jade'
, 'jenna'         => 'jenna'
, 'allison'       => 'allison'
, 'denise'        => 'denise'
, 'dana'          => 'dana'
)
;

// CREATE THE SELECTION
$select = '<select name="dancer[]" multiple="multiple">' . PHP_EOL;
foreach ($dancers as $key => $dancer)
{
    $select .= '<option value="' . $dancer . '">' . $key . '</option>' . PHP_EOL;
}
$select .= '</select><br/>' . PHP_EOL;

// THESE ARE THE FIELDS WE COLLECT IN TEXT
$fields = array
( 'name'       => 'Name?'
, 'email'      => 'Email?'
, 'city'       => 'City?'
, 'state'      => 'State?'
, 'phone'      => 'Phone?'
, 'dancer'     => $select
, 'dancer2'    => 'Second Dancer?'
, 'maledancer' => 'Male Dancer?'
, 'comment'    => 'Comments?'
)
;

// IF THE FORM WAS SUBMITTED
if (!empty($_POST))
{
    if ($_POST['color'] == $_SESSION['color'])
    {
        // COLLECT THE POST VARIABLES
        $out = NULL;
        foreach ($fields as $key => $value)
        {
            if (is_array($_POST[$key]))
            {
                foreach ($_POST[$key] as $sub_value)
                {
                    $out .= $key . ': ' . $sub_value . PHP_EOL;
                }
            }
            else
            {
                $out .= $value . ': ' . $_POST[$key] . PHP_EOL;
            }
        }

        // SEND THE MESSAGE
        mail($you, 'Booking', $out);
        echo 'Thanks!';
        die();
    }
    else
    {
        sleep(4);
        die('Please try again.  The color did not match');
    }
}

// CREATE THE FORM
$colors = array
( 'red'
, 'orange'
, 'yellow'
, 'green'
, 'blue'
, 'indigo'
, 'violet'
)
;
$x = $colors;
shuffle($x);
$_SESSION['color'] = current($x);

// CREATE THE FORM
$form = 'Please fill in this form: <form method="post">';

foreach ($fields as $key => $value)
{
    if (substr($value,-1) == '?')
    {
        $form
        .= $value
        . '<input name="'
        . $key
        . '" />'
        . '<br/>'
        . PHP_EOL
        ;
    }
    else
    {
        $form .= $value;
    }
}

// ADD THE COLOR CAPTCHA TEST
$form .= 'Choose "' . $_SESSION['color'] . '" here:<br/>' . PHP_EOL;
foreach ($colors as $color)
{
    $form
    .= '<input name="color" type="checkbox" value="'
    . $color
    . '" />'
    . $color
    . '<br/>'
    . PHP_EOL
    ;
}

// ADD THE SUBMIT BUTTON
$form .= '<input type="submit" />';
$form .= '</form>';
echo $form;

Open in new window

0
 
smphilAuthor Commented:
IT WORKS had to make a few changes but all good thank you
Phil
0
 
Ray PaseurCommented:
Thanks for the points, ~Ray
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 6
  • 5
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now