*注意我发现问题是尝试在用于“发件人”标头值 ( 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 为真,我想要做的是添加一个行空间 (\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;
}
}
?>