duanguoping2016 2013-04-02 16:53
浏览 37
已采纳

条件语句中带行间距的PHP变量

*NOTE I found that the problem was trying to enter a space/line break in the email variable used for the "From" header value (duh). This of course can't work. So instead I appended the values to the message body. See correct code bellow.

<?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 . "

" . $f_name . "can be reached on:
";

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 .= "
";
}

if(($_POST["telefon"]) == "telefon" || ($_POST["telefon"]) == ""){
    $f_phone_check = "";

    }else{
    $f_phone_check = cleanupentries($_POST["telefon"]);
    $message .= $f_phone_check;
    $message .= "
";
}

$message .= "
Technical info:
" . $from_ip . "," . $from_browser;

$headers = "From: " . $f_email . "
" .
    "Reply-To: " . $f_email . "
" .
    "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;
    }
}
?>

-------------- ORIGINAL POST --------------

I've been trying back and forth to get this to work, but no matter what I do I can't get it to work. What am I doing wrong here (see code bellow)?

What I'm trying to do is to add a line space ( ) if the else is true, otherwise it should display nothing. The displaying nothing part works just fine, its the adding of a line space that I can't get to work. If I add $f_email_check .= " "; the email doesn't get sent.

I've also tried $f_email_check = cleanupentries($_POST["epost"]." "); same problem.

And same thing with $f_email_check = cleanupentries($_POST["epost"])." ";.

If I completely remove the part the mail sends just fine. However, since the email and phone (that comes after the email) is on the same line with no space in the message body the email and phone go a as one long string. I know I can enter a or space in the message body, but then there will be a line space even if the email is empty in the message body.

What I'm trying to achieve is, if there is no email given, present nothing (in regards to email), if both phone AND email are given, put email on one line then on the next line show phone...

This is the full code I'm using

<?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"]."
");
    $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 . "

" . $f_name . "can be reached on
" . $f_email_check . "" . $f_phone_check . "

Technical info:
" . $from_ip . "," . $from_browser;

$headers = "From: " . $f_email . "
" .
    "Reply-To: " . $f_email . "
" .
    "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;
    }
}
?>
  • 写回答

2条回答 默认 最新

  • dongsheng6056 2013-04-02 16:55
    关注

    You seem to be sending an HTML formatted e-mail. HTML ignores whitespace.

    Use <br> to get a newline. You can also put nl2br() in your cleanupentries function but that might not be what you intended.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?