I’m trying to send an html message with an attachment with mail() and am having troubles with the mime encoded message. I’ve looked at the mime encoding and can’t see what is wrong. It sends the email and displays the message. There is a flag in the client saying that the message has an attachment but there’s no attachment. I see the encoded attachment in the mime encoding. I took the example off of php.net but cant get it to look. Any ideas what I’m doing wrong? Here’s the mime encode:
header:
From: [email][email protected]
Reply-To: [email][email protected]
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary=”2-1410065408-1410065408=:30893″
body:
This is a multi-part message in MIME format.
–2-1410065408-1410065408=:30893
Content-Type: multipart/alternative;
boundary=”1-1410065408-1410065408=:76321″
–1-1410065408-1410065408=:76321
Content-Type: text/plain;
charset=”iso-8859-1″
Content-Transfer-Encoding: 7bit
some messaged
–1-1410065408-1410065408=:76321
Content-Type: text/html;
charset=”iso-8859-1″
Content-Transfer-Encoding: 7bit
some messaged
–1-1410065408-1410065408=:76321–
–2-1410065408-1410065408=:30893–
Content-Type: application/octet-stream;
name=”test.zip”
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename=”test.zip”
UEsDBAoAAAAAAAtKczmGphA2BQAAAAUAAAAVAAAATmV3IFRleHQgRG9jdW1lbnQudHh0aGVsbG9Q
SwECFAsKAAAAAAALSnM5hqYQNgUAAAAFAAAAFQAAAAAAAAABACAAAAAAAAAATmV3IFRleHQgRG9j
dW1lbnQudHh0UEsFBgAAAAABAAEAQwAAADgAAAAAAA==
–2-1410065408-1410065408=:30893–
Here’s the class:email_class.php
<?php
class sendEmail {
/**
* sends email message.
* $recipients = object for array of email recipients, current count, and elements for dynamic message.
* $subject = subject line for email
* $from = email address of sender
* $attachment = $_FILES data or sting of headers for attachment. This is processed in the method attachmentHeaders if it is still an array.
* @var string headers
* @var string to
* @var object message
* @var multiarray/string attachment
public $headers;
public $recipients;
public $subject;
public $message;
public $from;
public $to;
public $attachment;
public function __construct () {}
/
* Creates headers and adds attachments if in object. Must have to,subject,message,from to send message.
*
*/
public function sendEmail ($msg = null) {
$msg = nl2br((is_null($msg) ? $this->message : $msg));
$txtMsg = strip_tags($msg);
$num1 = rand(0, 9) . “-” . rand(10000000000, 9999999999) . “-” . rand(10000000000, 9999999999) . “=:” . rand(10000, 99999);
$num2 = rand(0, 9) . “-” . rand(10000000000, 9999999999) . “-” . rand(10000000000, 9999999999) . “=:” . rand(10000, 99999);
//normal headers
$this->headers =<<<AKAM
From: $this->from <$this->from>
Reply-To: $this->from
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary=”$num1″
AKAM;
/
// This two steps to help avoid spam ?
$this->headers .= “Message-ID: <” . implode(”, gettimeofday()) . ” TheSystem@” . $
$this->headers .= “X-Mailer: PHP v” . phpversion() . “rn”;
*/
// With message
$body =<<<AKAM
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary=”$num1″
This is a multi-part message in MIME format.
–$num1
Content-Type: text/plain;
charset=”iso-8859-1″
Content-Transfer-Encoding: 7bit
$txtMsg
–$num1
Content-Type: text/html;
charset=”iso-8859-1″
Content-Transfer-Encoding: 7bit
$msg
–$num1–
AKAM;
// MAIL HEADERS with attachment
if (! is_array($this->attachment) && strlen($this->attachment)>0) {
$this->headers =<<<AKAM
From: $this->from <$this->from>
Reply-To: $this->from
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary=”$num1″
AKAM;
$body =<<<AKAM
This is a multi-part message in MIME format.
–$num1
Content-Type: multipart/alternative;
boundary=”$num2″
–$num2
Content-Type: text/plain;
charset=”iso-8859-1″
Content-Transfer-Encoding: 7bit
$txtMsg
–$num2
Content-Type: text/html;
charset=”iso-8859-1″
Content-Transfer-Encoding: 7bit
$msg
–$num2–
–$num1–
$this->attachment
–$num1–
AKAM;
} elseif (is_array($this->attachment)) {
$this->headers =<<<AKAM
From: $this->from <$this->from>
Reply-To: $this->from
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary=”$num1″
AKAM;
$body =<<<AKAM
This is a multi-part message in MIME format.
–$num1
Content-Type: multipart/alternative;
boundary=”$num2″
–$num2
Content-Type: text/plain;
charset=”iso-8859-1″
Content-Transfer-Encoding: 7bit
$txtMsg
–$num2
Content-Type: text/html;
charset=”iso-8859-1″
Content-Transfer-Encoding: 7bit
$msg
–$num2–
–$num1–
{$this->attachmentHeaders()}
–$num1–
AKAM;
}
//$this->headers .= “rn”.$body;
try {
$mail = mail($this->to, $this->subject, $body, $this->headers, “-f” . $this->from);
if (! $mail) {
throw new Exception(“Email failed for $this->to.”);
}
} catch (Exception $e) {
$errorMessage = date(‘m/d/Y h:i:s’, time()) . $e->getMessage() . “n”;
if (! $file = fopen(‘errorLog.txt’, ‘a+’)) {
$file = fopen(‘errorLog.txt’, ‘w+’);
}
fwrite($file, $errorMessage);
fclose($file);
}
return $mail;
}
/**
* Sends multiple emails off from result
*/
public function sendEmails () {
foreach ($this->recipients->result as $recipient) {
$this->to = $recipient[’email’];
if (! empty($this->to)) {
$this->sendEmail($this->dynamicMessage($recipient));
}
}
}
/
* Also adds headers to this->headers.
*
*/
public function attachmentHeaders () {
$fp = fopen($this->attachment[‘tmp_name’], “r”);
$size = filesize($this->attachment[‘tmp_name’]);
$file = fread($fp, $size);
$file = chunk_split(base64_encode($file));
$ftype = $this->attachment[‘type’];
fclose($fp);
$attachments = ”;
if ($size < 1048576) {
// Attachment headers
$attachments.=<<<ATTA
Content-Type: $ftype;
name=”{$this->attachment[‘name’]}”
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename=”{$this->attachment[‘name’]}”
$file
ATTA;
$this->attachment = $attachments;
}
return $attachments;
}
/**
* returns a dynamic message for recipient based on result data
*
*/
public function dynamicMessage ($recipient) {
$message = $this->message;
foreach ($recipient as $key => $val) {
$message = str_replace(“%$key%”, $val, $message);
}
return $message;
}
}
?>
Thanks in advance for any insight.