*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;
}
}
?>