2012-04-22 179 views
0

我有一個聯繫表單,但郵件沒有發送到我已經聲明的電子郵件。這是submit.php文件:電子郵件沒有發送

<?php 

/* config start */ 

$emailAddress = '[email protected]'; 

/* config end */ 

require "../php/class.phpmailer.php"; 

session_name("fancyform"); 
session_start(); 


foreach($_POST as $k => $v) 
{ 
    if(ini_get('magic_quotes_gpc')) 
     $_POST[$k] = stripslashes($_POST[$k]); 

    $_POST[$k] = htmlspecialchars(strip_tags($_POST[$k])); 
} 


$err = array(); 

if(!checkLen('name')) 
    $err[] = 'The name field is too short or empty!'; 

if(!checkLen('email')) 
    $err[] = 'The email field is too short or empty!'; 
elseif(!checkEmail($_POST['email'])) 
    $err[] = 'Your email is not valid!'; 

if(!checkLen('subject')) 
    $err[] = 'You have not selected a subject!'; 

if(!checkLen('message')) 
    $err[] = 'The message field is too short or empty!'; 

if((int)$_POST['captcha'] != $_SESSION['expect']) 
    $err[] = 'The captcha code is wrong!'; 


if(count($err)) 
{ 
    if($_POST['ajax']) 
    { 
     echo '-1'; 
    } 

    else if($_SERVER['HTTP_REFERER']) 
    { 
     $_SESSION['errStr'] = implode('<br />', $err); 
     $_SESSION['post'] = $_POST; 

     header('Location: '.$_SERVER['HTTP_REFERER']); 
    } 

    exit; 
} 

$msg = 
'Name: '.$_POST['name'].'<br /> 
Email: '.$_POST['email'].'<br /> 
IP: '.$_SERVER['REMOTE_ADDR'].'<br /><br /> 

Message:<br /><br /> 

'.nl2br($_POST['message']).' 

'; 

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

$mail->AddReplyTo($_POST['email'], $_POST['name']); 
$mail->AddAddress($emailAddress); 
$mail->SetFrom($_POST['email'], $_POST['name']); 
$mail->Subject = "A new ".mb_strtolower($_POST['subject'])." from ".$_POST['name']." | contact form feedback"; 

$mail->MsgHTML($msg); 

$mail->Send(); 


unset($_SESSION['post']); 

if($_POST['ajax']) 
{ 
    echo '1'; 
} 
else 
{ 
    $_SESSION['sent'] = 1; 

    if($_SERVER['HTTP_REFERER']) 
     header('Location: '.$_SERVER['HTTP_REFERER']); 

    exit; 
} 

function checkLen($str, $len = 2) 
{ 
    return isset($_POST[$str]) && mb_strlen(strip_tags($_POST[$str]), "utf-8") > $len; 
} 

function checkEmail($str) 
{ 
    return preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $str); 
} 

?> 

如果submit.php在瀏覽器中打開,我得到了以下錯誤消息:


警告:要求(../ PHP/class.phpmailer。 PHP)[function.require]:未能打開流: 在 /home/content/96/9227096/html/submit.php沒有這樣的文件或目錄上線
致命錯誤:require()[function.require]:需要打開失敗 '../php/class.phpmailer.php' (include_path ='。:/ usr/local/php5_3/lib/php')in在線

/home/content/96/9227096/html/submit.php我也通過我的託管服務器告訴我,我可能需要添加下面的中繼服務器在我的代碼: (但我不知道在哪裏) relay-hosting.secureserver.net

+1

這個錯誤很明顯地說明它沒有加載所需的PHP文件('../ php/class.phpmailer.php')。它甚至從來沒有得到與電子郵件服務器交互的代碼。 – David 2012-04-22 15:07:49

回答

0

聲明require "../php/class.phpmailer.php";意味着../php/class.phpmailer.php文件將被加載,如果它不能被加載,那麼腳本將終止。

無法加載該腳本。這可能是由於多種原因。也許它不在那裏。也許你提供的路徑是錯誤的。也許這是一個文件許可問題。你必須弄清楚這一部分。

但由於無法加載,因此腳本會終止並顯示錯誤消息。

+0

好吧class.phpmailer.php是在php文件夾中,並提交在另一個文件夾,所以我100%確定文件路徑是正確的。它可以是其他的嗎? – user1165861 2012-04-22 15:11:31

0

我在某些託管服務提供商遇到類似的問題。即使路徑很好,要求不包含文件並終止整個腳本。你可以寫一個真正的路徑到你的文件,也許這會有所幫助。因爲這個問題,我開始使用

require(getcwd().'actual/path/relevant/to/index.php'); 

和starder寫在一個類中,類同Java或C#桌面應用程序的應用程序。