I'm not sure what I am doing wrong here. In the emails, keeps showing every time there is a line break. How can I modify the code to fix it?
public function sendSupportEmail($email, $name, $comments)
{
// Wait until Google Apps are configured to accept from this domain
//$to = "test@mail.com";
$to = "test@mail.com.com";
$subject = "Support: Support Inquiry";
// Headers
// To send HTML mail, you can set the Content-type header.
$autoHeaders = "MIME-Version: 1.0
";
$autoHeaders .= "Content-type: text/html; charset=iso-88591
";
$autoHeaders .= "From: Web Bot";
$autoHeaders .= "<webbot@mail.com>
";
$autoHeaders .= "Reply-To: webbot@mail.com
";
$autoHeaders .= "Return-Path: webbot@mail.com
";
$autoHeaders .= "X-Mailer: PHP 5.x
";
// Print the local date
$date = new DateTime('now', new DateTimeZone('America/Denver'));
$datePrint = $date->format('F j, Y, g:i a');
// Create Text Based Message Below
$message = "<h3>Support Inquiry sent on {$datePrint}</h3>";
$message .= "<b>Name:</b><br>{$name}<br><br>";
$message .= "<b>Email:</b><br><a href='mailto:{$email}'>{$name}</a><br><br>";
$message .= "<b>Comments:</b><p>{$comments}</p>";
// Send them the E-Mail
return mail($to, $subject, $message, $autoHeaders);
}
The ideal function to use here would be PHP's nl2br()
function.
and used against the textarea input (or other inputs/variables) wherever you are using it. This has been established in comments that that is what you are using for the comments form element.
I.e.: and assuming that your form element is named "comments" and using a POST method for it, since we don't know what that is, or where $comments
has been assigned as. That wasn't in your question, therefore I am submitting the following as a possible solution.
$comments = $_POST['comments'];
$comments = nl2br($comments);
Or, all in one go:
$comments = nl2br($_POST['comments']);
As you are sending HTML emails, You need to replace with < br >'s in email body.
Before returning mail, you should use str_replace function of php as follows.
.........
.........
$message .= "<b>Comments:</b><p>{$comments}</p>";
$message = str_replace("
", "<br>", $message);
// Send them the E-Mail
return mail($to, $subject, $message, $autoHeaders);