/    Sign up×
Community /Pin to ProfileBookmark

having troubles mime encoding message

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][/email] <[email protected]>
Reply-To: [email][email protected][/email]
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.
*
$headers = email headers
* $recipients = object for array of email recipients, current count, and elements for dynamic message.
*
$to = email address of recipient
* $subject = subject line for email
*
$message = HTML or text message. template with dynamic content is stored in message->temp and active massage in message->active.
* $from = email address of sender
*
$cc = email address of cc recipient
* $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 object recipients
* @var string to
*
@var string subject
* @var object message
*
@var string from
* @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.
*

* @param string $msg
*
@return boolean $mail
*/
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@” . $_SERVER[‘SERVER_NAME’] . “>rn”;
$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));
}
}
}
/
**

*
Checks for attachement size and adds header for attachment if size is less then 1mb.
* Also adds headers to this->headers.
*

* @param int $num
*
@return string $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
*

* @param array $recipient
*
@return string $message
*/
public function dynamicMessage ($recipient) {
$message = $this->message;
foreach ($recipient as $key => $val) {
$message = str_replace(“&#37;$key%”, $val, $message);
}
return $message;
}
}
?>

Thanks in advance for any insight.

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @lcrane spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...