簡體   English   中英

Php Mailer / GoDaddy

[英]Php Mailer / GoDaddy

我通過GoDaddy上傳了一個聯系表單頁面,其中包含帶有嵌入php(contact.php)的html編碼的附件選項,但是當我要填寫表單並提交表單時,出現了很多我不理解的錯誤。

我正在使用php mailer,並且從我的聯系頁面將php包含到另一個php頁面(bootstrap.php)。

下面是代碼:

聯系頁面:

<!DOCTYPE html>
<html>
<head>
   <?php 
       ini_set('display_errors', 'On'); 
       error_reporting(E_ALL);
       include('bootstrap.php');
   ?>
</head>
<body>
   <div class = "wrapper">   
       <section class ="bulk">
           <?php if(isset($_GET["status"]) == "thanks") { ?>
                <br><br><br><br><br><br><br><br><br><br><br><br>
                <p class = "text-center"> Thanks for the email! We will be in touch shortly </p>
                <?php } else { ?>
                    <h1 class = "text-center">
                      We look forward to working with you!
                    </h1><br>                    
                    <h4 class = "text-center" style="font-weight:normal;">
                        Please complete the form and attach file
                    </h4>

               <!-- form stuff -->                                         
                    <div class ="row">                        
                        <div class="contact">                                                                                                                              
                            <form method ="post" action ="contact.php" enctype='multipart/form-data'>
                                <div class="form-group">
                                   <label for="name">Name</label>
                                    <input type ="text" class="form-control" name="name" id ="name" placeholder="Enter name" value="<?php if (!empty($_POST['name'])) {echo $_POST['name'];} ?>">                                           
                                    <?php 
                                        if ($error) {
                                            echo "<p class='text-danger'>$errName</p>";
                                        }
                                        else {
                                            echo "<br>";
                                        }                                           
                                    ?>
                                </div>                                                                                                                                                                         
                                <!--email-->
                                <div class="form-group">
                                    <label for="email">Email</label>
                                    <input type ="text" class="form-control" name="email" id ="email" placeholder="Enter email" value="<?php if (!empty($_POST['email'])) {echo $_POST['email'];} ?>">
                                    <?php 
                                        if ($error) {
                                            echo "<p class='text-danger'>$errEmail</p>";
                                        }
                                        else {
                                            echo "<br>";
                                        }                                           
                                    ?>
                                </div>   
                                <!--msg-->
                                <div class="form-group">
                                    <label for="message">Message</label>                                            
                                    <textarea name ="message" class="form-control" id ="message" placeholder="Enter message" rows = "10" ></textarea>
                                    <?php 
                                        if ($error) {
                                            echo "<p class='text-danger'>$errMessage</p>";
                                        }
                                        else {
                                            echo "<br>";
                                        }                                           
                                    ?>
                                </div> 
                                <!--attachments-->
                                <div class="form-group">
                                    <label for="attachment" >Attach your file</label>                                            
                                    <input type ="file" name='attachment' id='attachment'>
                                </div>
                                <!--Hpam Sponypot -->
                                <div class="form-group" style="visibility: hidden">     
                                    <label for="address">Address</label>
                                    <input type ="text" name="address" id ="address">  
                                    <p> Humans, do not fill out this form! </p>
                                </div>                                     
                                <!--attachments-->
                                <div class = "buttons">
</div><button type="submit" value="Send" class="button">Submit</button>
                        </form>
                    </div><br><br>
                    </div>
                     <?php } ?>
            </section>  

這是聯系頁面頂部包括的php頁面(bootstrap.php):

require_once("phpmailer/class.phpmailer.php");
require_once('phpmailer/PHPMailerAutoload.php');
$mail = new phpmailer();

// VARIABLE DECLARATIONS

$errName = '';
$errEmail = '';
$errMessage = '';
$error = false;


if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["name"]); //trim destroys whitespaces like "Tab" or  "Spacebar"
$email = trim($_POST["email"]);
$phonenumber = trim($_POST["phonenumber"]);
$message = trim($_POST["message"]);

// Attack #1 preventor - Spam Honeypot

if ($_POST["address"] != "") {
    echo "SPAM HONEYPOT";
    exit;
}

// Attack #2 preventor - Email header injection hack preventor 

foreach($_POST as $value) {
    if(stripos($value, 'Content-Type:') !== FALSE) {
        echo "There was a problem with the information you entered.";
        exit;
    }
}

 // Check if name has been entered
if ($name == "") {
    $errName = 'Sorry, you have not entered a name, please try again.';             
}

// Check if message has been entered
if ($message == "") {      
    $errMessage = 'Sorry, you have not typed a message.';      
}

// Check if email is valid
if (!$mail->validateAddress($email)) {        
    $errEmail = 'Sorry, please enter a valid email address.';
}

// Flag error messages if any tests fail

