My site has a contact form from which you can send an attachment, I can't send other details as body, please help solve this issue...
<?php
$message = "This is a multi-part message in MIME format.
" .
"-{$mime_boundary}
" .
"Content-Type: text/plain; charset=\"iso-8859-1
" .
"Content-Transfer-Encoding: 7bit
" .
$message .= "
";
$data = chunk_split
(base64_encode(file_get_contents($_FILES["fileAttach"] ["tmp_name"])));
$message .= "--{$mime_boundary}
" .
"Content-Type: {$fileatttype};
" .
" name=\"{$fileattname}\"
" .
"Content-Disposition: attachment;
" .
" filename=\"{$fileattname}\"
" .
"Content-Transfer-Encoding: base64
" .
$data .= $phone."
"; "
" .
"-{$mime_boundary}-
";
// Send the email
if(mail($to, $subject, $message, $headers)) {
?>
I would use the phpmailer for ease. https://github.com/Synchro/PHPMailer. You can simply use the $mail->addAttachment('images/phpmailer_mini.gif');
and email easily.
A typical code would look like
<?php
require '../PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
// Set PHPMailer to use the sendmail transport
$mail->isSendmail();
//Set who the message is to be sent from
$mail->setFrom('from@example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto@example.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer sendmail test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.gif');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
If you prefer to go old school try the below.
<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with
$headers = "From: webmaster@example.com
Reply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "
Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
Use this code in your mail,use it as a function for easy use
require 'PHPMailerAutoload.php';
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail.">
";
$header .= "Reply-To:".$my_reply." <".$replyto.">
";
$header .= "MIME-Version: 1.0
";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"
";
$header .= "Content-type:text/plain ; charset=iso-8859-1
";
$header .= "--".$uid."
";
$header .= "Content-type:text/html; charset=iso-8859-1
";
$header .= "Content-Transfer-Encoding: 7bit
";
$header .= $msg."
";
$header .= "--".$uid."
";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"
"; // use different content types here
$header .= "Content-Transfer-Encoding: base64
";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"
";
$header .= $content."
";
$header .= "--".$uid."--";
if (mail($to, $subject, "", $header)) {
// echo "mail send ... OK"; // or use booleans here
} else {
// echo "mail send ... ERROR!";
}