简体   繁体   English

PHP邮件功能错误

[英]PHP Mail Function ERROR

I m Facing problem with PHP mail Function. 我正在面对PHP邮件功能的问题。 The problem is mail is being successfully sent but $body and $ header is not working Properly 问题是邮件已成功发送,但是$ body和$标头无法正常工作

<?php

$name = $_POST['your_name'];

$email = $_POST['your_email'];

$phone = $_POST['your_phone'];

$subject = $_POST['your_subject'];

$msg = $_POST['your_message'];



$to = 'support@morelifelondon.net';

$subject = 'New Enquiry';

$body = $name '|' $email '|' $phone '|' $subject '|' $msg;

$header = 'From: $name <$email>';

if(mail($to, $subject, $body, $header)){
echo "Mail Sent";
}
else{
echo "Mail Failed";
}


?>

Learn basic PHP: 学习基本的PHP:

$body = $name '|' $email '|' $phone '|' $subject '|' $msg;

This is a flat-out syntax error. 这是完全语法错误。 You probably want 你可能想要

$body = $name . '|' . $email . etc....
              ^-----^---- etc...

And

$header = 'From: $name <$email>';
          ^--------------------^---

' -enclosed strings do NOT interpolate variables. '封闭的字符串插入变量。 Use " instead: 使用"代替:

$header = "From: $name <$email>";

Use like this instead: 像这样使用:

$body = $name . '|' . $email . '|' . $phone . '|' . $subject . '|' . $msg;

$header = "From: $name <$email>";
$body = $name '|' $email '|' $phone '|' $subject '|' $msg;

$header = 'From: $name <$email>';

I see 2 errors in those 2 lines: 我在这2行中看到2个错误:

To concatenate string (join pieces of text) into 1 variable, you need a concat operator (in every programming language). 要将字符串(文本片段)连接为1个变量,需要使用concat运算符(每种编程语言都可以)。 For PHP this is the dot (.) . 对于PHP,这是点(。)。

So 所以

$body = $name . '|' . $email . '|' . $phone . '|' . $subject . '|' . $msg;

Second: you are using variables between quotes. 第二:您在引号之间使用变量。 This could work in 2 ways: or you split them out (preferable) or use double quotes (of use a function that does this for you). 这可以通过两种方式起作用:或者将它们分开(最好),或者使用双引号(使用为您执行此操作的函数)。

$header = 'From: ' . $name . ' <' . $email . '>';

Too long messages can be truncated and not work properly. 太长的消息可能会被截断并且无法正常工作。

Add "\\r\\n" after $header = 'From: $name <$email>'; $header = 'From: $name <$email>';之后添加"\\r\\n" $header = 'From: $name <$email>'; , like this: , 像这样:

$header = 'From: $name <$email>'."\r\n";

Also, if your $msg can be long, split it with carriage returns after 100 or so characters. 另外,如果您的$msg可以很长,则用100个左右字符后的回车符将其拆分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM