Php form not sending notification

I have a newsletter sign up form which isn't quite working correctly.

I need it so the user submit their email, they recieve a 'thanks for registering....' email and another is sent to the website owner ie enquiries@business.co.uk.

Currently with the code below the user gets the email intended for the site owner which just says  "new registration: user email" and the site owner gets nothing?


 <form method="post" action="?">
                  <span class="keepuptodate">Keep up-to-date with the latest from Summit:</span>
                  <input name="email" type="text" class="left5" value="enter your email address" size="23" />
                  <input name="submit" type="submit" value=" " style="width: 110px; height: 28px; border: 0px; outline: 0px; padding: 0px;background: url(images/subscribe.jpg) no-repeat; cursor: pointer;" align="bottom" />
                  <?php 
if ($error) {
	echo '<font color="red">'.$error.'</font>';
}
?>
                  <span class="message">
                    <?php
//set up for email info
$strTo = enquiries@business.co.uk'; //your email here
$strSubject = 'Newsletter Sign up'; //default subject 
$strBody = '';

if ($_POST['submit']) {
	
	$strEmail = trim($_POST['email']);

	//both fields are required 
	if (!$strEmail) {
		$error = 'Please input both field!<br />';
	}	
	//elseif () {//extra validate should be here. eg: validate email address
	//}
	else {
		//prepare mail body
		$strBody = <<<BODY
New registration
Email: {$strEmail}
BODY;

		//send email
		@mail ($strTo, $strSubject, $strBody,
     "From: ".$strTo."\r\n" .
     "Reply-To: ".$strTo."\r\n" .
          "X-Mailer: PHP/" . phpversion());
	 //prepare mail body
$strBody = <<<BODY
Many thanks for subscribing to our Newsletter.

If we can be of assistance in the mean time please do not hesitate to call us 
BODY;

//send email
@mail ($strEmail, $strSubject, $strBody,
"From: ".$strTo."\r\n" .
"Reply-To: ".$strTo."\r\n" .
"X-Mailer: PHP/" . phpversion());



		//display message
		echo 'thanks for registering!';
		
	}
}
?>

Open in new window

BrighteyesDesignAsked:
Who is Participating?
 
Julian HansenCommented:
I believe the question was "why is the site owner not getting a copy of the message" - therefore we can assume the actual sending of the mail is working and behaving as expected.

The problem is addressing and can be found on line 19

$strEmail = trim($_POST['email']);

Open in new window


Only the POSTed email is provided - the siteOwner's address is not being added to this.

The fix is therefore to make the following change

$strEmail = trim($_POST['email']) . ',' . $strTo ;

Open in new window


I believe this will resolve the issue without the author having to do any further coding.
0
 
Julian HansenCommented:
Try changing line 19 to
$strEmail = trim($_POST['email']) . ',' . $strTo ;

Open in new window


The call to mail() has the recipient list as $strEmail  but as far as I can see it is only assigning one address - the Posted address.

Anyone else needs to be added on to this separated by commas
0
 
