Solved

php mail putting random spaces in email output

Posted on 2013-05-23
4
604 Views
Last Modified: 2013-05-24
Hi,

The script below opens a .csv file replaces the commas and new lines and then emails the output in HTML format. The problem is, on receiving the email it has a random space in the text, for example:

column1|column2|column3|column4|column5|column6
column1|column2|column3|column4|column5|column6
column1|column2|co lumn3|column4|column5|column6
column1|column2|column3|column4|column5|column6
column1|column2|column3|column4|column5|column6

This space roughly appears every 980 characters (with spaces) - This is using MS Outlook.

Writing the variable $new_file to a file and displaying to screen don't show up this issue. Any help would be greatly appreciated.

<?php

$file = file_get_contents("../upload/$filename);

$search = array(",", "\r\n");
$replace = array("|", "<br>");
$new_file = str_replace($search, $replace, $file);

file_put_contents("/tmp/maildebuglog.txt",$new_file);

mail($address, 'Email Subject', $new_file,
        "To: $user <$user@somedomain.com>\n".
        "From: $admin <$admin@somedomain.com>\n".
        "MIME-Version: 1.0\n".
        "Content-type: text/html; charset=iso-8859-1" . "\n");

echo "$new_file";

?>

Open in new window


Thank you.
0
Comment
Question by:bootneck2222
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
Mark Brady earned 500 total points
ID: 39190602
Hmmm that's strange. Line 3 is missing the closing double quote but I'm guessing that's a typo.

$file = nl2br(file_get_contents("../upload/$filename)); // now the <br> are already in the file

// however this does not remove the newline characters

$search = array(",", "\r", "\n");
$replace = array("|");

// by separating the \r and the \n you will catch any occurrences of these characters on their own.

// By having only 1 item in the replace array the 2nd item in the search array will be replace with an empty string by default

// trim
$new_file = trim(str_replace($search, $replace, $file)); // just for safety sake

See how that goes
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39191410
You should probably be including that file as an attachment instead of in the body.  According to the Internet Message Format RFC the latest of which is 5322:
There are two limits that this standard places on the number of characters in a line. Each line of characters MUST be no more than 998 characters, and SHOULD be no more than 78 characters, excluding the CRLF.

http://stackoverflow.com/questions/11794698/max-line-length-in-mail

The normal method for content that doesn't have a line length like images is to use MIME boundaries and chunked base64 encoding which all mail clients understand.  That is also how attachments are included in emails.  If you specify it properly, you can put the whole message in base64 encoded format and completely get around line length problems.
0
 

Author Closing Comment

by:bootneck2222
ID: 39194377
Thanks elvin66 for answer and introducing me to the nl2br function.
0
 
LVL 20

Expert Comment

by:Mark Brady
ID: 39194728
You're welcome. Just keep in mind that nl2br() only adds the <br> element into the text but does not remove the newline characters so you need to do a str_replace to remove them (if you need to).

Good luck
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question