2016-03-05 132 views
-1

林有麻煩通過我的網頁形式發送電子郵件,我使用PHP和PHPMailer的類PHP的電子郵件不工作

我的表單代碼是

<form class="cd-form floating-labels" method="post" action="email.php" enctype="multipart/form-data"> 
    <fieldset> 
     <legend>Contact Us</legend> 

     <div class="icon"> 
      <label class="cd-label" for="cd-name">Name</label> 
      <input class="user" type="text" name="cd-name" id="cd-name" value="<?php if(isset($_POST["cd-name"])){echo $_POST["cd-name"];}else{echo "";}?> required> 
     </div> 

     <div class="icon"> 
      <label class="cd-label" for="cd-company">Company</label> 
      <input class="company" type="text" name="cd-company" id="cd-company" required> 
     </div> 

     <div class="icon"> 
      <label class="cd-label" for="cd-email">Email</label> 
      <input class="email" type="email" name="cd-email" id="cd-email" required> 
     </div> 

     <div class="icon"> 
      <label class="cd-label" for="cd-phone">Contact No</label> 
      <input class="phone" type="text" name="cd-phone" id="cd-phone" required> 
     </div> 


    </fieldset> 

    <fieldset> 
     <div class="icon"> 
      <label class="cd-label" for="cd-message">Project description</label> 
      <textarea class="message" name="cd-message" id="cd-message" required></textarea> 
     </div> 

    <div class='icon'> 
     <label class="cd-upload" for='cd-upload' >Another upload:</label> 
     <input type="file" name='cd-upload' id='cd-upload' /> 
    </div> 

    <div> 
     <p class="cd-select icon"> 
      <select name="cd-hearsay" class="budget"> 
       <option value="0">Where Did You Hear About Us?</option> 
       <option value="1">Google</option> 
       <option value="2">Friend/Family</option> 
       <option value="3">Ad On A Website</option> 
      </select> 
     </p> 
    </div> 

    <div class="icon"> 
      <label class="cd-label" for="cd-coupon">Coupon Code</label> 
      <input class="coupon" type="text" name="cd-coupon" id="cd-name"> 
     </div>    

     <div> 
      <input type="submit" value="Send Message"> 
     </div> 

    </fieldset> 
</form> 

和我email.php代碼

<?php 
require("class.phpmailer.php"); 

// define variables and set to empty values 
$name = $company = $email = $phone = $message = $upload = $hearsay = $coupon = ""; 
$nameErr = $companyErr = $emailErr = $phoneErr = $messageErr = $uploadErr = $hearsayErr = $couponErr = ""; 


function clean($data) { 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 

//clean varibles 
$company = clean($_POST['cd-company']); 
$upload = clean($_POST['cd-upload']); 
$hearsay = clean($_POST['cd-hearsay']); 
$coupon = clean($_POST['cd-coupon']); 

//validate varibles 

//Check Name 
if(empty($_POST['cd-name'])) 
{ 
    $nameErr = "Name Field is Required"; 
} 
elseif(strlen($_POST['cd-name']) <= 3) 
{ 
    $nameErr = "Please provide your full name"; 
} 
elseif(!ctype_alpha($_POST['cd-name'])) 
{ 
    $nameErr = "Please only use letters in your name"; 
} 
else 
{ 
    $name = clean($_POST['cd-name']); 
} 

//Check Email 
if (empty($_POST["cd-email"])) 
{ 
    $emailErr = "Email is required"; 
} 
elseif(!filter_var($_POST["cd-email"], FILTER_VALIDATE_EMAIL)) 
{ 
    $emailErr = "Invalid Email Format"; 
} 
else 
{ 
    $email = clean($_POST["cd-email"]); 
} 

//Check Phone 
if(empty($_POST['cd-phone'])) 
{ 
    $phoneErr = "Phone number is required"; 
} 
elseif(!is_numeric($_POST['cd-phone'])) 
{ 
    $phoneErr = "Please use numers only"; 
} 
else 
{ 
    $phone = clean($_POST['cd-phone']); 
} 

//Check Message 
if(empty($_POST['cd-message'])) 
{ 
    $messageErr = "Message is blank!"; 
} 
elseif(!ctype_alnum($_POST['cd-message'])) 
{ 
    $messageErr = "Please use numbers and letters only";  
} 
else 
{ 
    $message = clean($_POST['cd-message']); 

} 

//Check Upload File Types/Size 
if(!empty($_POST['cd-upload'])) 
{ 
    //Get the uploaded file information 
    $name_of_uploaded_file = basename($_FILES['uploaded_file']['name']); 

    //get the file extension of the file 
    $type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1); 
    $size_of_uploaded_file = $_FILES["uploaded_file"]["size"]/1024;//size in KBs 

    //Settings 
    $max_allowed_file_size = 100; // size in KB 
    $allowed_extensions = array("doc", "xls", "txt"); 

    //Validations 
    if($size_of_uploaded_file > $max_allowed_file_size) 
    { 
     $uploadErr .= "\n Size of file should be less than $max_allowed_file_size"; 
    } 

    //------ Validate the file extension ----- 
    $allowed_ext = false; 
    for($i=0; $i<sizeof($allowed_extensions); $i++) 
    { 
     if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0) 
     { 
     $allowed_ext = true; 
     } 
    } 

    if(!$allowed_ext) 
    { 
     $uploadErr .= "\n The uploaded file is not supported file type. ". 
     " Only the following file types are supported: ".implode(',',$allowed_extensions); 
    } 

    //copy the temp. uploaded file to uploads folder 
    $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file; 
    $tmp_path = $_FILES["uploaded_file"]["tmp_name"]; 

    if(is_uploaded_file($tmp_path)) 
    { 
     if(!copy($tmp_path,$path_of_uploaded_file)) 
     { 
     $uploadErr .= '\n error while copying the uploaded file'; 
     } 
    } 
} 


