Jump to content

Help with PHP Mailer Contact form undefined variable


Ricky55
Go to solution Solved by maxxd,

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 = "name@hotmail.com";
   $mail->Password = "xxx";
    
   $mail->From     = "name@hotmail.com";
   $mail->FromName = "Richard Dale";
   $mail->AddAddress("name@hotmail.com", "Rachel Recipient");
   $mail->AddReplyTo("name@hotmail.com", "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\">";
   }
?>

Link to comment
Share on other sites

  • Solution

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";
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.