/    Sign up×
Community /Pin to ProfileBookmark

Sending multipart mail with attachements

I am playing with sending mail through php’s mail(). It sends the mail just fine, but all the MIME types, the boundry markers, and what-not are being displayed as though they are part of the email. Rather than seeing attached files I see the file data in the message.

I would be grateful if someone could point me in the right direction, below is the code I’m playing w/:

[code=php]<?php

CLASS EMAIL {
PRIVATE $hasAttachments;
PRIVATE $to, $from, $subject, $message, $html;
PRIVATE $boundry, $filedata, $filename, $filetype;

PUBLIC FUNCTION __construct( $to, $subject, $message, $from = NULL ) {
$this->boundry = ‘macizo’. substr( md5( time() ), 7 );
$this->hasAttachments = FALSE;
$this->to( $to );
$this->subject( $subject );
$this->message( $message );
$this->from = $from;
}

PUBLIC FUNCTION attach( $filename ) {
$pos = strrpos( $filename, ‘.’ );
$extension = substr( $filename, $pos+1 );
SWITCH ( strtolower( $extension ) ) {
CASE ‘mp3’: $mimetype = ‘audio/mpeg’; BREAK;
CASE ‘pdf’: $mimetype = ‘application/pdf’; BREAK;
CASE ‘png’: $mimetype = ‘image/png’; BREAK;
CASE ‘zip’: $mimetype = ‘application/zip’; BREAK;
DEFAULT: $mimetype = ‘application/octet-stream’;
}
IF ( file_exists( $filename ) ) {
$this->hasAttachments = TRUE;
$index = count( $this->filedata );
$this->filedata[$index] = chunk_split(base64_encode(file_get_contents($filename)));
$this->filename[$index] = basename( $filename );
$this->filetype[$index] = $mimetype;
}
}

PUBLIC FUNCTION attachments() {
RETURN $this->hasAttachments;
}

PUBLIC FUNCTION from( $from = NULL ) {
IF ( $from != NULL ) $this->from = filter_var( $from, FILTER_SANITIZE_EMAIL );
RETURN $this->from;
}

PUBLIC FUNCTION html( $bool ) {
$bool = strtolower( $bool );
$types = array( TRUE, 1, ‘on’, ‘y’, ‘yes’ );
$this->html = ( in_array($bool, $types) ) ? TRUE : FALSE;
}

PUBLIC FUNCTION message( $message = NULL ) {
IF ( $message != NULL ) $this->message = $message;
RETURN $this->message;
}

PUBLIC FUNCTION subject( $subject = NULL ) {
IF ( $subject != NULL ) $this->subject = $subject;
RETURN $this->subject;
}

PUBLIC FUNCTION to( $to = NULL ) {
IF ( $to != NULL ) $this->to = filter_var( $to, FILTER_SANITIZE_EMAIL );
RETURN $this->to;
}

PUBLIC FUNCTION send() {

IF ( $this->attachments() ) {
$mime = “rnContent-Type: multipart/mixed;”;
} ELSEIF( $this->html() ) {
$mime = “rnContent-Type: text/html”;
} ELSE {
$mime .= “rnContent-Type: text/plain”;
}
$mime .= ‘ boundary=”‘. $this->boundry .'”‘;

$headers = “From: “. $this->from .”rnReply-To: “. $this->from .”rn”. $mime;

$output = ‘–‘. $this->boundry .”rn”;
$output .= “MIME-Version: 1.0rn”;
$output .= $mime .”rn”;

$output .= ‘–‘. $this->boundry .”rn”;
//content type
$output .= $this->message .”rn”;

IF ( $this->attachments() ) {
$t = count( $this->filedata );

FOR ( $index = 0; $index < $t; $index++ ) {
$output .= ‘–‘. $this->boundry .”rn”;
$output .= ‘Content-Type: ‘. $this->filetype[$index] .’; name=’. $this->filename[$index];
$output .= “Content-Transfer-Encoding: base64rn”;
$output .= “Content-Disposition: attachmentrn”;
$output .= $this->filedata[ $index ];
$output .= “rn”;
}
}
$output .= ‘–‘. $this->boundry .’–‘;

echo @mail( $this->to, $this->subject, $output, $headers );

}
}

$test = NEW EMAIL( ‘[email protected]’, ‘SUBJECT’, ‘MESSAGE’, ‘[email protected]’ );
$test->attach( ‘test.pdf’ );
$test->attach( ‘test2.pdf’ );
$test->send();

?>
[/code]

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @ilbonparaurti 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.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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