簡體   English   中英

使用SMTP PHP phpmailer發送郵件

[英]Sending a Mail using SMTP PHP phpmailer

我希望能夠在每次填寫並提交聯系表時將郵件發送到我的電子郵件地址。 我遵循了有關如何執行此操作的教程,但不幸的是,它沒有將郵件發送到郵箱。 每次我提交表單時,它都會返回錯誤消息“有錯誤”。有人可以檢查此代碼以找出可能是什么問題嗎?

這是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>

請更改此行。

$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!
}

真正的參數意味着它將對錯誤拋出異常,這是我們需要捕獲的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM