1

I'm trying to get a contact form send an email to an inbox with PHPMailer. Once the email is sent or ready to send, I want it to relocate the page to thankyou.php.

The trouble I'm having when clicking my form button: It goes to the mail.php page but it's blank. I have the header at the bottom redirecting the page.

Testing the script on the command line via "php mail.php", returning with no errors, I then receive the email.

Testing the script on a browser brings up white page and no email.

When I remove only the code for PHPMailer but leave my require() statement on the first line and header(Location: thankyou.php), it then redirects me to my thankyou page.

Here's my code:

PHPMailer Code : mail.php

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

$mailto = new PHPMailer();
$mailto->SMTPDebug = 2;
$mailto->CharSet = 'UTF-8';
$mailto->isSMTP();
$mailto->Host = "smtp.gmail.com";
$mailto->SMTPAuth = true;
$mailto->Username = "*****@gmail.com";
$mailto->Password = "*****";
$mailto->SMTPSecure = "tls";
$mailto->Port = 587;

$mailto->addAddress('example@gmail.com', 'Joe User');
$mailto->addReplyTo('example@gmail.com', 'Information');
$mailto->setFrom('example@gmail.com', 'Mailer');

$mailto->isHTML(true);
$mailto->Subject = "Testing From Contact Page";
$mailto->Body = "This is the HTML message body <b>in bold!</b>"; 
$mailto->AltBody = "This is the body in plain text for non-HTML mail clients";

$mailto->Send();
header("Location: http://$URL/thankyou.php");
?>

HTML CODE FOR

<?php
    include('navigation.php');
?>
<div id="contact_form_wrapper">
<div class="contact_call">
    <div>
        <img src="images/icons/icon_call.png" alt="Phone: 555.555.5555" />
        <div>
            <h2>Call us at 555.555.5555</h2>
            <p>
                Address Canada<br /><br />
                Hours: Monday - Friday 8:00 a.m. to 4:00 p.m.
            </p>
        </div>
    </div>
</div>


<h1>Fill out this form to request more information</h1>

<form name="requestquote" method="POST" action="mail.php">
<!-- onsubmit="return formValidation() -->
    <!-- <ul class="request_div">
        <li><p>Company Name</p><input type="text" name="company" maxlength="50" size="30"/></li>
        <li><p>Full Name</p><input type="text" name="name" maxlength="50" size="30"/></li>
        <li><p>Postal Code</p><input type="text" name="zip" maxlength="50" size="30"/></li>
        <li><p>Telephone</p><input type="text" name="phone" maxlength="50" size="30"/></li>
        <li><p>Email</p><input type="text" name="email" maxlength="50" size="30"/></li>
    </ul>
    <ul class="div_comment">
        <li><p>Request Information</p><textarea  name="comments"></textarea></li>
    </ul> -->
    <div id="btnRequests">
        <input type="submit" value="SUBMIT" class="btnStyle1"/>
        <input type="reset" value="CLEAR" class="btnStyle1" />
    </div>
</form> 

<div class="contact_call">
    <div>
        <img src="images/icons/icon_call.png" alt="Phone: 555.555.5555" />
        <div>
            <h2>Call us at 555.555.5555</h2>
            <p>
                Address Canada<br /><br />
                Hours: Monday - Friday 8:00 a.m. to 4:00 p.m.
            </p>
        </div>
    </div>
</div>

</div>
<?php
    include('footer.php');
?>

I've already tried the following as well:

  • Checked for White Space around ,

  • Messed around with Ob_start() Not sure how that really went. didn't do anything to the code.

  • Tried putting Script tags and adding JS. I couldn't get that to even work. though my other sites have working js. I kind of don't want to go this route.

  • I've placed exit(); everywhere, as well as die();

  • Placed and Removed Echo Statements.

  • Tried installing the PHPMailer with Composer.

  • Added many various require, require_once, include variations

  • My Ports are open. The hosting server has all open as I already called them to confirm.

Maybe after all one of the above are right, I've been working on this for three days. Now asking for some help. Let's Chat!

  • Where's the error_reporting? Checked the log files? – mario Oct 27 '18 at 04:05
  • @mario, So I looked into error_reporting and went into the php.ini file and un-commented all the error_reporting lines. I also added ini_set('display_errors', 'On');ini_set('display_startup_errors', 1); error_reporting(E_ALL); and -1. When I then run script on the browser -> error. Fatal error: Cannot instantiate non-existent class: phpmailer in mail.php on line 7 Line 7 => $mailto = new PHPMailer(); I looked in the class.phpmailer.php and the class is there with preconfigs. I cannot access error.log file due to chmod restrict and not my server. – Jesse Doucet Oct 27 '18 at 19:03
  • @mario, I also added all the files in the same directory. I get the same error. I'm working on a server that runs PHP 5.3.10-1ubuntu3.26, It's not my server and don't really have full access to much. though they run many MTA's via that server "So THEY say...". – Jesse Doucet Oct 27 '18 at 19:11
  • Wouldn't it be `= new PHPMailer\PHPMailer\PHPMailer();` then? – mario Oct 27 '18 at 19:23
  • Also yes, that server is not going to last much longer, even if you keep running outdated versions of everything. – mario Oct 27 '18 at 19:24

1 Answers1

0

You can’t produce output and also redirect, and you are producing output because you have PHPMailer debug output enabled. Turn it off:

$mail->SMTPDebug = false;
Synchro
  • 29,823
  • 14
  • 69
  • 85
  • I've changed the configuration a few times, false, 0-4 When I had done $mail->SMTPDebug = 2; I used to get the a PHP Fatal Error via Ubuntu Command Line with the following input -> php mail.php Warning: Cannot modify header information - headers already sent by (output started at /usr/userweb/sewright/new/class.smtp.php:87) in /usr/userweb/sewright/new/mail.php on line 29 I then commented out the line in the class.smtp.php and it sent an email via command line. I went into the class.phpmailer.php file and also changed the setting to false. Right now they're all turned to false. – Jesse Doucet Oct 27 '18 at 18:51
  • Don’t edit files loaded through composer. You’ve also described editing files that are from an old version of PHPMailer, so upgrade. It’s difficult to help further because you have not posted your error output. – Synchro Oct 27 '18 at 19:34