简体   繁体   English

使用SMTP PHP phpmailer发送邮件

[英]Sending a Mail using SMTP PHP phpmailer

I want to be able to send a mail to my Email address whenever I fill in and submit my contact form. 我希望能够在每次填写并提交联系表时将邮件发送到我的电子邮件地址。 I followed a tutorial on how to do that but unfortunately it does not send a mail to mailbox. 我遵循了有关如何执行此操作的教程,但不幸的是,它没有将邮件发送到邮箱。 Everytime I submit the form it justs returns the error message 'There is an error' Can someone please check this code to find out what can be the problem? 每次我提交表单时,它都会返回错误消息“有错误”。有人可以检查此代码以找出可能是什么问题吗?

Here is the php code 这是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 = '';
            }
            }

    ?>

Here is my form 这是我的表格

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

Please change this line. 请更改此行。

$mail = new PHPMailer;

To: 至:

$mail = new PHPMailer(true);

Put code in try and catch block as: 将代码放入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!
}

The true param means it will throw exceptions on errors, which we need to catch. 真正的参数意味着它将对错误抛出异常,这是我们需要捕获的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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