PHP中的邮件功能

时间:2011-07-01 14:39:53

标签: php email

我正在使用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: info@something.be';

$to     = 'me@gmail.com';
$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>';
        }
}

6 个答案:

答案 0 :(得分:3)

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

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

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

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

答案 1 :(得分:1)

您已使用撇号'打开“消息”字符串,但尝试使用引号"关闭它。 SO语法高亮显示器让它离开!

答案 2 :(得分:1)

您已使用单引号启动变量$message = 'Hello $firstname,并以双引号结束,您需要做的只是制作

$message = "Hello $firstname

如果你把它放在单引号php不会扫描你的变量内容为varible,如$ firstname

答案 3 :(得分:1)

您的$message变量以'开头,但以"结尾,因此其后的所有代码都包含在变量中,直到另一个'为止定义$feedback时会发生。

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

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

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

答案 4 :(得分:1)

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

答案 5 :(得分: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 => "no-reply@yourdomain.com"))
    ->setTo(array($email => "$fname $lname"))
    ->setBody($body);

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