Search code examples
phpemailphpmailer

Message body empty in contact form (using PhPMailer)


I'm having a little problem with my contact form.

I'm using PhpMailer and Bootstrap contact form. When I run the code I get this message:

"Uncaught exception 'phpmailerException' with message 'Message body empty'"

This is my code:

$name = $_POST['InputName'];
$company = $_POST['InputFirma'];
$email = $_POST['InputEmail'];
$phone = $_POST['InputPhone'];
$message = $_POST['InputSubject'];

require '../../PHPMailer-master/PHPMailerAutoload.php';
require '../../PHPMailer-master/class.smtp.php';

$mail = new PHPMailer(true);
$mail->SMTPDebug = false;             // Enable verbose debug output
$mail->isSMTP();                      // Set mailer to use SMTP
$mail->Host = 'poczta.cgsa.com.pl';   // Specify main and backup SMTP servers
$mail->SMTPAuth = true;               // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'FU86m6BSp7';       // SMTP password
$mail->Port = 587;

$mail->setFrom('[email protected]', 'Giełd');
$mail->addAddress('[email protected]', 'Odbiorca'); // Add a recipient

$mail->isHTML(true);                               // Set email format to HTML


if(!$mail->send()) {
    echo 'Wiadomość nie mogła zostać wysłana';
    echo "<br><br><br><hr><br>";
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Wiadomość została wysłana';
}

$Body = "Wiadomość od: $name\n E-Mail: $email\n Firma: $company\n";

$success = mail($name, $company, $phone, $message);

This is my HTML:

<form class="padding-top-40" role="form" id="contactForm" class="contact-form" data-toggle="validator" class="shake">
  <div class="form-group">
    <label for="InputName">Imię i nazwisko</label>
    <input type="text" class="form-control" id="InputName" name="fullname"  placeholder="Imię i nazwisko" required data-error="Proszę wpisać swoje imię i nazwisko">
    <div class="help-block with-errors"></div>
  </div>
  <div class="form-group">
    <label for="InputFirma">Firma</label>
    <input type="text" class="form-control" id="InputFirma" name="subject" name="comments"  placeholder="Firma" required data-error="Proszę wpisać nazwę firmy">
    <div class="help-block with-errors"></div>
  </div>
  <div class="form-group">
    <label for="InputEmail">E-mail</label>
    <input type="email" class="form-control" id="InputEmail" name="emailid"  placeholder="E-mail" required data-error="Proszę wpisać swój email">
    <div class="help-block with-errors"></div>
  </div>
  <div class="form-group">
    <label for="InputPhone">Telefon kontaktowy</label>
    <input type="number" class="form-control" name="phone" id="InputPhone" placeholder="Numer telefonu" required data-error="Proszę wprowadzić numer telefonu">
    <div class="help-block with-errors"></div>
  </div>
  <div class="form-group">
    <label for="InputSubject">Temat</label>
    <textarea type="text" class="form-control" name="subject"  id="InputSubject" placeholder="Treść wiadomości" rows="4" required data-error="Proszę wpisać treść wiadomości"></textarea>
    <div class="help-block with-errors"></div>
  </div>
  <div class="padding-top-20">
    <button type="submit" value="send" class="btn btn-default" id="submit" >Wyślij</button>
    <div id="msgSubmit" class="h3 text-center"></div>
  </div>
</form>   

Question

How can I address the error message?


Solution

  • You're just doing things in the wrong order. You need to set the Body property (and not just a variable called $Body) before you send the message, and you don't need to call mail() at all.

    $mail->Body = "Wiadomość od: $name\n E-Mail: $email\n Firma: $company\n";
    
    if(!$mail->send()) {
        echo 'Wiadomość nie mogła zostać wysłana';
        echo "<br><br><br><hr><br>";
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Wiadomość została wysłana';
    }
    

    You're using the auoloader, so you don't need to require the SMTP class separately, it will be loaded automatically.

    You are enabling exceptions (by passing true in the constructor), but you are not wrapping your code in a try/catch block to deal with any that may happen.