php - Message body empty in contact form (using PhPMailer) -


i'm having little problem contact form.

i'm using phpmailer , bootstrap contact form. when run code message:

"uncaught exception 'phpmailerexception' message 'message body empty'" 

this 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 use smtp $mail->host = 'poczta.cgsa.com.pl';   // specify main , backup smtp servers $mail->smtpauth = true;               // enable smtp authentication $mail->username = 'sample@sample.pl'; // smtp username $mail->password = 'fu86m6bsp7';       // smtp password $mail->port = 587;  $mail->setfrom('sample@sample.pl', 'giełd'); $mail->addaddress('sample@sample.pl', 'odbiorca'); // add recipient  $mail->ishtml(true);                               // set email format 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 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ę nazwisko</label>     <input type="text" class="form-control" id="inputname" name="fullname"  placeholder="imię nazwisko" required data-error="proszę wpisać swoje imię 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 address error message?

you're doing things in wrong order. need set body property (and not variable called $body) before send message, , don't need call mail() @ 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 auoloader, don't need require smtp class separately, loaded automatically.

you enabling exceptions (by passing true in constructor), not wrapping code in try/catch block deal may happen.


Comments