简体   繁体   中英

PHP Mailer Coding Issue

I am having an issue with getting my PHP mailer code to work correctly. I can get it to send an email on page load fine, the issue is with the ISSET command to get it to submit the form once the submit button has been pressed. I have tried putting it in various places but still can't get it to work. Any help would be appreciated.

<?php
require '/phpmailer/PHPMailerAutoload.php';
require_once '/phpmailer/class.phpmailer.php';
include '/phpmailer/class.smtp.php';

if(isset($_POST["Submit"])) 
{

$emailaddress = '****@**********';

$message=
    'Name:  '.$_POST['name'].'<br />
    Email:  '.$_POST['email'].'<br />
    IP: '.$_SERVER['REMOTE_ADDR'].'<br /><br />
    Message:<br /><br />
    '.nl2br($_POST['message']).'
    ';

$mail             = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "********"; // SMTP server
$mail->SMTPDebug  = 1;                     // 1 = errors and messages,2 = messages only
$mail->SMTPAuth   = false;                  // enable SMTP authentication
$mail->Port       = 25;                    // set the SMTP port for the GMAIL server
$mail->SMTPSecure = 'false';                            // Enable encryption, 'ssl' also accepted
$mail->CharSet  = 'UTF-8';  // so it interprets foreign characters
$mail->setFrom('***********');
$mail->AddReplyTo('**********');
$mail->Subject    = "Contact form submission from ".$_POST['name']." ";
$mail->MsgHTML($message);
$mail->AddAddress($emailaddress);

if(!$mail->Send())
{
    echo "Message could not be sent. <p>";
    echo "Mailer Error: " . $mail->ErrorInfo;
    exit;
}

    echo "Thank you, your message has been sent!";
}

I'm going to make my comment as an answer, because that is the only conclusion I can come up with and the most likely to be, without seeing the OP's HTML form.

Your conditional statement if(isset($_POST["Submit"])) is based on a submit button named Submit . If your submit button has name="submit" instead of name="Submit" , then that would explain it. If it isn't named, then do .

Ie:

<input type="submit" name="Submit" value="Send Email">

is not the same as

<input type="submit" name="submit" value="Send Email">
  • POST variables are case-sensitive.

Your presently shown code has the proper bracing.

Having used error reporting and placed the top of your file(s) right after your opening <?php tag

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

would have signaled an Undefined index Submit... warning message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM