dbrree
dbrree

Reputation: 623

PHPMailer sending to blank page

I am trying to get PHPMailer to work to send mail from form data(obviously).

ISSUE:

When submitting form, I am getting sent to a blank page. No errors are being reported. I have followed as many stackoverflow threads surrounding this issue as I could find, and no solutions in those threads resolved the issue for myself.

Thanks

ERRORS:

Fixed all errors aside from: 2016-03-26 22:10:43 SMTP ERROR: Failed to connect to server: Connection refused (111) 2016-03-26 22:10:43 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

PHP:

<?php
$message=
'First Name:    '.$_POST['first'].'<br />
Last Name:  '.$_POST['last'].'<br />
Phone:  '.$_POST['phone'].'<br />
Email:  '.$_POST['email'].'<br />
Message:    '.$_POST['message'].'
';

require 'PHPMailer/PHPMailerAutoload.php';

    $mail = new PHPMailer();

    $mail->SMTPDebug = 2;           // Enable verbose debug output
    $mail->IsSMTP();                // Sets up a SMTP connection  
    $mail->CharSet = 'UTF-8';
    $mail->SMTPAuth = true;         // Connection with the SMTP does require authorization    
    $mail->SMTPSecure = "tls";      // Connect using a TLS connection  
    $mail->Host = "smtp.gmail.com";  //Gmail SMTP server address
    $mail->Port = 587;

    // Authentication  
    $mail->Username   = "[email protected]"; // Your full Gmail address
    $mail->Password   = "xxxx"; // Your Gmail password

    // Compose
    $mail->SetFrom($_POST['email'], $_POST['firstname'] . ' ' . $_POST['lastname']);
    $mail->AddReplyTo($_POST['email'], $_POST['firstname'] . ' ' . $_POST['lastname']);
    $mail->Subject = "New Contact Form Enquiry";      // Subject (which isn't required)  
    $mail->MsgHTML($message);

    // Send To  
    $mail->AddAddress("[email protected]", "xxxx"); // Where to send it - Recipient
    $result = $mail->Send();        // Send!  
    $message = $result ? 'Successfully Sent!' : 'Sending Failed!';      
    unset($mail);
}
?>

HTML:

 <form class="form-inline" name="contactform" action="mailer.php" enctype="multipart/form-data" method="POST">
                    <div class="col-sm-6 form-group form-sty">
                        <label class="sr-only" for="first" required>First Name</label>
                        <input type="text" class="form-control-sty" name="first" placeholder="First Name">
                    </div>
                    <div class="col-sm-6 form-group form-sty">
                        <label class="sr-only" for="last" required>Last Name</label>
                        <input type="text" class="form-control-sty" name="last" placeholder="Last Name">
                    </div>
                    <div class="col-sm-6 form-group form-sty">
                        <label class="sr-only" for="phone" required>Phone</label>
                        <input type="text" class="form-control-sty" name="phone" placeholder="Phone">
                    </div>
                    <div class="col-sm-6 form-group form-sty">
                        <label class="sr-only" for="email" required>Email</label>
                        <input type="email" class="form-control-sty" name="mailfrom" placeholder="Email">
                    </div>
                    <div class="col-sm-12 form-sty">
                        <label class="sr-only" for="comment">Comment</label>
                        <textarea class="form-control-sty" name="message" id="message" rows="7" placeholder="What's on your mind?" required></textarea>
                    </div>
                    <div class="col-sm-12 form-sty form-sty-btn">
                        <button type="submit" value="Submit" class="btn btn-default col-sm-12">Send</button>
                    </div>
                    <p><?php if(!empty($message)) echo $message; ?></p>
                </form>

Upvotes: 0

Views: 3616

Answers (2)

Torchify
Torchify

Reputation: 134

You need to redirect the header back to the form (I assume that is where you want to send them) Replace the location inside the header with your URL

<?php
$message=
'First Name:    '.$_POST['first'].'<br />
Last Name:  '.$_POST['last'].'<br />
Phone:  '.$_POST['phone'].'<br />
Email:  '.$_POST['email'].'<br />
Message:    '.$_POST['message'].'
';

require 'PHPMailer/PHPMailerAutoload.php';

    $mail = new PHPMailer();

    $mail->SMTPDebug = 2;           // Enable verbose debug output
    $mail->IsSMTP();                // Sets up a SMTP connection  
    $mail->CharSet = 'UTF-8';
    $mail->SMTPAuth = true;         // Connection with the SMTP does require authorization    
    $mail->SMTPSecure = "tls";      // Connect using a TLS connection  
    $mail->Host = "smtp.gmail.com";  //Gmail SMTP server address
    $mail->Port = 587;

    // Authentication  
    $mail->Username   = "[email protected]"; // Your full Gmail address
    $mail->Password   = "xxxx"; // Your Gmail password

    // Compose
    $mail->SetFrom($_POST['email'], $_POST['firstname'] . ' ' . $_POST['lastname']);
    $mail->AddReplyTo($_POST['email'], $_POST['firstname'] . ' ' . $_POST['lastname']);
    $mail->Subject = "New Contact Form Enquiry";      // Subject (which isn't required)  
    $mail->MsgHTML($message);

    // Send To  
    $mail->AddAddress("[email protected]", "xxxx"); // Where to send it - Recipient
    $result = $mail->Send();        // Send!  
    if ($result)
    {
        header("Location: : http://www.example.com/contactform.php");
    }
    else {
        echo "sending mail failed: " . $mail->ErrorInfo;
    }
    unset($mail);
}
?> 

Upvotes: 1

John Foley
John Foley

Reputation: 4479

You should make sure there are no errors. Try placing this code at the top of your file to make sure all errors/warnings are reported and run the script again.

ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(E_ALL);

Upvotes: 1

Related Questions