mail attachments without PEAR or phpmailer

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
roscoe
Forum Commoner
Posts: 85
Joined: Tue Aug 05, 2003 10:24 am
Location: essex uk

mail attachments without PEAR or phpmailer

Post 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??
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

You want SwiftMailer.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
roscoe
Forum Commoner
Posts: 85
Joined: Tue Aug 05, 2003 10:24 am
Location: essex uk

Post 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
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post 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.
roscoe
Forum Commoner
Posts: 85
Joined: Tue Aug 05, 2003 10:24 am
Location: essex uk

Post 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)
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
roscoe
Forum Commoner
Posts: 85
Joined: Tue Aug 05, 2003 10:24 am
Location: essex uk

Post 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:
Post Reply