PHP Classes

Suggested changes to this class

Recommend this page to a friend!

      cMail  >  All threads  >  Suggested changes to this class  >  (Un) Subscribe thread alerts  
Subject:Suggested changes to this class
Summary:Change how the header and message data is handled
Messages:1
Author:Ron Goral
Date:2013-11-27 16:47:17
 

  1. Suggested changes to this class   Reply   Report abuse  
Picture of Ron Goral Ron Goral - 2013-11-27 16:47:17
First, thank you for this class. It has been very helpful to me. I am hopeful that these suggestions will be helpful to you.

I attempted to implement this code and ran into a couple issues: 1) an attachment would not be attached to the email, rather it would print in the message body; 2) without an attachment, I got errors about incorrect parameters being sent to the php mail function call.

I would suggest making the following four changes to the cMail.mail function: 1) vars are enclosed in {} while part of a non-literal string; 2) introduced the $msg var to contain the email body and attachment; 3) consolidated actions to take if the $file var is not empty; 4) added some double newlines in certain places.

public function mail( $to, $from, $subject, $message, $file = "" ){
$msg = '';
$uid = md5( uniqid( time() ) );
$header = "From: {$from}".$this->newline;
$header .= "Reply-To: {$from}".$this->newline;
$header .= "MIME-Version: 1.0".$this->newline;
$header .= "Content-Type: multipart/mixed; boundary=\"{$uid}\"".$this->newline;

$msg .= "--{$uid}".$this->newline;
$msg .= "Content-type:text/html; charset=\"UTF-8\"".$this->newline;
$msg .= "Content-Transfer-Encoding: 7bit".$this->newline.$this->newline;
$msg .= $message.$this->newline.$this->newline;

if( $file != "" ){
$fn = explode("/", $file);
$filename = $fn[sizeof($fn)-1];
$attachment = chunk_split( base64_encode( file_get_contents( $file ) ) );

$msg .= "--{$uid}".$this->newline;
$msg .= "Content-Type: application/octet-stream; name=\"{$filename}\"".$this->newline;
$msg .= "Content-Transfer-Encoding: base64".$this->newline;
$msg .= "Content-Disposition: attachment; filename=\"{$filename}\"".$this->newline.$this->newline;
$msg .= $attachment.$this->newline.$this->newline;
}

$msg .= "--".$uid."--";
$subject = '=?UTF-8?B?'.base64_encode( $subject ).'?=';
if( mail( $to, $subject, $msg, $header ) ){return true;}
else {return false;}
}