Marco GasiFreelancerCommented:
Personally, I strongly suggest to use PHPMailer (http://code.google.com/a/apache-extras.org/p/phpmailer/downloads/list): it's really simple send emails to as recipient as you wish:

Cheers
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Marco GasiFreelancerCommented:
Check this out:

 $mailer = new PHPMailer();
 $mailer->IsSMTP(); // set mailer to use SMTP
 $mailer->Host = "mail.yourdomain.com";
 $mailer->SMTPAuth = true;
 $mailer->Username = 'username';
 $mailer->Password = 'password';
 $mailer->SetFrom('your@email.com', 'your name');
 $mailer->AddAddress('to_address', 'to_name');
 $mailer->AddAddress('another_visible_email_address', 'to_name');
 $mailer->AddBCC('another_secret_email_address');
 $mailer->Subject = 'subject';
 $mailer->MsgHTML($message);
 if (!$mailer->Send()) {
   echo "Problem!";
 }else{
  echo "Sent!";
}

Cheers
0
 
Dave BaldwinFixer of ProblemsCommented:
PHPMailer is a good suggestion.  Here also is my PHP email demo.  Save it as 'Email.php' so it can post to itself.  Put your email address in for the $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
 
Ray PaseurCommented:
If the client who registered is receiving the email that is intended for the owner of the site, it sounds like a logic error, and it might be best to hire a professional programmer to refactor (or at least debug) the program code.  We can usually answer questions at EE, but with something like this, I would want to have an accountable party undertake the task.

Not sure if it will help you, but this article shows the design pattern that many sites use for this sort of thing.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_3939-Registration-and-Email-Confirmation-in-PHP.html

Best regards, ~Ray
0
 
Ray PaseurCommented:
This may help illustrate a way to do it.  Just the essential moving parts.

<?php // RAY_temp_brighteye.php
error_reporting(E_ALL);

if (!empty($_POST['email']))
{
    // IF THE EMAIL IS BOGUS
    if (!check_valid_email($_POST['email']))
    {
        $error = 'PLEASE ENTER A VALID EMAIL ADDRESS';
    }

    // IF THE EMAIL IS VALID
    else
    {
        // SEND THE MESSAGE TO THE OWNER
        mail
        ( 'owner@website.com'
        , 'NEW EMAIL REGISTRATION'
        , $_POST['email']
        )
        ;

        // SEND THE GREETING TO THE CLIENT
        mail
        ( $_POST['email']
        , 'WELCOME'
        , 'THANKS FOR REGISTERING'
        )
        ;

        // CREATE THE RESPONSE PAGE
        die('THANKS');
    }
}

// CREATE THE FORM
$form = <<<ENDFORM
<form method="post">
<input name="email" />
<input type="submit" value="Sign Up for Email" />
</form>
ENDFORM;

echo $form;


// A FUNCTION TO TEST FOR A VALID EMAIL ADDRESS, RETURN TRUE OR FALSE
// SEE MAN PAGE: http://php.net/manual/en/intro.filter.php
function check_valid_email($email, $rout=TRUE)
{
    // LIST OF BLOCKED DOMAINS
    $bogus = array
    ( '@unknown.com'
    , '@example.com'
    , '@gooseball.org'
    )
    ;

    // IF PHP 5.2 OR ABOVE, WE CAN USE THE FILTER
    if (strnatcmp(phpversion(),'5.2') >= 0)
    {
        if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) return FALSE;
    }

    // IF LOWER-LEVEL PHP, WE CAN CONSTRUCT A REGULAR EXPRESSION
    else
    {
        $regex
        = '/'                        // START REGEX DELIMITER
        . '^'                        // START STRING
        . '[A-Z0-9_-]'               // AN EMAIL - SOME CHARACTER(S)
        . '[A-Z0-9._-]*'             // AN EMAIL - SOME CHARACTER(S) PERMITS DOT
        . '@'                        // A SINGLE AT-SIGN
        . '([A-Z0-9][A-Z0-9-]*\.)+'  // A DOMAIN NAME PERMITS DOT, ENDS DOT
        . '[A-Z\.]'                  // A TOP-LEVEL DOMAIN PERMITS DOT
        . '{2,6}'                    // TLD LENGTH >= 2 AND =< 6
        . '$'                        // ENDOF STRING
        . '/'                        // ENDOF REGEX DELIMITER
        . 'i'                        // CASE INSENSITIVE
        ;
        // TEST THE STRING FORMAT
        if (!preg_match($regex, $email)) return FALSE;
    }

    // TEST TO SEE IF THE DOMAIN IS IN OUR BLOCKED LIST
    foreach ($bogus as $badguy)
    {
        if (stripos($email, $badguy)) return FALSE;
    }

    // FILTER_VAR OR PREG_MATCH DOES NOT TEST IF THE DOMAIN IS ROUTABLE
    if ($rout)
    {
        $domain = explode('@', $email);

        // MAN PAGE: http://php.net/manual/en/function.checkdnsrr.php
        if ( checkdnsrr($domain[1], "MX") || checkdnsrr($domain[1], "A") ) return TRUE;

        // EMAIL IS NOT ROUTABLE
        return FALSE;
    }
    return TRUE;
}

Open in new window

HTH, ~Ray
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.