2011-07-01 74 views
1

我使用PHP发送电子邮件。电子邮件中的值取决于表单的输入。但由于某种原因,邮件突然没有发送。它之前做过。我的代码有什么问题?PHP中的邮件功能

订单被正确放置在数据库中,所以没有错误。

if ($order->addOrder($_DB)) { 
$user  = "SafetyCam.be";        
$verzonden  = FALSE;        

    $firstname = $_POST['firstname']; 
    $lastname = $_POST['lastname']; 
    $address = $_POST['address']; 
    $zip  = $_POST['zip']; 
    $place  = $_POST['place']; 
    $country = $_POST['country']; 
    $phone  = $_POST['phone']; 
    $email  = $_POST['email']; 
    $twogig = $_POST['vn_qty']; 
    $fourgig = $_POST['ja_qty']; 
    $total  = $_POST['total']; 

    $totaal = (($twogig * 50) + ($fourgig *80) + 2.5); 

    $headers = 'From: [email protected]'; 

$to  = '[email protected]'; 
$subject = 'Bevestiging bestelling'; 
$message = 'Hello $firstname, 

     You placed the following order on our website. 

     - $twogig x 2GB SafetyCams ordered 
     - $fourgig x 4GB SafetyCams ordered 
     + shippingcosts (2,5 EUR) 

     The total cost for this order amounts to $totaal EUR. 

     Your products will be delivered as quickly as possible after receipt of your payment. 
     Please transfer the order amount of $totaal EUR into account X.      

     After payment, the products will be sent to the following address: 

     $firstname $lastname 
     $address 
     $zip $place 
     $country 

     We hope you will be very happy with your purchase. 

     Sincerely yours"; 


    if (mail($to, $subject, $message, $headers)) { 
       $verzonden = TRUE; 
       $feedback = '<div class="feedbackheader">Thanks</div><br/>'; 

      } else { 
       $verzonden = FALSE; 
       $feedback = '<div class="feedbackheader">Error!</div>';    } 
      } 
     else { 
      $feedback = '<div class="feedbackheader">Error!</div>'; 
     } 
} 
+0

的代码是不完整的,请粘贴代码休息... – fdaines

+0

你不认真消毒用户输入的数据? –

+0

对不起,代码是完整的,但它有格式问题.. – fdaines

回答

0

我用SwiftMailer:

require_once('../lib/swiftMailer/lib/swift_required.php'); 
function sendEmail(){ 
    //Sendmail 
    $transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs'); 

    //Create the Mailer using your created Transport 
    $mailer = Swift_Mailer::newInstance($transport); 

    $body="Dear $fname,\n\nYour job application was successful. \n\nYours,\n\nEamorr\n\n\n\n\n\n\n"; 


    //Create a message 
    $message = Swift_Message::newInstance('Subject goes here') 
    ->setFrom(array($email => "[email protected]")) 
    ->setTo(array($email => "$fname $lname")) 
    ->setBody($body); 

    //Send the message 
    $result = $mailer->send($message); 
} 
3

为什么用单引号打开邮件$信息并以双引号结尾。

你应该用双引号打开和结束,特别是因为你在里面使用PHP变量。

$message = 'Hello $firstname"; //Wrong 

$message = "Hello $firstname"; // Works 
+0

好吧,有道理,但没有帮我出去... – Michiel

1

你已经打开了“消息”字符串撇号',但试图以引号"关闭它。 SO语法突出显示器将其删除!

1

你用单引号开始你的变量$message = 'Hello $firstname,,并用双引号结束它,你需要做的就是让

$message = "Hello $firstname 

,如果你把它放在单引号PHP不会扫描varible您的变量的内容像$名字

+0

太糟糕了,没有工作.. – Michiel

1

$message变量开始以'绳子,但与"结束它,所以它后面的所有代码包含在变,直到另一个'当你定义$feedback恰好。

基本上你没有关闭字符串,因此你的整个代码都在改变。如果你使用颜色编码,你应该看到这个(我可以从你的问题中看到它)。

此外,如果您使用单引号,则不能使用内联变量。

$var = 1; 
echo '$var'; // ouput: $var; 
echo "$var"; // output: 1 
1

您使用单引号(')开始您的消息字符串并尝试用双引号结尾,因此您的逻辑被错误地解析。