在localhost中执行时PHP Mailer中的错误

时间:2017-11-24 06:53:12

标签: php xampp localhost phpmailer

我刚开始使用PHPmailer尝试在localhost(XAMPP)服务器上发送邮件。我包括了class.phpmailer.php和class.smtp.php文件但是我收到了错误消息"出现错误"为了解决这个问题,我将gmail帐户的IMAP设置更改为启用,并允许安全性较低的应用。 但问题一直没有解决。我明白了:

  

警告:fwrite()要求参数1为资源,在第1023行的C:\ xampp \ htdocs \ email-phpmailer \ class \ class.smtp.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 = 'smtp.gmail.com';     //Sets the SMTP hosts of your Email hosting, this for Godaddy
        $mail->Port = '587';                                //Sets the default SMTP server port
        $mail->SMTPAuth = true;                         //Sets SMTP authentication. Utilizes the Username and Password variables
        $mail->Username = 'abc@gmail.com';                  //Sets SMTP username
        $mail->Password = 'MyPass';                 //Sets SMTP password
        $mail->SMTPSecure = 'ssl';                          //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('xyz@gmail.com', 'its me');       //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 = '';
    }
}

?>
<!DOCTYPE html>
<html>
    <head>
        <title>Send an Email on Form Submission using PHP with PHPMailer</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </head>
    <body>
        <br />
        <div class="container">
            <div class="row">
                <div class="col-md-8" style="margin:0 auto; float:none;">
                    <h3 align="center">Send an Email on Form Submission using PHP with PHPMailer</h3>
                    <br />
                    <?php echo $error; ?>
                    <form method="post">
                        <div class="form-group">
                            <label>Enter Name</label>
                            <input type="text" name="name" placeholder="Enter Name" class="form-control" value="<?php echo $name; ?>" />
                        </div>
                        <div class="form-group">
                            <label>Enter Email</label>
                            <input type="text" name="email" class="form-control" placeholder="Enter Email" value="<?php echo $email; ?>" />
                        </div>
                        <div class="form-group">
                            <label>Enter Subject</label>
                            <input type="text" name="subject" class="form-control" placeholder="Enter Subject" value="<?php echo $subject; ?>" />
                        </div>
                        <div class="form-group">
                            <label>Enter Message</label>
                            <textarea name="message" class="form-control" placeholder="Enter Message"><?php echo $message; ?></textarea>
                        </div>
                        <div class="form-group" align="center">
                            <input type="submit" name="submit" value="Submit" class="btn btn-info" />
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>

0 个答案:

没有答案