2017-06-07 28 views
0

我正在搞一個模板,試圖讓我的聯繫表單工作(試圖看看它是如何工作的,發送消息等)。當填寫我的聯繫表格,然後提交,而不是成功,其中一個錯誤彈出:聯繫人表單發送錯誤

電子郵件無法發送,由於一些意外的錯誤。請稍後再試。

原因: 無法實例化郵件功能。

http://prntscr.com/fh5cct

下面是HTML我的聯繫方式:

<div class="col_half col_last"> 

    <div class="fancy-title title-dotted-border test-john"> 
     <h3>Send us an Email</h3> 
    </div> 

    <div class="contact-widget"> 

     <div class="contact-form-result"></div> 

     <form class="nobottommargin" id="template-contactform" name="template-contactform" action="include/sendemail.php" method="post"> 

     <div class="form-process"></div> 

     <div class="col_one_third"> 
      <label for="template-contactform-name">Name <small>*</small></label> 
      <input type="text" id="template-contactform-name" name="template-contactform-name" value="" class="sm-form-control required" /> 
     </div> 

     <div class="col_one_third"> 
      <label for="template-contactform-email">Email <small>*</small></label> 
      <input type="email" id="template-contactform-email" name="template-contactform-email" value="" class="required email sm-form-control" /> 
     </div> 

     <div class="col_one_third col_last"> 
      <label for="template-contactform-phone">Phone</label> 
      <input type="text" id="template-contactform-phone" name="template-contactform-phone" value="" class="sm-form-control" /> 
     </div> 

     <div class="clear"></div> 

     <div class="col_full"> 
      <label for="template-contactform-subject">Subject <small>*</small></label> 
      <input type="text" id="template-contactform-subject" name="template-contactform-subject" value="" class="required sm-form-control" /> 
     </div> 

     <div class="clear"></div> 

     <div class="col_full"> 
      <label for="template-contactform-message">Message <small>*</small></label> 
      <textarea class="required sm-form-control" id="template-contactform-message" name="template-contactform-message" rows="6" cols="30"></textarea> 
     </div> 

     <div class="col_full hidden"> 
      <input type="text" id="template-contactform-botcheck" name="template-contactform-botcheck" value="" class="sm-form-control" /> 
     </div> 

     <div class="col_full"> 
      <button name="submit" type="submit" id="submit-button" tabindex="5" value="Submit" class="button button-3d nomargin">Submit Comment</button> 
     </div> 

     </form> 
    </div> 

</div> 

然後我有sendemail.php文件:

<?php 

require_once('phpmailer/PHPMailerAutoload.php'); 

$toemails = array(); 

$toemails[] = array(
       'email' => '[email protected]', // Your Email Address 
       'name' => 'My Name' // Your Name 
      ); 

// Form Processing Messages 
$message_success = 'We have <strong>successfully</strong> received your Message and will get Back to you as soon as possible.'; 

// Add this only if you use reCaptcha with your Contact Forms 
$recaptcha_secret = 'your-recaptcha-secret-key'; // Your reCaptcha Secret 

$mail = new PHPMailer(); 

// If you intend you use SMTP, add your SMTP Code after this Line 


if($_SERVER['REQUEST_METHOD'] == 'POST') { 
    if($_POST['template-contactform-email'] != '') { 

     $name = isset($_POST['template-contactform-name']) ? $_POST['template-contactform-name'] : ''; 
     $email = isset($_POST['template-contactform-email']) ? $_POST['template-contactform-email'] : ''; 
     $phone = isset($_POST['template-contactform-phone']) ? $_POST['template-contactform-phone'] : ''; 
     $service = isset($_POST['template-contactform-service']) ? $_POST['template-contactform-service'] : ''; 
     $subject = isset($_POST['template-contactform-subject']) ? $_POST['template-contactform-subject'] : ''; 
     $message = isset($_POST['template-contactform-message']) ? $_POST['template-contactform-message'] : ''; 

     $subject = isset($subject) ? $subject : 'New Message From Contact Form'; 

     $botcheck = $_POST['template-contactform-botcheck']; 

     if($botcheck == '') { 

      $mail->SetFrom($email , $name); 
      $mail->AddReplyTo($email , $name); 
      foreach($toemails as $toemail) { 
       $mail->AddAddress($toemail['email'] , $toemail['name']); 
      } 
      $mail->Subject = $subject; 

      $name = isset($name) ? "Name: $name<br><br>" : ''; 
      $email = isset($email) ? "Email: $email<br><br>" : ''; 
      $phone = isset($phone) ? "Phone: $phone<br><br>" : ''; 
      $service = isset($service) ? "Service: $service<br><br>" : ''; 
      $message = isset($message) ? "Message: $message<br><br>" : ''; 

      $referrer = $_SERVER['HTTP_REFERER'] ? '<br><br><br>This Form was submitted from: ' . $_SERVER['HTTP_REFERER'] : ''; 

      $body = "$name $email $phone $service $message $referrer"; 

      // Runs only when File Field is present in the Contact Form 
      if (isset($_FILES['template-contactform-file']) && $_FILES['template-contactform-file']['error'] == UPLOAD_ERR_OK) { 
       $mail->IsHTML(true); 
       $mail->AddAttachment($_FILES['template-contactform-file']['tmp_name'], $_FILES['template-contactform-file']['name']); 
      } 

      // Runs only when reCaptcha is present in the Contact Form 
      if(isset($_POST['g-recaptcha-response'])) { 
       $recaptcha_response = $_POST['g-recaptcha-response']; 
       $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=" . $recaptcha_secret . "&response=" . $recaptcha_response); 

       $g_response = json_decode($response); 

       if ($g_response->success !== true) { 
        echo '{ "alert": "error", "message": "Captcha not Validated! Please Try Again." }'; 
        die; 
       } 
      } 

      $mail->MsgHTML($body); 
      $sendEmail = $mail->Send(); 

      if($sendEmail == true): 
       echo '{ "alert": "success", "message": "' . $message_success . '" }'; 
      else: 
       echo '{ "alert": "error", "message": "Email <strong>could not</strong> be sent due to some Unexpected Error. Please Try Again later.<br /><br /><strong>Reason:</strong><br />' . $mail->ErrorInfo . '" }'; 
      endif; 
     } else { 
      echo '{ "alert": "error", "message": "Bot <strong>Detected</strong>.! Clean yourself Botster.!" }'; 
     } 
    } else { 
     echo '{ "alert": "error", "message": "Please <strong>Fill up</strong> all the Fields and Try Again." }'; 
    } 
} else { 
    echo '{ "alert": "error", "message": "An <strong>unexpected error</strong> occured. Please Try Again later." }'; 
} 

