使用SMTP PHP phpmailer发送邮件

时间:2018-03-31 18:11:46

标签: php smtp phpmailer mail-server

每当我填写并提交联系表格时,我希望能够向我的电子邮件地址发送邮件。我遵循了如何做到这一点的教程,但不幸的是它没有发送邮件到邮箱。每次我提交表单时,它都会返回错误消息“有错误”有人可以查看此代码以找出可能出现的问题吗?

这是php代码

        <?php
            //index.php

            $error = '';
            $name = '';
            $email = '';
            $subject = '';
            $message = '';

            function clean_text($string)
            {
            $string = trim($string);
            $string = stripslashes($string);
            $string = htmlspecialchars($string);
            return $string;
            }

            if(isset($_POST["submit"]))
            {
            if(empty($_POST["name"]))
            {
                $error .= '<p><label class="text-danger">Please Enter your Name</label></p>';
            }
            else
            {
                $name = clean_text($_POST["name"]);
                if(!preg_match("/^[a-zA-Z ]*$/",$name))
                {
                    $error .= '<p><label class="text-danger">Only letters and white space allowed</label></p>';
                }
            }
            if(empty($_POST["email"]))
            {
                $error .= '<p><label class="text-danger">Please Enter your Email</label></p>';
            }
            else
            {
                $email = clean_text($_POST["email"]);
                if(!filter_var($email, FILTER_VALIDATE_EMAIL))
                {
                    $error .= '<p><label class="text-danger">Invalid email format</label></p>';
                }
            }
            if(empty($_POST["subject"]))
            {
                $error .= '<p><label class="text-danger">Subject is required</label></p>';
            }
            else
            {
                $subject = clean_text($_POST["subject"]);
            }
            if(empty($_POST["message"]))
            {
                $error .= '<p><label class="text-danger">Message is required</label></p>';
            }
            else
            {
                $message = clean_text($_POST["message"]);
            }
            if($error == '')
            {
                require 'class/class.phpmailer.php';
                $mail = new PHPMailer;
                $mail->IsSMTP();                                //Sets Mailer to send message using SMTP
                $mail->Host = 'kwchems.com';        //Sets the SMTP hosts of your Email hosting, this for Godaddy
                $mail->Port = '465';                                //Sets the default SMTP server port
                $mail->SMTPAuth = true;                         //Sets SMTP authentication. Utilizes the Username and Password variables
                $mail->Username = 'info@kwchems.com';                   //Sets SMTP username
                $mail->Password = 'txpxbaron45';                    //Sets SMTP password
                $mail->SMTPSecure = 'tls';                          //Sets connection prefix. Options are "", "ssl" or "tls"
                $mail->From = $_POST["email"];                  //Sets the From email address for the message
                $mail->FromName = $_POST["name"];               //Sets the From name of the message
                $mail->AddAddress('info@kwchems.com', 'Name');      //Adds a "To" address
                $mail->AddCC($_POST["email"], $_POST["name"]);  //Adds a "Cc" address
                $mail->WordWrap = 50;                           //Sets word wrapping on the body of the message to a given number of characters
                $mail->IsHTML(true);                            //Sets message type to HTML             
                $mail->Subject = $_POST["subject"];             //Sets the Subject of the message
                $mail->Body = $_POST["message"];                //An HTML or plain text message body
                if($mail->Send())                               //Send an Email. Return true on success or false on error
                {
                    $error = '<label class="text-success">Thank you for contacting us</label>';
                }
                else
                {
                    $error = '<label class="text-danger">There is an Error</label>';
                }
                $name = '';
                $email = '';
                $subject = '';
                $message = '';
            }
            }

    ?>

这是我的表格

<div class="contact-form">
                <?php echo $error; ?>

        <h2>Contact Us (* Required Field)</h2>

            <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
                <div>
                    <span><label>Your name*</label></span>
                    <span><input type="text" name="name"  value="<?php echo $name; ?>" style=" padding:8px; border-radius:5px;"></span>
                </div>
                <div>
                    <span><label>Your email*</label></span>
                    <span><input type="text" name="email" value="<?php echo $email; ?>" style=" padding:8px; border-radius:5px;"></span>
                </div>
                <div>
                    <span><label>Company:</label></span>
                    <span><input type="text" name="company" value="<?php echo $company; ?>" style=" padding:8px; border-radius:5px;"</span>
                </div>
                <div>
                    <span><label>Country*:</label></span>
                    <span><input type="text" name="country" value="<?php echo $country; ?>" style=" padding:8px; border-radius:5px;"</span>
                </div>

                <div>
                    <span><label>Phone:</label></span>
                    <span><input type="text" name="phone" value="<?php echo $phone; ?>"  style=" padding:8px; border-radius:5px;"</span>
                </div>
                <div>
                    <span><label>SUBJECT*</label></span>
                    <span><input type="text" name="subject" value="<?php echo $subject; ?>" style=" padding:8px; border-radius:5px;"</span>
                </div>
                <div>
                    <span><label>Type Your Message Please*</label></span>
                    <span><textarea name="message"> <?php echo $message; ?></textarea></span>
                </div>
               <div>
                    <span><input type="submit" name="submit" value="Send"></span>
              </div>
            </form>


      </div>

1 个答案:

答案 0 :(得分:0)

请更改此行。

$mail = new PHPMailer;

要:

$mail = new PHPMailer(true);

将代码放入try和catch块中:

try {

 require 'class/class.phpmailer.php';
            $mail = new PHPMailer;
            $mail->IsSMTP();                                //Sets Mailer to send message using SMTP
            $mail->Host = 'kwchems.com';        //Sets the SMTP hosts of your Email hosting, this for Godaddy
            $mail->Port = '465';                                //Sets the default SMTP server port
            $mail->SMTPAuth = true;                         //Sets SMTP authentication. Utilizes the Username and Password variables
            $mail->Username = 'info@kwchems.com';                   //Sets SMTP username
            $mail->Password = 'txpxbaron45';                    //Sets SMTP password
            $mail->SMTPSecure = 'tls';                          //Sets connection prefix. Options are "", "ssl" or "tls"
            $mail->From = $_POST["email"];                  //Sets the From email address for the message
            $mail->FromName = $_POST["name"];               //Sets the From name of the message
            $mail->AddAddress('info@kwchems.com', 'Name');      //Adds a "To" address
            $mail->AddCC($_POST["email"], $_POST["name"]);  //Adds a "Cc" address
            $mail->WordWrap = 50;                           //Sets word wrapping on the body of the message to a given number of characters
            $mail->IsHTML(true);                            //Sets message type to HTML             
            $mail->Subject = $_POST["subject"];             //Sets the Subject of the message
            $mail->Body = $_POST["message"];                //An HTML or plain text message body
            if($mail->Send())                               //Send an Email. Return true on success or false on error
            {
                $error = '<label class="text-success">Thank you for contacting us</label>';
            }
            else
            {
                $error = '<label class="text-danger">There is an Error</label>';
            }
            $name = '';
            $email = '';
            $subject = '';
            $message = '';

} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}

true param意味着它会抛出我们需要捕获的错误的异常。