0

I have tried to make a simple PHP contact form, but it turned out that I needed to use SMTP and PHPMailer because it's hosted on a windows server. I have set up a contact form using the example document on the PHPMailer website. I am having trouble pulling the data from my form. The email does send but in the body it shows up as:

> Name: $name
> 
> Email: $email
> 
> Message: $comment

I know there are lots of questions about this on here Stackoverflow, but I can't seem to figure it out.


PHP


<?php
require 'PHPMailer/PHPMailerAutoload.php';

$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$comment = $_REQUEST['comment'];

$mail = new PHPMailer;

$mail->SMTPDebug = 2;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'mail.example.co.uk';  // Specify main and backup SMTP servers
$mail->SMTPAuth = false;                               // Enable SMTP authentication
$mail->Username = 'test@example.co.uk';                 // SMTP username
$mail->Password = 'password';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25;                                    // TCP port to connect to

$mail->From = 'test@example.co.uk';
$mail->FromName = 'Mailer';
$mail->addAddress('test@example.co.uk', 'Bob');     // Add a recipient
//$mail->addAddress('ellen@example.com');               // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');

//$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML



$mail->Subject = 'Here is the subject';
$mail->Body    = '<p><strong>Name:</strong> $name</p>
<p><strong>Email:</strong> $email</p>
<p><strong>Message:</strong> $comment</p>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';





if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
?>

HTML


<form role="form" method="post" action="mail.php">

        <p><label for="name">Name</label></p>
        <p><input type="text"  id="name" name="name" placeholder="First &amp; Last Name" value=""></p>

        <p><label for="subject" >Subject</label></p>
        <p><input type="text" id="subject" name="subject" placeholder="Subject" value=""></p>


        <p><label for="email" >Email</label></p>
        <p><input type="email" id="email" name="email" placeholder="example@domain.com" value=""></p>


        <p><label for="comment">Message</label></p>
        <p><textarea rows="4" id="comment" name="comment"></textarea></p>


        <p><input id="submit" name="submit" type="submit" value="Send"></p> 

  </form>

jonathanl5660
  • 171
  • 1
  • 1
  • 12
  • 1
    Could you please provide and MCVE? http://stackoverflow.com/help/mcve – Kalle Jul 08 '15 at 09:32
  • This is nothing to do with PHPMailer - you just need to learn [basic PHP syntax](http://be2.php.net/manual/en/language.types.string.php) for quoting in strings. – Synchro Jul 08 '15 at 09:49

1 Answers1

1

Your code for $mail->body is:

$mail->Body    = '<p><strong>Name:</strong> $name</p>
<p><strong>Email:</strong> $email</p>
<p><strong>Message:</strong> $comment</p>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

Just replace with below code :

$mail->Body    = '<p><strong>Name: </strong>'.$name.'</p>
<p><strong>Email: </strong>'.$email.'</p>
<p><strong>Message: </strong>'.$comment.'</p>';
$mail->AltBody = ' This is the body in plain text for non-HTML mail clients';
Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33