Attaching a txt file to an email with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Attaching a txt file to an email with PHP

Post by nielsene »

I'm trying to attach two raw text files to an email using MIME.
I've been able to attach various binary/html files before, but pure text is causing problems.

The source code is at http://ballroom.mit.edu/compinabox/Publ ... xport.phps

The text file contains newlines, which generate an error like

Code: Select all

mhshow: eol encountered in field "138   їsnip], їsnip]  (Boston University) & їsnip], їsnip] (Boston University"
mhshow: message format error in component #3
where I have removed people's names from the error (there are no funny characters in the people's names so its not something there). The first for loop about 2/3 of the way through the file generates this text to include.

I've tried setting the encoding for teh block to base64 and encoding the
data as well as using chunk_split, instead of sending raw 7bit ASCII.

Do I need to use a different content type for a raw text file as an attachment?
mjr
Forum Newbie
Posts: 1
Joined: Fri Oct 25, 2002 1:19 pm

Re: Attaching a txt file to an email with PHP

Post by mjr »

nielsene wrote:I'm trying to attach two raw text files to an email using MIME.
I've been able to attach various binary/html files before, but pure text is causing problems.

The source code is at http://ballroom.mit.edu/compinabox/Publ ... xport.phps

The text file contains newlines, which generate an error like

Code: Select all

mhshow: eol encountered in field "138   їsnip], їsnip]  (Boston University) & їsnip], їsnip] (Boston University"
mhshow: message format error in component #3
where I have removed people's names from the error (there are no funny characters in the people's names so its not something there). The first for loop about 2/3 of the way through the file generates this text to include.

I've tried setting the encoding for teh block to base64 and encoding the
data as well as using chunk_split, instead of sending raw 7bit ASCII.

Do I need to use a different content type for a raw text file as an attachment?
these are functions i use to create html files u may alter it to attach plain texts

Code: Select all

function a2qp($s){
  $res='';
  $len=strlen($s);
  for($i=0;$i!=$len;$i++){
    if($sї$i]>chr(127) or $sї$i]=='=') $res.=('='.dechex(ord($sї$i])));
    else $res.=$sї$i];
  }
  return $res;
};

function SendHtmlMail($from,$to,$subj,$body,$enc){
 global $ENC_NAME;

 $bound="----=_NextPart_".md5(rand()+time());
 $body=recode($body,$enc);
 $body_lat=(($enc=='e' or $enc=='~')?'':recode($body,'~'));
 $subj=recode($subj,$enc);
 if($enc!='e' && $enc!='~'){
   $subj="=?${ENC_NAMEї$enc]}?Q?".a2qp($subj).'?=';
 }
 $mail_body=
   "--$bound\n".
   "Content-Type: text/plain;\n\tcharset="${ENC_NAMEї$enc]}"\n".
   "Content-Transfer-Encoding: quoted-printable\n\n".
   $body.
   "\n\n--$bound\n".
   "Content-Type: text/html;\n\tcharset="${ENC_NAMEї$enc]}"\n".
   "Content-Transfer-Encoding: quoted-printable\n\n".
   a2qp("<html><body bgcolor=white>".nl2br($body)."<br><br>".nl2br($body_lat)."</body></html>").
   "\n\n--$bound\n\n";
 $mail_header=
   "From: $from\n".
   "Content-Type: multipart/alternative;\n\tboundary="$bound"\n".
   "MIME-Version: 1.0\n".
   "X-Mailer: PHP/" . phpversion();

   return mail(
   "$to",
   $subj,
   $mail_body,
   $mail_header
  );
&#125;
Post Reply