Page 1 of 1

mail attachments without PEAR or phpmailer

Posted: Thu Mar 29, 2007 6:10 am
by roscoe
I need to send attachments of WORD files from a form.

I do not have root access, nor path to php.ini, nor any other options but to write this code by hand so we can count these out as the host will not assist. It is 4.31 php without PEAR.

So here goes:

Code: Select all

$mime_boundary = "<<<:" . md5(uniqid(mt_rand(), 1)); 

  $header = "From: $from\r\n"; 
  $header.= "MIME-Version: 1.0\r\n"; 
  $header.= "Content-Type: multipart/mixed;"; 
  $header.= " boundary=\"".$mime_boundary."\"\r\n"; 

  $content = "This is a multi-part message in MIME format.\r\n\r\n"; 

  $content.= "--".$mime_boundary."\r\n"; 
  $content.= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"; 
  $content.= "Content-Transfer-Encoding: 7bit\r\n\r\n"; 
  $content.= strip_tags($message)."\r\n\r\n"; 
  $content.= "--".$mime_boundary."\r\n"; 
  $content.= "Content-Type: ".$file['type']."; name=\"".$file['name']."\"\r\n"; 
  $content.= "Content-Transfer-Encoding: base64\r\n"; 
  $content.= "Content-Disposition: attachment\r\n\r\n"; 
  $content.= $file['data']."\r\n"; 
  $content.= "--" . $mime_boundary . "--\r\n"; 
  $mail=mail($to,$subject,$content,$header,"-f$from"); 

  if (!$mail) return false; 
  else return true; 
}
This bit I hope will send off the attachment (above)

but I need to pick up file and chuck it

Code: Select all

//Attachment data  
$file['name']   //OK I can get that 
$file['type']   //OK I can get that 
$file['data']=chunk_split(base64_encode($row[2]),76); 



$file['data']=preg_replace("/\r?\n|\r/","\r\n",$file['data']); 




$mail=sendmailattachment($to,$subject,$message,$from,$file); 

if($mail) echo "success"; 
else echo "failure";
// DO I NEED TO OPEN AND READ THE ATTACHMENT??

Posted: Thu Mar 29, 2007 6:57 am
by onion2k
You want SwiftMailer.

Posted: Thu Mar 29, 2007 7:21 am
by Chris Corbyn
You need to do more than just read the attachment, you need to encode it and add line breaks at relevant places.

Looking at your headers, they also will not work with many clients and (I forget exactly) may not even comply with the RFC (specifically, your Content-disposition header).

Why are you so opposed to avoiding using third-party code? People spend hundreds of hours writing this stuff so you don't have to.

http://www.swiftmailer.org/

http://www.swiftmailer.org/wikidocs/v3/ ... ttachments

Posted: Thu Mar 29, 2007 7:35 am
by roscoe
It is not that I am opposed to using code already done for the purpose (I am one of the many grateful to all those out there that can do it for us luddites) but I have restrictions by the host company that I cannot change the perameters of the hosting pack and so far using other forums and searches I had not found anything that could help.

I will have a look and see if the product is compatable for the server.

Thank you for your suggestion

Posted: Thu Mar 29, 2007 7:35 am
by stereofrog
roscoe, you don't need root or even shell access to install phpMailer of swift as others suggested.

If, for whatever reason, you want to learn how to code mime messages (no rocket science imo), I recommend you read RFC 2045 first, then send yourself a message and look what its source looks like. Once you understand how MIME works, it's easy to code it properly.

Posted: Thu Mar 29, 2007 8:29 am
by roscoe
Firstly phpmailer does require access to php.ini.

Second, baring in mind I only have access via FTP how do I unpack the bz2 files on the server ? (I have xp and the server is apache)

Posted: Thu Mar 29, 2007 8:53 am
by stereofrog
roscoe wrote:Firstly phpmailer does require access to php.ini.
no, it doesn't
Second, baring in mind I only have access via FTP how do I unpack the bz2 files on the server ? (I have xp and the server is apache)
goto http://phpmailer.sourceforge.net/, download a zip package, unzip it and ftp to your host. That's all about it.

Posted: Thu Mar 29, 2007 9:22 am
by Chris Corbyn
I second that, PHPMailer and Swift Mailer do not require access to php.ini. They will run in Safe mode too. All you have to do is ftp the files to your server. There's no "install" as such.

Posted: Thu Mar 29, 2007 9:42 am
by roscoe
OK chaps, I have had a look again and still have reservations on phpmailer. I didn't need to unzip just my error in reading the install stuff and studying the folders. Apols. :roll:

However the swift is a different matter (just like Ronseal, does what it says on the tin!) , having tested it, I cannot seem to use smtp, but sendmail works and I assume this does not effect attachments. I further went to the swift forum which I must say answered most of my luddite questions. I may have one or 2 if I get stuck down the line.

And yes in desperation having spoken to my hosts this am and posted on Devshed yesterday to somebody who was unable to help, I tried a couple of other forums (this and one other).

If it is any help, I do sometimes (when I am not so pushed) answer those I can in forums.

Many Thanks :idea: