When sending an email, many servers add additional line breaks to limit the length of each line.
How can the original line breaks be recovered when fetching the email in a PHP script?
Example
Suppose I send the following content:
Lorem ipsum Dolore incididunt in culpa ea ea sed quis sint voluptate quis laborum ullamco Excepteur do adipisicing consequat ex in reprehenderit officia in ad deserunt magna nulla dolor laborum occaecat reprehenderit aliquip dolor ea anim ea in veniam adipisicing culpa tempor qui elit voluptate consectetur elit laboris minim consectetur laboris anim incididunt Ut sunt sunt mollit elit irure do cillum dolore consequat in ea culpa ut velit sunt nulla in dolore voluptate dolore laborum reprehenderit dolore ut.
Ut non in veniam enim minim elit ad ut id ad eu voluptate cillum dolor laboris irure tempor mollit dolore exercitation eiusmod ea non ea ullamco nostrud cillum nostrud laborum commodo esse reprehenderit ut deserunt officia do in anim dolore ullamco pariatur ex amet nulla Excepteur mollit officia fugiat eu sed quis nisi fugiat dolor ea commodo ut sunt in consequat consectetur ut nulla pariatur est dolor dolore non ut occaecat officia Duis Ut ex exercitation esse ullamco nulla incididunt commodo pariatur dolore nostrud fugiat id dolor minim non sint amet adipisicing occaecat enim non Ut ad irure sint aliquip nisi ut commodo minim proident elit nulla quis ut ad dolor Excepteur dolore Duis.
Note that there is just one single line break in this text!
Checking the source code of the email at the receiving end using Thunderbird, or fetching the email body via PHP, the content is formatted like this:
Lorem ipsum Dolore incididunt in culpa ea ea sed quis sint voluptate
quis laborum ullamco Excepteur do adipisicing consequat ex in
reprehenderit officia in ad deserunt magna nulla dolor laborum occaecat
reprehenderit aliquip dolor ea anim ea in veniam adipisicing culpa
tempor qui elit voluptate consectetur elit laboris minim consectetur
laboris anim incididunt Ut sunt sunt mollit elit irure do cillum dolore
consequat in ea culpa ut velit sunt nulla in dolore voluptate dolore
laborum reprehenderit dolore ut.
Ut non in veniam enim minim elit ad ut id ad eu voluptate cillum dolor
laboris irure tempor mollit dolore exercitation eiusmod ea non ea
ullamco nostrud cillum nostrud laborum commodo esse reprehenderit ut
deserunt officia do in anim dolore ullamco pariatur ex amet nulla
Excepteur mollit officia fugiat eu sed quis nisi fugiat dolor ea commodo
ut sunt in consequat consectetur ut nulla pariatur est dolor dolore non
ut occaecat officia Duis Ut ex exercitation esse ullamco nulla
incididunt commodo pariatur dolore nostrud fugiat id dolor minim non
sint amet adipisicing occaecat enim non Ut ad irure sint aliquip nisi ut
commodo minim proident elit nulla quis ut ad dolor Excepteur dolore Duis.
Note that each line is limited to a certain length, so 16 additional line breaks are present. These additional line breaks were automatically added somewhere in the chain of events leading to me receiving the email.
I want my email-fetching PHP script to remove the additional line breaks to restore the original two-line format of the content.
I know that the new line breaks are not added in by the PHP script, I know where they come from, what I do not know is how I could make my PHP script remove those line breaks.
Here is the code used to fetch the email body:
$connection = imap_open(
sprintf(
'{%s:110/pop3}INBOX',
Configure::read('Email.Inbox.host')
),
Configure::read('Email.Inbox.email'),
Configure::read('Email.Inbox.password')
);
$mailbox = imap_check($connection);
$messages = imap_fetch_overview($connection, '1:' . $mailbox->Nmsgs);
foreach($messages as $message) {
$content = imap_fetchbody($connection, $message->msgno, 1);
}
What have I tried?
I tried using imap_body
instead of imap_fetchbody
, as the former does not process the email body. But the additional line breaks are already present before that and are indistinguishable from the regular line breaks. Both consist of \r\n
.
I assume there has to be a way to do this, as Thunderbird displays the received email with the correct formatting, without the additional 16 line breaks, although they are present in the source code of the displayed message. So there probably has to be a way to strip the additional 16 line breaks from the email.
Here is a screenshot from Thunderbird which shows the source code of the email on the top and the resulting plain-text display on the bottom.
format=flowed
, which means the mail client CAN ignore the line breaks if it so chooses. – Marc Bstr_replace('\r\n', '', $content);
would replace all line breaks, not only the ones automatically added. – Lars Ebert