dongqiaozhe5070 2014-10-06 16:43
浏览 31
已采纳

PHP邮件程序编码问题

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!";
}
  • 写回答

1条回答 默认 最新

  • douyu0792 2014-10-06 16:47
    关注

    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.

    I.e.:

    <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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?