17

I am trying to let users fill out a contact form, which will then be sent to my email. But its not working for some reason. I just get a blank page with no error message or any text and email is also not sent.

if (isset($_POST['submit']))
{
    include_once('class.phpmailer.php');

    $name = strip_tags($_POST['full_name']);
    $email = strip_tags ($_POST['email']);
    $msg = strip_tags ($_POST['description']);

    $subject = "Contact Form from DigitDevs Website";

    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->CharSet = 'UTF-8';

    $mail->Host       = "mail.example.com"; // SMTP server example
    //$mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Port       = 26;                    // set the SMTP port for the GMAIL server
    $mail->Username   = "info@example.com"; // SMTP account username example
    $mail->Password   = "password";        // SMTP account password example

    $mail->From = $email;
    $mail->FromName = $name;

    $mail->AddAddress('info@example.com', 'Information'); 
    $mail->AddReplyTo($email, 'Wale');

    $mail->IsHTML(true);

    $mail->Subject = $subject;

    $mail->Body    =  $msg;
    $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;
    exit;
}
echo 'Message has been sent';
Mahesh.D
  • 1,651
  • 2
  • 19
  • 42
Wale
  • 345
  • 1
  • 2
  • 10

9 Answers9

17

You need to call:

$mail = new PHPMailer(true); // with true in the parenthesis

From the documentation:

The true param means it will throw exceptions on errors, which we need to catch.

Sven
  • 1,243
  • 3
  • 31
  • 53
user3670588
  • 171
  • 1
  • 2
14

Its working now, i didnt include the 'class.smtp.php' file. The working code is below:

 if (isset($_POST['submit']))
{
 include_once('class.phpmailer.php');

 require_once('class.smtp.php');

$name = strip_tags($_POST['full_name']);
$email = strip_tags ($_POST['email']);
$msg = strip_tags ($_POST['description']);

$subject = "Contact Form from DigitDevs Website";

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';

$mail->Host       = "mail.example.com"; // SMTP server example
//$mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Port       = 26;                    // set the SMTP port for the GMAIL server
$mail->Username   = "info@example.com"; // SMTP account username example
$mail->Password   = "password";        // SMTP account password example

$mail->From = $email;
$mail->FromName = $name;

$mail->AddAddress('info@example.com', 'Information'); 
$mail->AddReplyTo($email, 'Wale');

$mail->IsHTML(true);

$mail->Subject = $subject;

$mail->Body    =  $msg;
$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;
exit;
}
 echo 'Message has been sent';
Wale
  • 345
  • 1
  • 2
  • 10
8

I had the same problem with no error message even with SMTPDebug enabled. After searching around for working examples I noticed that I didn't include the SMTP Secure value. Try adding this line:

$mail->SMTPSecure = 'ssl'; //secure transfer enabled

Work like a charm now.

Syclone
  • 799
  • 9
  • 12
4

I had a similar problem. In reference to @Syclone's answer. I was using the default "tls".

$mail->SMTPSecure = 'tls';

After I changed it to $mail->SMTPSecure = 'ssl'; It worked ! My mailserver was only accepting connections over SSL.

JH_
  • 408
  • 4
  • 13
phil
  • 767
  • 8
  • 13
2

What worked for me was setting From as Username and FromName as $_POST['email']

Hope this helps

Raf
  • 352
  • 2
  • 10
1

PHPMailer use exception.

Try this

try {

    include_once('class.phpmailer.php');

    $name = strip_tags($_POST['full_name']);
    $email = strip_tags ($_POST['email']);
    $msg = strip_tags ($_POST['description']);

    $subject = "Contact Form from DigitDevs Website";

    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->CharSet = 'UTF-8';

    $mail->Host       = "mail.example.com"; // SMTP server example
    //$mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Port       = 26;                    // set the SMTP port for the GMAIL server
    $mail->Username   = "info@example.com"; // SMTP account username example
    $mail->Password   = "password";        // SMTP account password example

    $mail->From = $email;
    $mail->FromName = $name;

    $mail->AddAddress('info@example.com', 'Information'); 
    $mail->AddReplyTo($email, 'Wale');

    $mail->IsHTML(true);

    $mail->Subject = $subject;

    $mail->Body    =  $msg;
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->Send();

    exit;

} catch (phpmailerException $e) {
  echo $e->errorMessage(); //error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage();
}
Bastien D
  • 1,245
  • 2
  • 12
  • 25
1

I was trying to load an HTML file to send, which did not belong to the www-data group on my Ubuntu server.

chown -R www-data * 
chgrp -R www-data *

Problem solved!

Andy
  • 868
  • 10
  • 16
0

I was debating whether to write my own handler or crow-bar PHPMailer into my existing class structure. In the event it was very easy because of the versatility of the spl_autoload_register function which is used within the PHPMailer system as well as my existing class structure.

I simply created a basic class Email in my existing class structure as follows

<?php

   /**
     * Provides link to PHPMailer
     *
     * @author Mike Bruce
     */
   class Email {
      public $_mailer; // Define additional class variables as required by your application
      public function __construct()
      {
         require_once "PHPMail/PHPMailerAutoload.php" ;
         $this->_mailer = new PHPMailer() ;
         $this->_mailer->isHTML(true);
         return $this;
      }
   }
?>

From a calling Object class the code would be:

$email = new Email;
$email->_mailer->functionCalls();

// continue with more function calls as required

Works a treat and has saved me re-inventing the wheel.

Mofi
  • 38,783
  • 14
  • 62
  • 115
Mike M0ITI
  • 23
  • 1
  • 5
0

Try this ssl settings:

$mail->SMTPSecure  = 'tls'; //tls or ssl
$mail->SMTPOptions = array('ssl' => array('verify_peer'       => false,
                                          'verify_peer_name'  => false,
                                          'allow_self_signed' => true));
Ns789
  • 523
  • 5
  • 15