if ($errName || $errMessage || $errPhonenumber || $errEmail) {
    $error = true; // Boolean to flag validating error messages in the HTML
} 
else {             


    // EMAIL BODY IFF FORM VALIDATION IS SUCCESSFULL! 
    $email_body = "";
    $email_body = $email_body. "Name: " . $name . "<br>";
    $email_body = $email_body. "Email: " . $email . "<br>";
    $email_body = $email_body. "Phone number: " . $phonenumber . "<br>";
    $email_body = $email_body. "Message: " . $message;


    date_default_timezone_set('Etc/UTC');


    //Tell PHPMailer to use SMTP
    $mail->isSMTP();

    //Enable SMTP debugging
    // 0 = off (for production use)
    // 1 = client messages
    // 2 = client and server messages
    $mail->SMTPDebug = 2;

    //Ask for HTML-friendly debug output
    $mail->Debugoutput = 'html';

    //Set the hostname of the mail server
    $mail->Host = 'smtp.gmail.com';

    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
    $mail->Port = 465;

    //Set the encryption system to use - ssl (deprecated) or tls
    $mail->SMTPSecure = 'ssl';

    //Whether to use SMTP authentication
    $mail->SMTPAuth = true;

    //Username to use for SMTP authentication - use full email address for gmail
    $mail->Username = "#############################";

    //Password to use for SMTP authentication
    $mail->Password = "###################";

    //Set who the message is to be sent from
    $mail->setFrom($email, $name);

    //Set who the message is to be sent to
    $mail->addAddress('companyname@gmail.com', 'Company Name');

    //Set the subject line
    $mail->Subject = 'PHPMailer GMail SMTP test';

    //Add attachment
    $mail->addAttachment($_FILES['attachment']);
//        if (isset($_FILES['attachment']) &&
//            $_FILES['attachment']['error'] == UPLOAD_ERR_OK) {
//            $mail->addAddress($_FILES['attachment']['tmp_name'],
//                                 $_FILES['attachment']['name']);
//        }


    //Read an HTML message body from an external file, convert referenced images to embedded,
    //convert HTML into a basic plain-text alternative body
    $mail->msgHTML($email_body);


    //send the message, check for errors
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    }
    else {             
    }
    header("Location: contact.php?status=thanks");
    exit;
}
}
?>

這些是提交表單時出現的錯誤消息:

Notice: Array to string conversion in 

/home/username/public_html/phpmailer/class.phpmailer.php on line 2395
Could not access file: Array
SERVER -> CLIENT: 220-n1plcpnl0057.prod.ams1.secureserver.net ESMTP Exim 4.85                 
#2 Thu, 07 Jan 2016 07:46:23 -0700 220-We do not authorize the use of this     

system          to transport unsolicited, 220 and/or bulk e-mail.
CLIENT -> SERVER: EHLO mywebsite.com
SERVER -> CLIENT: 250-n1plcpnl0057.prod.ams1.secureserver.net Hello     

n1plcpnl0057.prod.ams1.secureserver.net [46.252.205.183]250-SIZE 52428800250-    

8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250-STARTTLS250 HELP
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 TLS go ahead
CLIENT -> SERVER: EHLO mywebsite.com
SERVER -> CLIENT: 250-n1plcpnl0057.prod.ams1.secureserver.net Hello     

n1plcpnl0057.prod.ams1.secureserver.net [46.252.205.183]250-SIZE 52428800250-   

8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250 HELP
CLIENT -> SERVER: AUTH LOGIN
SERVER -> CLIENT: 334 xxx
CLIENT -> SERVER: IyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyM=
SERVER -> CLIENT: 334 xxx
CLIENT -> SERVER: IyMjIyMjIyMjIyMjIyMjIyMjIw==
SERVER -> CLIENT: 535 Incorrect authentication data
SMTP ERROR: Password command failed: 535 Incorrect authentication data
SMTP Error: Could not authenticate.
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 221 n1plcpnl0057.prod.ams1.secureserver.net closing    
connection
SMTP connect() failed.     
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed.     
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Warning: Cannot modify header information - headers already sent by (output     
started at /home/myusername/public_html/contact.php:4) in     
/home/username/public_html/bootstrap.php on line 148

bootstrap.php的第148行中的代碼是

header("Location: contact.php?status=thanks");

謝謝你的幫助。

您的問題是您addAttachment()整個$_FILES['attachment']數組傳遞給第一個參數中的addAttachment()方法,而不是包含文件路徑的字符串。 因此,錯誤消息!

文件位置的此路徑位於$_FILES['attachment']['tmp_name']因為您尚未將上載的文件從tmp存儲移動到永久文件位置。

如果需要,也可以傳遞參數2(可選),該參數可以在$_FILES['attachment']['name']

所以像這樣修改這段代碼

//Add attachment
$mail->addAttachment($_FILES['attachment']['tmp_name'],
                     $_FILES['attachment']['name']);

該方法的原型為:

public function AddAttachment($path,
                              $name = '',
                              $encoding = 'base64',
                              $type = 'application/octet-stream')

您是否嘗試閱讀錯誤消息? 它包含指向疑難解答指南的鏈接,該指南涵蓋了GoDaddy以及如何連接到gmail。 您的代碼基於一些隨機的,過時的示例,而不是PHPMailer提供的維護示例。

在這種情況下,您要告訴它連接到smtp.gmail.com ,但是它表明您正在連接到n1plcpnl0057.prod.ams1.secureserver.net ,這是一個GoDaddy服務器-它們正在攔截和重定向您的SMTP通信-因此當然,您的gmail憑據將無法使用。

除此之外,您對addAttachment的使用既錯誤addAttachment安全,但這不是導致連接問題的原因。

順便說一句,您的SMTP記錄以易於解碼的形式顯示您的ID和密碼,因此您應該更改它們。

暫無
暫無

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

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