*的注意 我发现问题是尝试在用于“From”标头值( duh )的电子邮件变量中输入空格/换行符。这当然不能奏效。因此,我将值附加到邮件正文。请参阅下面的正确代码。
<?php
$send_to = "[MY@EMAIL.COM]";
$send_subject = "contact form from website";
$f_name = cleanupentries($_POST["namn"]);
$f_message = cleanupentries($_POST["meddelande"]);
$from_ip = $_SERVER['REMOTE_ADDR'];
$from_browser = $_SERVER['HTTP_USER_AGENT'];
function cleanupentries($entry) {
$entry = trim($entry);
$entry = stripslashes($entry);
$entry = htmlspecialchars($entry);
return $entry;
}
$message = $f_message . "\n\n" . $f_name . "can be reached on:\n";
if(($_POST["epost"]) == "epost" || ($_POST["epost"]) == ""){
$f_email = $send_to;
$f_email_check = "";
}else{
$f_email_check = cleanupentries($_POST["epost"]);
$f_email = $f_email_check;
$message .= $f_email_check;
$message .= "\n";
}
if(($_POST["telefon"]) == "telefon" || ($_POST["telefon"]) == ""){
$f_phone_check = "";
}else{
$f_phone_check = cleanupentries($_POST["telefon"]);
$message .= $f_phone_check;
$message .= "\n";
}
$message .= "\nTechnical info:\n" . $from_ip . "," . $from_browser;
$headers = "From: " . $f_email . "\r\n" .
"Reply-To: " . $f_email . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if (!$f_email) {
echo '<p>No email.</p><br /><p>< <a style="text-decoration: none; color: blue;" href="/" target="_self">Pleas try again</a></p>';
exit;
}else if (!$f_name){
echo '<p>No name.</p><br /><p>< <a style="text-decoration: none; color: blue;" href="/" target="_self">Pleas try again</a></p>';
exit;
}else{
if (filter_var($f_email, FILTER_VALIDATE_EMAIL)) {
mail($send_to, $send_subject, $message, $headers);
echo "true";
}else{
echo '<p>Invalid email.</p><br /><p>< <a style="text-decoration: none; color: blue;" href="/" target="_self">Pleas try again</a></p>';
exit;
}
}
?>
--------------原始帖子--------------
我一直在尝试让它发挥作用,但无论我做什么,我都无法让它发挥作用。我在这里做错了什么(见下面的代码)?
我要做的是在else为true时添加一个行间距(\ n),否则它什么都不显示。显示没有任何部分工作正常,它添加了一个我无法工作的行空间。
如果我添加$f_email_check .= "\n";
,则不会发送电子邮件。
我也尝试过$f_email_check = cleanupentries($_POST["epost"]."\n");
同样的问题。
与$f_email_check = cleanupentries($_POST["epost"])."\n";
相同。
如果我完全删除\ n部分邮件发送就好了。但是,由于电子邮件和电话(在电子邮件之后)位于同一行,邮件正文中没有空格,因此电子邮件和电话将作为一个长字符串。我知道我可以在邮件正文中输入\ n或空格,但即使邮件正文中的电子邮件为空,也会有行空格。
我想要实现的是,如果没有给出电子邮件,则不提供任何信息(关于电子邮件),如果同时给出电话和电子邮件,请将电子邮件放在一行然后在下一行显示电话上。
这是我正在使用的完整代码
<?php
$send_to = "[MY@EMAIL.COM]";
$send_subject = "webpage contactform";
$f_name = cleanupentries($_POST["namn"]);
if(($_POST["epost"]) == "epost" || ($_POST["epost"]) == ""){
$f_email = $send_to;
$f_email_check = "";
}else{
$f_email_check = cleanupentries($_POST["epost"]."\n");
$f_email_check .= "";
$f_email = $f_email_check;
}
if(($_POST["telefon"]) == "telefon" || ($_POST["telefon"]) == ""){
$f_phone_check = "";
}else{
$f_phone_check = cleanupentries($_POST["telefon"]);
}
$f_message = cleanupentries($_POST["meddelande"]);
$from_ip = $_SERVER['REMOTE_ADDR'];
$from_browser = $_SERVER['HTTP_USER_AGENT'];
function cleanupentries($entry) {
$entry = trim($entry);
$entry = stripslashes($entry);
$entry = htmlspecialchars($entry);
return $entry;
}
$message = $f_message . "\n\n" . $f_name . "can be reached on\n" . $f_email_check . "" . $f_phone_check . "\n\nTechnical info:\n" . $from_ip . "," . $from_browser;
$headers = "From: " . $f_email . "\r\n" .
"Reply-To: " . $f_email . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if (!$f_email) {
echo '<p>No email.</p><br /><p>< <a style="text-decoration: none; color: blue;" href="/" target="_self">Please try again</a></p>';
exit;
}else if (!$f_name){
echo '<p>No name.</p><br /><p>< <a style="text-decoration: none; color: blue;" href="/" target="_self">Please try again</a></p>';
exit;
}else{
if (filter_var($f_email, FILTER_VALIDATE_EMAIL)) {
mail($send_to, $send_subject, $message, $headers);
echo "true";
}else{
echo '<p>Invalid email.</p><br /><p>< <a style="text-decoration: none; color: blue;" href="/" target="_self">Please try again</a></p>';
exit;
}
}
?>
答案 0 :(得分:3)
您好像正在发送HTML格式的电子邮件。 HTML忽略空格。
使用<br>
获取换行符。您也可以将nl2br()
放入cleanupentries
函数中,但这可能不是您想要的。
答案 1 :(得分:0)
使用FILTER_VALIDATE_EMAIL阅读filter_var手册(注释)并不是最佳选择(http://php.net/manual/en/function.filter-var.php)。
而不是:
if (filter_var($f_email, FILTER_VALIDATE_EMAIL)) {
mail($send_to, $send_subject, $message, $headers);
echo "true";
}else{
echo '<p>Invalid email.</p><br /><p>< <a style="text-decoration: none; color: blue;" href="/" target="_self">Please try again</a></p>';
exit;
}
我会尝试类似的事情:
try {
mail($send_to, $send_subject, $message, $headers);
}
catch (Exception $e) {
throw new Exception($e->__toString();
}
OR
$mail = mail($send_to, $send_subject, $message, $headers);
//check result of $mail.
//...