Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a PHP string to send with PHPMailer

Tags:

php

phpmailer

I have a contact form that sends a confirmation mail to the person that filled the form using PHPMailer. The mail is sent without line breaks, so I am trying to format the message sent, I have 2 problems.

  1. There are no line breaks.
  2. I have a textarea for inputting the message. Same problem, the message is sent without the line breaks.

Here is the code that sends the message

 $msg =  'Hello! ' . $from_name . 'Thank you...! For Contacting Us. ' . "\r\n" .    
         'Subject: ' . $email_subject . "\r\n" .
         'Message: ' . $email_message . "\r\n" .
         'This is a Contact Confirmation mail. We Will contact You as soon as possible' . "\r\n";    

This is the variable that is sent through PHPMailer, the mail is not sent, there is an error (I don't know which, I just catch that the mail was not sent successfully). if I don't add the "\r\n" there are no problems.
The variable $email_message contains the textarea (also sent without line breaks).

like image 993
Daniel Ben-Shabtay Avatar asked Dec 03 '25 10:12

Daniel Ben-Shabtay


1 Answers

HTML does not preserve white space, which is why you're not seeing line breaks. You can tell PHPMailer not to use HTML like this:

$mail->isHTML(false);
$mail->Body = $msg;

When you send that your line breaks will be preserved.

Alternatively, keep it as HTML, but wrap your paragraphs in <p> tags or add line breaks with <br> tags.

like image 127
Synchro Avatar answered Dec 05 '25 01:12

Synchro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!