if(!$nameErr && !$emailErr && !$phoneErr && !$messageErr && !$uploadErr) 
{ 
    echo "no errors"; 
    $mail = new PHPMailer(); 

    $mail->IsSMTP();          // set mailer to use SMTP 
    $mail->Host = "send.one.com"; // specify main and backup server 
    $mail->SMTPAuth = true;  // turn on SMTP authentication 
    $mail->Username = "jswan"; // SMTP username 
    $mail->Password = "secret"; // SMTP password 

    $mail->From = $email; 
    $mail->FromName = $name; 
    $mail->AddAddress("[email protected]", "Looty Jackinson"); 
    #$mail->AddAddress("[email protected]");     // name is optional 
    $mail->AddReplyTo("[email protected]", "Information"); 

    $mail->WordWrap = 50;         // set word wrap to 50 characters 
    $mail->AddAttachment("uploads");   // add attachments 
    #$mail->AddAttachment("uploads/image.jpg", "new.jpg"); // optional name 
    $mail->IsHTML(true);         // set email format to HTML 

    $mail->Subject = "Here is the subject"; 
    $mail->Body = "This is the HTML message body <b>in bold!</b>"; 
    $mail->AltBody = "This is the body in plain text for non-HTML mail clients"; 

    if(!$mail->Send()) 
    { 
     echo "Message could not be sent. <p>"; 
     echo "Mailer Error: " . $mail->ErrorInfo; 
     exit; 
    } 

    echo "Message has been sent"; 
} 
else 
{ 
    echo "errors"; 
    print_r($nameErr); 
    print_r($emailErr); 
    print_r($phoneErr); 
    print_r($uploadErr);  
    #header("index.php"); 
} 
?> 

但這導致下面的錯誤,沒有errorsCould無法訪問文件:上傳警告:require_once(class.smtp.php):未能打開流:在沒有這樣的文件或目錄/客戶/ 2/9/2 /克恩ow_connect.com/httpd.www/contact-form/class.phpmailer.php on line 760致命錯誤:require_once():無法打開所需的'class.smtp.php'(include_path ='。:/ usr/share/php')in /customers/2/9/2/kernow-connect.com/httpd.www/contact-form/class.phpmailer.php on line 760

我真的不確定我在做什麼並非常感謝您的幫助!

您可以在http://www.kernow-connect.com/contact-form/form.php

看到這個問題(一個或多個)如果需要 非常感謝

盧克

+0

無論你有什麼'class.phpmailer.php',它都不是你的'email.php'代碼正在尋找它的地方。嘗試一個完整的路徑,也許? – andrewsi

+0

'class.phpmailer.php'文件在同一目錄下可用? – devpro

+0

感謝您的回覆我已經得到了class.phpmailer.php與email.php相同的目錄我會嘗試添加完整的文件路徑:) – BBLJ84

回答

0

你沒有read the docs我可以張貼PHPMailer.php的內容(自述告訴你你需要知道什麼)或PHPMailer提供的示例代碼,並且你可能使用的是舊版本。自述文件引用:

At the very least you will need class.phpmailer.php. If you're using SMTP, you'll need class.smtp.php, and if you're using POP-before SMTP, you'll need class.pop3.php. For all of these, we recommend you use the autoloader too as otherwise you will either have to require all classes manually or use some other autoloader.

在您發佈之前請先閱讀。