Html mail gets broken by new lines I am using the Mail system and the Swift mailer* modules to send HTML emails. I then implemented a custom module to send HTML newsletters to users, and I noticed the emails didn't look as expected: Many styles kept broking randomly.

After digging, I discovered the problem was with the string of email randomly being broken by new lines: If a new line came in a critical position in the string, the whole mail would be broken. I needed to know where these strange new lines were coming from.

It turned out I am not doing anything to string expect this.

$body = 'some html code without new lines';
$body .= 'some more html code without new lines';
$body .= 'some more and some more html code without new lines';

$params['body'] = Markup::create($body);
$language = $recipient['lang'];
$to = $recipient['mail'];
$params['to'] = $to;
$result = $mailManager-&gt;mail('custom_newsletter_mail', 'newsletter', $to, $language, $params, $from, $send_bool);


I don't know what Markup::create($body) does, so I tried removing it. The email was delivered with no new lines, but the HTML was not rendered.

In mail system configuration, I set swiftmailer as FORMATTER and default PHP mailer as SENDER.

This is the custom module's .module file.

function custom_newsletter_mail_mail($key, &amp;$message, $params) {
  $message['body'] = [];
  $message['subject'] = [];
  $message['format'] = 'text/html';
  $message['subject'] = $params['title'];
  $message['body'][] = $params['body'];
  $message['headers'] = array(
    'content-type' =&gt; 'text/html',
    'charset' =&gt; 'UTF-8',
    'format' =&gt; 'flowed',
    'delsp' =&gt; 'yes',
    'from' =&gt; $params['from']
  );
}


I found out that my testing environment send emails with no new lines (Email looks as expected), while my production environment doesn't. I double checked every setting; both are identical as far as I can tell.

I tried using mime mail instead of swift mailer and it WORKED!! there is no newlines or spaces (The HTML mail actually looks good), BUT mime mail only reaches live mails. It doesn't work with yahoo or gmail for some reason.

I need to fix this. I need to remove those annoying new lines in the email body, which I feel it's a swift mailer issue.
You cannot send email without line breaks.

The SMTP standard requires that lines be limited to 1000 characters. 998 really because the line must end with "\r\n".

If you successfully avoid having line breaks inserted for you, what will happen is that many email servers on the Internet will reject your email. Some will accept it, because they decided to ignore the standards. Guess who does that.

Many servers will not.

The correct answer to this problem is to create HTML email that has line breaks. This is really easy to do. Pick a few elements that occur fairly often, like &lt;/tr&gt; if you use tables, and CSS rule end braces, and put a newline after it.

Reviewing your HTML will also help you avoid very stupid looking HTML text, if you care. I was just viewing source on a newsletter email from TweakTown where they first include a bunch of CSS rules at the top. Then every single table row repeats the same CSS as inline styles. Really. Why? No wonder their lines are more than 1,000 characters long.

So, are people intentionally creating HTML with no line breaks to save space? Because if so, CSS rule spam is the opposite of saving space.