I am trying to send mail with attachment in PHP but it displays the following code....
"
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="==Multipart_Boundary_x30cf82fc9724f0bee0e311417f5d383ex"
Bcc: forms@delian.net
This is a multi-part message in MIME format.
--==Multipart_Boundary_x30cf82fc9724f0bee0e311417f5d383ex
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
-
-
-
-
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAA==
--==Multipart_Boundary_x30cf82fc9724f0bee0e311417f5d383ex--
"
Kindly,help me out....
mail with attachement
Moderator: General Moderators
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
-
vish_krish
- Forum Newbie
- Posts: 6
- Joined: Wed Mar 05, 2008 6:52 am
Re: mail with attachement
It will be better to if u post ur code
you can try this code this code to send a mail with attachment
<form name='MyExampleForm' action='example.php' method='post' enctype='multipart/form-data'>
<input type='file' name='uploaded_file'>
<input type='submit' value='Submit file!'>
</form>
example.php
<?php
// include the class
include("mailer.php");
// your email
$recipient = "myemail@mydomain.com";
// person sending it
$from = "me@mydomain.com";
// subject
$subject = "A new file has been submitted!";
// email message
$message = "A new file has been submitted!";
// where to move file to once uploaded
$uploadedFolder = "/home/username/public_html/uploaded/";
// initialize email object (to, from, subject)
$myEmail = new EPDev_Emailer($recipient, $from, $subject);
// Add the message to the email
$myEmail->addText($message);
if (!empty($_FILES['uploaded_file']['name']))
{
// move the file from tmp to a folder
move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $uploadedFolder . basename($_FILES['uploaded_file']['name']));
// add as file attachment to email message
$result = $myEmail->addFile($uploadedFolder . basename($_FILES['uploaded_file']['name']), $_FILES['uploaded_file']['type']);
if (!$result)
die("Problem adding file to email!");
}
else
{
die("nothing uploaded!");
}
// actually send out the email
$myEmail->send();
mailer.php :
<?php
class EPDev_Emailer
{
var $message;
var $FILES;
var $EMAIL;
function EPDev_Emailer($to_address, $from_address, $subject, $reply_address=null, $cc_address, $bcc_address, $mailer=null, $custom_header=null)
{
$this->EMAIL = array(
"to" => $to_address,
"from" => $from_address,
"subject" => $subject,
"reply" => (empty($reply_address) ? $from_address : $reply_address),
"cc" => (empty($cc_address) ? "" : $cc_address),
"bcc" => (empty($bcc_address) ? "" : $bcc_address),
"mailer" => (empty($mailer) ? "X-Mailer: PHP/" . phpversion() : $mailer),
"header" => (empty($custom_header) ? "" : $custom_header),
"boundary" => "_mimeboundary_".md5(uniqid(mt_rand(), 1))
);
$this->message = "";
$this->FILES = array();
}
function addFile($filename, $type=null, $filecontents=null)
{
if ($filecontents !== null)
{
$index = count($this->FILES);
$this->FILES[$index]['data'] = chunk_split(base64_encode($filecontents));
$this->FILES[$index]['name'] = basename($filename);
if (empty($type))
$this->FILES[$index]['mime'] = mime_content_type($filename);
else
$this->FILES[$index]['mime'] = $type;
}
else if (file_exists($filename))
{
$index = count($this->FILES);
$this->FILES[$index]['data'] = chunk_split(base64_encode(file_get_contents($filen ame)));
$this->FILES[$index]['name'] = basename($filename);
if (empty($type))
$this->FILES[$index]['mime'] = mime_content_type($filename);
else
$this->FILES[$index]['mime'] = $type;
}
else
{
return false;
}
return true;
}
function addText($text)
{
$this->message .= $text;
}
function getHeader()
{
$header = "From: {$this->EMAIL['from']}\r\n"
. "Reply-To: {$this->EMAIL['reply']}\r\n"
. (!empty($this->EMAIL['cc']) ? "Cc: {$this->EMAIL['cc']}\r\n" : "")
. (!empty($this->EMAIL['bcc']) ? "Bcc: {$this->EMAIL['bcc']}\r\n" : "")
. "X-Mailer: {$this->EMAIL['mailer']}\r\n"
. "MIME-Version: 1.0\r\n"
. "Content-Type: multipart/mixed; boundary=\"{$this->EMAIL['boundary']}\";\r\n";
return $header;
}
function getEmail()
{
$content .= "--{$this->EMAIL['boundary']}\r\n"
. "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"
. "Content-Transfer-Encoding: 7bit\r\n\r\n"
. $this->message . "\r\n";
if (!empty($this->FILES))
{
foreach($this->FILES as $file)
{
$content .= "--{$this->EMAIL['boundary']}\r\n"
. "Content-Type: {$file['mime']}; name=\"{$file['name']}\"\r\n"
. "Content-Transfer-Encoding: base64\r\n"
. "Content-Disposition: attachment\r\n\r\n"
. $file['data'] . "\r\n";
}
}
$content .= "--{$this->EMAIL['boundary']}--\r\n";
return $content;
}
function send()
{
return mail($this->EMAIL['to'], $this->EMAIL['subject'], $this->getEmail(), $this->getHeader());
}
}
you can try this code this code to send a mail with attachment
<form name='MyExampleForm' action='example.php' method='post' enctype='multipart/form-data'>
<input type='file' name='uploaded_file'>
<input type='submit' value='Submit file!'>
</form>
example.php
<?php
// include the class
include("mailer.php");
// your email
$recipient = "myemail@mydomain.com";
// person sending it
$from = "me@mydomain.com";
// subject
$subject = "A new file has been submitted!";
// email message
$message = "A new file has been submitted!";
// where to move file to once uploaded
$uploadedFolder = "/home/username/public_html/uploaded/";
// initialize email object (to, from, subject)
$myEmail = new EPDev_Emailer($recipient, $from, $subject);
// Add the message to the email
$myEmail->addText($message);
if (!empty($_FILES['uploaded_file']['name']))
{
// move the file from tmp to a folder
move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $uploadedFolder . basename($_FILES['uploaded_file']['name']));
// add as file attachment to email message
$result = $myEmail->addFile($uploadedFolder . basename($_FILES['uploaded_file']['name']), $_FILES['uploaded_file']['type']);
if (!$result)
die("Problem adding file to email!");
}
else
{
die("nothing uploaded!");
}
// actually send out the email
$myEmail->send();
mailer.php :
<?php
class EPDev_Emailer
{
var $message;
var $FILES;
var $EMAIL;
function EPDev_Emailer($to_address, $from_address, $subject, $reply_address=null, $cc_address, $bcc_address, $mailer=null, $custom_header=null)
{
$this->EMAIL = array(
"to" => $to_address,
"from" => $from_address,
"subject" => $subject,
"reply" => (empty($reply_address) ? $from_address : $reply_address),
"cc" => (empty($cc_address) ? "" : $cc_address),
"bcc" => (empty($bcc_address) ? "" : $bcc_address),
"mailer" => (empty($mailer) ? "X-Mailer: PHP/" . phpversion() : $mailer),
"header" => (empty($custom_header) ? "" : $custom_header),
"boundary" => "_mimeboundary_".md5(uniqid(mt_rand(), 1))
);
$this->message = "";
$this->FILES = array();
}
function addFile($filename, $type=null, $filecontents=null)
{
if ($filecontents !== null)
{
$index = count($this->FILES);
$this->FILES[$index]['data'] = chunk_split(base64_encode($filecontents));
$this->FILES[$index]['name'] = basename($filename);
if (empty($type))
$this->FILES[$index]['mime'] = mime_content_type($filename);
else
$this->FILES[$index]['mime'] = $type;
}
else if (file_exists($filename))
{
$index = count($this->FILES);
$this->FILES[$index]['data'] = chunk_split(base64_encode(file_get_contents($filen ame)));
$this->FILES[$index]['name'] = basename($filename);
if (empty($type))
$this->FILES[$index]['mime'] = mime_content_type($filename);
else
$this->FILES[$index]['mime'] = $type;
}
else
{
return false;
}
return true;
}
function addText($text)
{
$this->message .= $text;
}
function getHeader()
{
$header = "From: {$this->EMAIL['from']}\r\n"
. "Reply-To: {$this->EMAIL['reply']}\r\n"
. (!empty($this->EMAIL['cc']) ? "Cc: {$this->EMAIL['cc']}\r\n" : "")
. (!empty($this->EMAIL['bcc']) ? "Bcc: {$this->EMAIL['bcc']}\r\n" : "")
. "X-Mailer: {$this->EMAIL['mailer']}\r\n"
. "MIME-Version: 1.0\r\n"
. "Content-Type: multipart/mixed; boundary=\"{$this->EMAIL['boundary']}\";\r\n";
return $header;
}
function getEmail()
{
$content .= "--{$this->EMAIL['boundary']}\r\n"
. "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"
. "Content-Transfer-Encoding: 7bit\r\n\r\n"
. $this->message . "\r\n";
if (!empty($this->FILES))
{
foreach($this->FILES as $file)
{
$content .= "--{$this->EMAIL['boundary']}\r\n"
. "Content-Type: {$file['mime']}; name=\"{$file['name']}\"\r\n"
. "Content-Transfer-Encoding: base64\r\n"
. "Content-Disposition: attachment\r\n\r\n"
. $file['data'] . "\r\n";
}
}
$content .= "--{$this->EMAIL['boundary']}--\r\n";
return $content;
}
function send()
{
return mail($this->EMAIL['to'], $this->EMAIL['subject'], $this->getEmail(), $this->getHeader());
}
}
Re: mail with attachement
--------------------------this is my code------------------------------------
if(is_uploaded_file($file))
{
//echo 'entered';
// Read the file to be attached ('rb' = read binary)
$file_received = fopen($file,'rb');
$data = fread($file_received,filesize($file));
fclose($file_received);
// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"\nBcc: forms@delian.net";
// Add a multipart boundary above the plain message
$msg = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$msg . "\n\n";
// Base64 encode the file data
$data = chunk_split(base64_encode($data));
// Add file attachment to the message
$msg .= "--{$mime_boundary}\n" .
"Content-Type: {$file_type};\n" .
" name=\"{$file_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}else{
$headers .= "Bcc: forms@delian.net\r\nContent-Type: text/html; charset=\"iso-8859-1\"\n";
}
// send email
$checksending=mail($sendto,$subject,$msg,$headers);
//echo $msg;
header('Location: sent.htm');
}
if(is_uploaded_file($file))
{
//echo 'entered';
// Read the file to be attached ('rb' = read binary)
$file_received = fopen($file,'rb');
$data = fread($file_received,filesize($file));
fclose($file_received);
// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"\nBcc: forms@delian.net";
// Add a multipart boundary above the plain message
$msg = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$msg . "\n\n";
// Base64 encode the file data
$data = chunk_split(base64_encode($data));
// Add file attachment to the message
$msg .= "--{$mime_boundary}\n" .
"Content-Type: {$file_type};\n" .
" name=\"{$file_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}else{
$headers .= "Bcc: forms@delian.net\r\nContent-Type: text/html; charset=\"iso-8859-1\"\n";
}
// send email
$checksending=mail($sendto,$subject,$msg,$headers);
//echo $msg;
header('Location: sent.htm');
}