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->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, &$message, $params) {
  $message['body'] = [];
  $message['subject'] = [];
  $message['format'] = 'text/html';
  $message['subject'] = $params['title'];
  $message['body'][] = $params['body'];
  $message['headers'] = array(
    'content-type' => 'text/html',
    'charset' => 'UTF-8',
    'format' => 'flowed',
    'delsp' => 'yes',
    'from' => $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.

1 Answer 1


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 </tr> 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.

  • It is fine if the email contains extremely long lines as long as they are chunked during transport. In other words, the client may see the long lines, but the server must not see them. Please google Content-Transfer-Encoding: base64 and the PHP functions chunk_split(base64_encode($mail_content)); for more information.
    – Gogowitsch
    Commented Jun 9, 2019 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.