首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Php Mailer / GoDaddy

Php Mailer / GoDaddy
EN

Stack Overflow用户
提问于 2016-01-08 00:11:48
回答 2查看 615关注 0票数 0

我通过GoDaddy上传了一个用html编码的带有附件选项的联系人表单页面,并嵌入了php (contact.php),但是当我想要填写表单并提交它时,我收到了许多我不理解的错误。

我正在使用php mailer和一个php包含到另一个php页面(bootstrap.php)从我的联系页面。

代码如下:

联系页面:

代码语言:javascript
复制
<!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):

代码语言:javascript
复制
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;
}
}
?>

以下是提交表单时出现的错误消息:

代码语言:javascript
复制
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行中的代码是

代码语言:javascript
复制
header("Location: contact.php?status=thanks");

谢谢你的帮助。

EN

回答 2

Stack Overflow用户

发布于 2016-01-08 01:19:18

您的问题是在第一个参数中传递给addAttachment()方法的是整个$_FILES['attachment']数组,而不是一个包含文件路径的字符串。因此出现了错误消息!

此文件位置的路径在$_FILES['attachment']['tmp_name']中,因为您尚未将上传的文件从临时存储移动到永久文件位置。

参数2虽然是可选的,但也可以根据需要传递,可以在$_FILES['attachment']['name']中找到

所以像这样修改这段代码

代码语言:javascript
复制
//Add attachment
$mail->addAttachment($_FILES['attachment']['tmp_name'],
                     $_FILES['attachment']['name']);

该方法的原型是:

代码语言:javascript
复制
public function AddAttachment($path,
                              $name = '',
                              $encoding = 'base64',
                              $type = 'application/octet-stream')
票数 0
EN

Stack Overflow用户

发布于 2016-01-08 01:24:06

你有没有试过阅读错误信息?它包括一个故障排除指南的链接,该指南涵盖了GoDaddy和如何连接到gmail。您的代码基于一些随机的、过时的示例,而不是PHPMailer提供的维护示例。

在这种情况下,您告诉它连接到smtp.gmail.com,但它显示您连接到n1plcpnl0057.prod.ams1.secureserver.net,这是一个GoDaddy服务器-他们正在拦截并重定向您的SMTP流量-所以您的gmail凭据当然不起作用。

除此之外,您对addAttachment的使用既是错误的,也是不安全的,但这不是导致连接问题的原因。

顺便说一句,您的SMTP文本以一种易于解码的形式显示了您的ID和密码,因此您应该更改它们。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34659926

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档