?> 

這就要求這個autoload.php文件:

<?php 
/** 
* PHPMailer SPL autoloader. 
* PHP Version 5 
* @package PHPMailer 
* @link https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project 
* @author Marcus Bointon (Synchro/coolbru) <[email protected]> 
* @author Jim Jagielski (jimjag) <[email protected]> 
* @author Andy Prevost (codeworxtech) <[email protected]> 
* @author Brent R. Matzelle (original founder) 
* @copyright 2012 - 2014 Marcus Bointon 
* @copyright 2010 - 2012 Jim Jagielski 
* @copyright 2004 - 2009 Andy Prevost 
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License 
* @note This program is distributed in the hope that it will be useful - WITHOUT 
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
* FITNESS FOR A PARTICULAR PURPOSE. 
*/ 

/** 
* PHPMailer SPL autoloader. 
* @param string $classname The name of the class to load 
*/ 
function PHPMailerAutoload($classname) 
{ 
    //Can't use __DIR__ as it's only in PHP 5.3+ 
    $filename = dirname(__FILE__).DIRECTORY_SEPARATOR.'class.'.strtolower($classname).'.php'; 
    if (is_readable($filename)) { 
     require $filename; 
    } 
} 

if (version_compare(PHP_VERSION, '5.1.2', '>=')) { 
    //SPL autoloading was introduced in PHP 5.1.2 
    if (version_compare(PHP_VERSION, '5.3.0', '>=')) { 
     spl_autoload_register('PHPMailerAutoload', true, true); 
    } else { 
     spl_autoload_register('PHPMailerAutoload'); 
    } 
} else { 
    /** 
    * Fall back to traditional autoload for old PHP versions 
    * @param string $classname The name of the class to load 
    */ 
    function __autoload($classname) 
    { 
     PHPMailerAutoload($classname); 
    } 
} 

編輯:看起來我必須在這裏編輯/更改幾行代碼並輸入更多數據。

如果您沒有收到表單中的電子郵件,那麼您的服務器配置可能不支持PHP mail()函數。但是你可以使用SMTP認證。

這樣我就可以添加以下$mail = new PHPMailer();下:

$mail->IsSMTP(); 
$mail->Host = "mail.yourdomain.com"; 
$mail->SMTPDebug = 0; 
$mail->SMTPAuth = true; 
$mail->Port = 26; 
$mail->Username = "[email protected]"; 
$mail->Password = "yourpassword"; 

現在,這給了我幾個問題。 mail.yourdomain.com中的mail是什麼?是cPanel的用戶/密碼,我的電子郵件是什麼?

+0

你在本地服務器上嘗試呢? – DCR

+0

我想你需要在你的PHPMail $ mail實例上設置其他屬性 - 除非你爲了安全起見而省略了這些屬性。 – Lando

+0

@DCR No在我的現場託管網站|| Magnus Eriksson - 這是否意味着我需要將表單中的值更改爲'POST'而不是'SUBMIT'? ||蘭登我沒有做到,所以不完全確定是什麼導致 –

回答

0

以下是我將它安放BlueHost的:

function mailerExpressBlueHost(array $mailInputs){ 

     require_once '../includes/phpmailer/PHPMailerAutoload.php'; 

     $mail = new PHPMailer(); 
     $mail->IsMail(); 

     $mail->SetFrom('[email protected]'); // make sure this is an andress 
               // on your domain   
     $mail->IsHTML(true); 
     $mail->addAddress($mailInputs['addAddress']);  
     $body = $mailInputs['body'] ;    
     $mail->isHTML(true); 
     $mail->Subject = $mailInputs['subject'] ; 
     $mail->Body = $body; 

     if(!$mail->send()) { 
      return 'Message could not be sent.' . 'Mailer Error: ' . $mail->ErrorInfo; 
     } else { 
      return 'Message has been sent'; 
     } 

      $mail->ClearAddresses(); 
}