PHPMAILER Not sending and not giving error
Asked Answered
S

10

24

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   = "[email protected]"; // SMTP account username example
    $mail->Password   = "password";        // SMTP account password example

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

    $mail->AddAddress('[email protected]', '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';
Swineherd answered 28/6, 2013 at 12:2 Comment(7)
Check the server logs and find out what's wrong.Wagon
which os your using ?In case of Ubuntu if mail sending fails then in your web root a file named dead.letter will be created,once check that.Kopaz
Try to insert '}' after 'exit;'Duclos
or after last 'echo' ?Duclos
@AmalMurali this is what i get in the log: [28-Jun-2013 11:57:02 UTC] PHP Fatal error: require_once() [<a href='function.require'>function.require</a>]: Failed opening required 'class.smtp.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/digitdev/public_html/class.phpmailer.php on line 1004Swineherd
@Wale: Did you follow the directions in the INSTALL.txt file, including downloading and installing the PHPMailer package?Wagon
Are you using Gmail?Dantzler
M
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.

Massorete answered 24/5, 2014 at 1:42 Comment(1)
You, sir, are a real hero! I have been fighting a bug for a while, but was unable to locate it, until now!Staphyloplasty
S
16

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   = "[email protected]"; // SMTP account username example
$mail->Password   = "password";        // SMTP account password example

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

$mail->AddAddress('[email protected]', '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';
Swineherd answered 29/6, 2013 at 21:0 Comment(2)
This CORRECT. I had the same issue , after adding the class.smtp.php it work fine.Blau
Also requiring SMTP.php was vital for me, too.Lancelot
C
9

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.

Creamery answered 4/12, 2013 at 19:52 Comment(0)
E
5

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.

Enlistment answered 3/2, 2016 at 3:25 Comment(0)
C
2

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

Hope this helps

Coward answered 26/4, 2014 at 12:40 Comment(0)
A
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   = "[email protected]"; // SMTP account username example
    $mail->Password   = "password";        // SMTP account password example

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

    $mail->AddAddress('[email protected]', '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();
}
Awl answered 28/6, 2013 at 12:39 Comment(4)
Dreamweaver highlights the code from "catch" as red which means syntax is not correct...Swineherd
I tested the code with php balise "<?php // your code ?>". It does not have parse error.Awl
just tried it again and no code error, but it still doent work. Will my error_log help?Swineherd
your email is no sent or no received ? without error your email is probably no receivedAwl
P
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!

Provencher answered 17/1, 2014 at 6:9 Comment(0)
W
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.

Wes answered 9/8, 2014 at 12:46 Comment(0)
I
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));
Ias answered 13/4, 2021 at 17:58 Comment(0)
O
0

Try with the following sample code.

<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.example.com';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = '[email protected]';                     //SMTP username
    $mail->Password   = 'secret';                               //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
    $mail->Port       = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

    //Recipients
    $mail->setFrom('[email protected]', 'Mailer');
    $mail->addAddress('[email protected]', 'Joe User');     //Add a recipient
    $mail->addAddress('[email protected]');               //Name is optional
    $mail->addReplyTo('[email protected]', 'Information');
    $mail->addCC('[email protected]');
    $mail->addBCC('[email protected]');

   
    //Content
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

Use a free SMTP sandbox like MailMug.net account to test it

Obsequies answered 17/2 at 16:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.