试图创建一个发送电子邮件的功能,但它没有工作..新的PHP ...我做错了什么?

时间:2013-11-01 19:18:49

标签: php function email

function SendEmail() {

    $to      = $email;  
    $subject = 'Sales | Purchase Instructions';  
    $message = ' 

    Thank you for your purchasing 'My Product'.

    Your account information 
    ------------------------- 
    Name:   'Your Name'
    Email:  'Your Email' 

    ------------------------- 

    You can purchase our product by clicking on the purchase button, entering product name, and quantity in the provided boxes, and then click purchase.;  
    $headers = 'From:My Company <me@mycompany.net>' . "\r\n";  


mail($to, $subject, $message, $headers);  
}

2 个答案:

答案 0 :(得分:1)

我认为你的引号有问题。您使用'来封装字符串,但是您也在字符串中使用这些标记,因此它不起作用,解释器不知道字符串的结束位置并引发错误。因此,您应该使用相同的引号来封装您在其中使用的字符串,或者必须转义字符串内的引号(通过在其前面放置\)。我的建议是在字符串周围使用",因为你不在字符串中使用这些引号。

如果使用'"封装字符串,您应该知道存在差异。当您在字符串中使用"变量时,它们将被其值替换,但在您使用'时则不会。

答案 1 :(得分:0)

<?php

$name = 'MonkeyZeus';
$message = 'Hi there '.$name.', <--this is a variable but this is a literal quote inside a quote --> \'';

echo $message;

?>

预期输出为:

你好MonkeyZeus,&lt; - 这是一个变量,但这是引用中的字面引用 - &gt; “