Jump to content

Help with PHP Mailer Contact form undefined variable


Ricky55

Recommended Posts

Hi guys

 

Hoping you can help me with this. I'm very much a front end person, I know HTML, CSS and some JS but when it comes to PHP and very much a newbie.

 

I'm using this code to process a straight forward contact form. I'm using the PHP Mailer as I need to use an external smtp server and the company I use for this provided some basic code.

 

Its working but I'm getting a error flash up before I see the success page that says "Undefined variable: message in send.php on line 25.

 

Why is this happening and how do I fix it?

 

Thanks guys.

 

my code


 
<?php
   require("PHPMailerAutoload.php"); // path to the PHPMailer class.
 
   $after = "/contact/thanks.php"; 
   $oops = "/contact/error.php";
 
   $mail = new PHPMailer();
   $mail->IsSMTP();
   $mail->Mailer = "smtp";
   $mail->Host = "smtpcorp.com"; 
   $mail->Port = "2525"; 
   $mail->SMTPAuth = true;
   
   $mail->Username = "[email protected]";
   $mail->Password = "xxx";
    
   $mail->From     = "[email protected]";
   $mail->FromName = "Richard Dale";
   $mail->AddAddress("[email protected]", "Rachel Recipient");
   $mail->AddReplyTo("[email protected]", "Sender's Name");
 
   $mail->Subject  = "Contact enquiry from the website";
 
   $message .= "How can we help: {$_POST['help']} \n\n";
   $message .= "Name: {$_POST['name']} \n\n";
   $message .= "Email: {$_POST['email']} \n\n";
   $message .= "Telephone: {$_POST['tel']} \n\n";
   $message .= "Message: {$_POST['message']} \n\n";
   $message .= "Address: {$_POST['address']} \n\n";
 
 
   $mail->Body     = $message;
   $mail->WordWrap = 50;  
 
   if(!$mail->Send()) {
echo "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=$oops\">";
   } else {
echo "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=$after\">";
   }
?>

The first time you use $message, it's not defined yet, so you can't concatenate (the period is a concatenation operator if PHP). So, where you've got

$message .= "How can we help: {$_POST['help']} \n\n";

Should be either

$message  = "How can we help: {$_POST['help']} \n\n";

or

$message = "";
$message .= "How can we help: {$_POST['help']} \n\n";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.