I want send a pdf generated with TCPDF using mail Php function.
If I send a simple mail like this works: mail('xxx#ne.es', 'My Subject', $message);
The pdf is generated well, but when I want attach pdf in header not works.
This is my code:
$pdfdoc=$pdf->Output('file.pdf', 'S');
$separator = md5(time());
$eol = PHP_EOL;
// Send
$filename = "_Desiredfilename.pdf";
// encode data (puts attachment in proper format)
$attachment = chunk_split(base64_encode($pdfdoc));
///////////HEADERS INFORMATION////////////
// main header (multipart mandatory) message
$headers = "From: Sender_Name<sender#domain.com>".$eol;
$headers .= "Bcc: email#domain.com".$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;
// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
//Email message
mail('xxx#gmail.com', 'prova', 'hola', $headers);
When is the problem?
Thanks
Regards
I hope this will work for you too. I have checked and its works well. please try it.Before that please check the "php.ini" for mail and Check the pdf file was created or not.
$pdfdoc=$pdf->Output('file.pdf', 'F');
// Send
$files = "file.pdf";
///////////HEADERS INFORMATION////////////
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
// preparing attachments
$message .= "--{$mime_boundary}\n";
$fp = #fopen($files,"rb");
$data = #fread($fp,filesize($files));
#fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($files)."\"\n" .
"Content-Description: ".basename($files)."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($files)."\"; size=".filesize($files).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}--";
//Email message
mail('xxx#gmail.com', 'prova', 'hola', $headers);
or include this function in your code, use this function for sent multi-attachment file.
this one worked well . please try this.
/*
* Function Name: sentMail
* Scope : Sent Mail Function with CC , BCC and Attachment files..
* Input : Recipient Name => $recipientName // (Required)
* Recipient MailId(Many) => $recipientMailId // (Required & Array Format)
* Subject Content => $subjectContent // (Required)
* Message Content => $messageContent // (Required)
* Sender MailId => $senderMailId // (Required)
* Sender Name(Many) => $senderName // (Required)
* Recipient CC MailId => $recipientCCMailId //(Optional & Array Format)
* Recipient BCC MailId => $recipientBCCMailId //(Optional & Array Format)
* Attachment Files => $attachmentFiles //(Optional & Array Format)
* Output : Sent mail to the Recipient.
*/
public function sentMail($recipientName,$recipientMailId,$subjectContent,$messageContent,$senderName,$senderMailId,$recipientCCMailId,$recipientBCCMailId,$attachmentFiles)
{
try
{
/*Get the Mulitple Recipient CC MailId*/
if(isset($recipientCCMailId)){
if(count($recipientCCMailId)>1){
$recipientCCMailId = implode(',',$recipientCCMailId);
}
else{
$recipientCCMailId = $recipientCCMailId[0];
}
}
/*Get the Mulitple Recipient BCC MailId*/
if(isset($recipientBCCMailId)){
if(count($recipientBCCMailId)>1){
$recipientBCCMailId = implode(',',$recipientBCCMailId);
}
else{
$recipientBCCMailId = $recipientBCCMailId[0];
}
}
/*** Mail Contents Starts ***/
$subj = $subjectContent;
$msg ="";
/*Get the Mulitple Recipient BCC MailId*/
if(count($recipientMailId)>1){
$recipientMailId = implode(',',$recipientMailId);
$msg .="Dear, <b>All</b>\r <br>";
}
else{
$recipientMailId = $recipientMailId[0];
$msg .="Dear, <b>".ucwords($user_name)."</b> \r <br>";
}
$msg .=$messageContent."\r <br><br>";
$msg .="Thank you"."\r\n\n <br>";
$msg .=$senderName."\r\n\n <br><br>";
$headers ="";
/** Get the Mulitple Attachment files and Attachment Process Starts **/
if(isset($attachmentFiles)){
$headers .= "From: ".$senderMailId."\r\n";
$headers .= "Cc: ".$recipientCCMailId."\r\n";
$headers .= "Bcc: ".$recipientBCCMailId."\r\n";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$msg .= "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $msg . "\n\n";
// preparing attachments
for($i=0;$i<count($attachmentFiles);$i++)
{
if(is_file($attachmentFiles[$i]))
{
$msg .= "--{$mime_boundary}\n";
$fp = #fopen($attachmentFiles[$i],"rb");
$data = #fread($fp,filesize($attachmentFiles[$i]));
#fclose($fp);
$data = chunk_split(base64_encode($data));
$msg .= "Content-Type: application/octet-stream; name=\"".basename($attachmentFiles[$i])."\"\n" .
"Content-Description: ".basename($attachmentFiles[$i])."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($attachmentFiles[$i])."\"; size=".filesize($attachmentFiles[$i]).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
$msg .= "--{$mime_boundary}--";
}
/** Attachment Process Ends **/
else{
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=utf-8" . "\r\n";
//$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: ".$senderMailId."\r\n";
$headers .= "Cc: ".$recipientCCMailId."\r\n";
$headers .= "Bcc: ".$recipientBCCMailId."\r\n";
}
/*** Mail Contents Ends ***/
$sent_mail=mail($recipientMailId,$subj,$msg,$headers);
if($sent_mail)
{
return true;
}
else
{
return false;
}
}
catch (Exception $e)
{
getConnection('close');
echo "Caught Exception:",$e->getMessage();
}
}
and you edited this one by this parameter as specified format in the DTD of the function.
sentMail($recipientName,$recipientMailId,$subjectContent,$messageContent,$senderName,$senderMailId,$recipientCCMailId,$recipientBCCMailId,$attachmentFiles);
here,
$recipientName = "sam";
$recipientMailId = array("xxx#gmail.com");
$subjectContent = "this is sample pdf attachment over email";
$messageContent = "this is sample pdf attachment over email";
$senderMailId = "sender#gmail.com"
$senderName = "sender";
$recipientCCMailId = array("xxx#gmail.com");
$recipientBCCMailId = array("xxx#gmail.com");
$attachmentFiles = array("filename.pdf");
sentMail($recipientName,$recipientMailId,$subjectContent,$messageContent,$senderName,$senderMailId,$recipientCCMailId,$recipientBCCMailId,$attachmentFiles);
Related
I want to send mail through php mail function. For that I googled it and found the code which send mail attached with pdf file. Result is fine, mail send but mail only send attached pdf file it can not send message body.
Here is Code:
<?php
$name = "myname";
$to = "receive#gmail.com";
$email = "sender#gmail.com";
$from = "myname";
$subject = "Here is your attachment";
$mainMessage = "Hi, here's the file.";
$fileatt = $_SERVER['DOCUMENT_ROOT']."/xxx/ticket.pdf";
$fileatttype = "application/pdf";
$fileattname = "ticket.pdf";
$headers = "From: $from";
// File
$file = fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
fclose($file);
// This attaches the file
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message = "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" .
$mainMessage . "\n\n";
$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatttype};\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileattname}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"-{$mime_boundary}-\n";
// Send the email
if(mail($to, $subject, $message, $headers)) {
echo "The email was sent.";
}
else {
echo "There was an error sending the mail.";
}
?>
I can not identify where i done mistake, please help me and give some suggestion.
Note: Don't Give suggestion to use PHPMailer.
Thanks
You can try this code:
$to = "youremail#gmail.com";
$from = "Myname <sender#gmail.com>";
$subject = "Test Attachment Email";
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "document.pdf";
//$pdfdoc is PDF generated by FPDF
$pdfdoc = "/opt/transmail/2018-03-07_32_11564_invoice.pdf";
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$message = "Thanks";
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
// send message
if (mail($to, $subject, $body, $headers)) {
echo "mail send ... OK";
} else {
echo "mail send ... ERROR";
}
hope this will work for you.
I have seen similar questions, but nothing directly on topic...
I have a multi part mail script with attachments. The attachments get sent fine, but the main body text, which is populated from a form, isn't sent. I tried sending with the attachment function commented out and the form elements went through. My code is:
if (empty($_POST['RadioGroup1'])){
echo "PLease select a version of message";
} else {$selected_msg = $_POST['RadioGroup1'];}
if (isset($_FILES) && (bool) $_FILES){
$files = array();
// Check for attachments
$questions = $_POST['questions'];
//loop through all the files
foreach($_FILES as $name=>$file){
// define the variables
$file_name = $file['name'];
$temp_name = $file['tmp_name'];
//check if file type allowed
$path_parts = pathinfo($file_name);
//move this file to server
$server_file = "reports/$path_parts[basename]";
move_uploaded_file($temp_name, $server_file);
//add file to array of file
array_push($files,$server_file);
}
// define mail var
$to = $email;
$from = "[server]";
$subject = "Closed Case: $case_id $casename";
$headers = "From: $from";
//define boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Header know about boundary
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n";
$headers .= " boundary=\"{$mime_boundary}\"";
// Define plain text mail
$message .= "--{$mime_boundary}\n";
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n" . $message . "\r\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
foreach($files as $file){
$aFile = fopen($file, "rb");
$data = fread($aFile,filesize($file));
fclose($aFile);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"}\n";
$message .= " name=\"$file\"\n";
$message .= "Content-Disposition: attachment;\n";
$message .= " filename=\"$file\"\n";
$message .= "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
} // END foreach attachment
$message .= $questions;
$message .= $selected_msg;
$ok = #mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p class=\"success\">mail sent to $to!</p>";
The script runs without errors and does sent the file attachments OR the body text. Any insight would be appreciated.
Instead of calling the form variables from within --- $message .= $questions;. $message .= $selected_msg; Hard code the variables into the $message.=
Like-- $message .= "Content-Transfer-Encoding: 7bit\r\n" . $questions . "\r\n" . $selected_msg . "\r\n";
If you like add both text and attachment in email, then used bellow code
$bodyMeaasge = 'Your Body Meaasge';
$filename = yourfile.pdf;
$path = '../';
$mpdf->Output($path.$filename,'F');
$file = $path . "/" . $filename;
$to = "senderemail#gmail.com";
$subject = "My Subject";
$random_hash = md5(date('r', time()));
$headers = "From:your#domain.com\r\n" .
"X-Mailer: PHP" . phpversion() . "\r\n" .
"MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $random_hash\r\n\r\n";
if(file_exists($file))
{
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
//plain text
$body = "--$random_hash\r\n";
$body .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($bodyMeaasge));
//attachment
$body .= "--$random_hash\r\n";
$body .="Content-Type: application/octet-stream; name=".$filename."\r\n";
$body .="Content-Disposition: attachment; filename=".$filename."\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $content;
}else{
//plain text
$body = "--$random_hash\r\n";
$body .= "Content-Type: text/html; charset=utf-8\r\n"; // use different content types here
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($bodyMeaasge));
}
if (mail($to,$subject,$body,$headers)) {
header("Location:".$url.'&success=successfully mail send.');
} else {
header("Location:".$url.'&error=There was a problem sending the email.');
}
I m new to php. I need to send the email with the pdf attachment.I m able to send an email with the attachment. But unable to open the pdf. I get the some error like this
"Acrobat could not oen 'file_name' because it is either not a supported file type or because the file has been damaged(for example, it was sent as an email attachment and wasn't correctly decoded)..."
If someone could help me resolve this problem, that would be great. Thanks!
Here is my code:
$to = 'form#kronova.in, ' . $Email;
$subject = 'ABC :: Admission Form Details';
$repEmail = 'form#kronova.in';
$fileName = 'ABC-Admission.pdf';
$fileatt = $pdf->Output($fileName, 'E');
$attachment = chunk_split($fileatt);
$eol = PHP_EOL;
$separator = md5(time());
$headers = 'From: Principal abc <'.$repEmail.'>'.$eol;
$headers .= 'MIME-Version: 1.0' .$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
$message = "--".$separator.$eol;
$message .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$message .= "Thanks for filling online application form. Your online admission registration number is E0000". mysql_insert_id() . "." .$eol;
$message .= "--".$separator.$eol;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$message .= "--".$separator.$eol;
$message .= "Content-Type: application/pdf; name=\"".$fileName."\"".$eol;
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment".$eol.$eol;
$message .= $attachment.$eol;
$message .= "--".$separator."--";
if (mail($to, $subject, $message, $headers)){
echo "Email sent";
}
else {
echo "Email failed";
}
Try this:
$attachment = chunk_split(base64_encode($fileatt));
Instead of
$attachment = chunk_split($fileatt);
I am trying to implement mail functionality in php, its working fine with single attachment, but problem is when I am trying to send more than one attachment, its not working. I am using php mail() function for sending emails, I am trying to attach a PDF and an Image file. If PDF attach then Image won't attach, if Image attach then PDF won't attach. Any thoughts where I'm doing wrong?
$header .= 'From: test <noreply#test.ae>' . "\r\n";
$header .= "MIME-Version: 1.0\r\n";
$file = '1.png'
$displayname = '1.png';
$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);
$filepdf = '1.pdf'
$displaynamepdf= '1.pdf';
$file_sizepdf = filesize($filepdf);
$handlepdf = fopen($filepdf, "r");
$contentpdf = fread($handlepdf, $file_sizepdf);
fclose($handlepdf);
$contentpdf = chunk_split(base64_encode($contentpdf));
$name = basename($file);
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$displayname."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$displayname."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$displaynamepdf."\"\r\n"; // use different contentpdf types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$displaynamepdf."\"\r\n\r\n";
$header .= $contentpdf."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($to, $subject, "", $header)) {
return 'sent';
} else {
return 'not sent';
}
PHPMailer is the best to use for email.
checkout some below links which will help you in future also:
File Attachments in PHP Mail with PHPMailer
php mailer attachments
attachments using phpMailer
PHPMailer Tutorial
A Simple Example : Sending Email with Attachment Using
Phpmailer
PHP send email with multiple attachments with PHPMailer with SMTP
Authentication
Sending email with multiple attachments with PHP
may this help you.
You have $file and $filepdf both set to '1.png'. Set $filepdf="1.pdf" so that the rest of the header is correct.
Here is the code for multiple attachments in mail function..
I have created the function for multiple attachment . and check this.
/*
* Function Name: sentMail
* Scope : Sent Mail Function with CC , BCC and Attachment files..
* Input : Recipient Name => $recipientName // (Required)
* Recipient MailId(Many) => $recipientMailId // (Required & Array Format)
* Subject Content => $subjectContent // (Required)
* Message Content => $messageContent // (Required)
* Sender MailId => $senderMailId // (Required)
* Sender Name(Many) => $senderName // (Required)
* Recipient CC MailId => $recipientCCMailId //(Optional & Array Format)
* Recipient BCC MailId => $recipientBCCMailId //(Optional & Array Format)
* Attachment Files => $attachmentFiles //(Optional & Array Format)
* Output : Sent mail to the Recipient.
*/
public function sentMail($recipientName,$recipientMailId,$subjectContent,$messageContent,$senderName,$senderMailId,$recipientCCMailId,$recipientBCCMailId,$attachmentFiles)
{
try
{
/*Get the Mulitple Recipient CC MailId*/
if(isset($recipientCCMailId)){
if(count($recipientCCMailId)>1){
$recipientCCMailId = implode(',',$recipientCCMailId);
}
else{
$recipientCCMailId = $recipientCCMailId[0];
}
}
/*Get the Mulitple Recipient BCC MailId*/
if(isset($recipientBCCMailId)){
if(count($recipientBCCMailId)>1){
$recipientBCCMailId = implode(',',$recipientBCCMailId);
}
else{
$recipientBCCMailId = $recipientBCCMailId[0];
}
}
/*** Mail Contents Starts ***/
$subj = $subjectContent;
$msg ="";
/*Get the Mulitple Recipient BCC MailId*/
if(count($recipientMailId)>1){
$recipientMailId = implode(',',$recipientMailId);
$msg .="Dear, <b>All</b>\r <br>";
}
else{
$recipientMailId = $recipientMailId[0];
$msg .="Dear, <b>".ucwords($user_name)."</b> \r <br>";
}
$msg .=$messageContent."\r <br><br>";
$msg .="Thank you"."\r\n\n <br>";
$msg .=$senderName."\r\n\n <br><br>";
$headers ="";
/** Get the Mulitple Attachment files and Attachment Process Starts **/
if(isset($attachmentFiles)){
$headers .= "From: ".$senderMailId."\r\n";
$headers .= "Cc: ".$recipientCCMailId."\r\n";
$headers .= "Bcc: ".$recipientBCCMailId."\r\n";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$msg .= "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $msg . "\n\n";
// preparing attachments
for($i=0;$i<count($attachmentFiles);$i++)
{
if(is_file($attachmentFiles[$i]))
{
$msg .= "--{$mime_boundary}\n";
$fp = #fopen($attachmentFiles[$i],"rb");
$data = #fread($fp,filesize($attachmentFiles[$i]));
#fclose($fp);
$data = chunk_split(base64_encode($data));
$msg .= "Content-Type: application/octet-stream; name=\"".basename($attachmentFiles[$i])."\"\n" .
"Content-Description: ".basename($attachmentFiles[$i])."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($attachmentFiles[$i])."\"; size=".filesize($attachmentFiles[$i]).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
$msg .= "--{$mime_boundary}--";
}
/** Attachment Process Ends **/
else{
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=utf-8" . "\r\n";
//$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: ".$senderMailId."\r\n";
$headers .= "Cc: ".$recipientCCMailId."\r\n";
$headers .= "Bcc: ".$recipientBCCMailId."\r\n";
}
/*** Mail Contents Ends ***/
$sent_mail=mail($recipientMailId,$subj,$msg,$headers);
if($sent_mail)
{
return true;
}
else
{
return false;
}
}
catch (Exception $e)
{
getConnection('close');
echo "Caught Exception:",$e->getMessage();
}
}
i was facing the same issue. The problem is with mime boundary. Don't add "--".$uid."--"; after every attachment. Adding boundary before content type of attachment is enough.
I am using a class i found to email something with attachment, yet when i send the message it dosen't appear. i'm trying to send an HTML message but even a regular one dosen't send.
Can't spot the error.
The email sends fine only the message is empty
Can someone take a look please?
Code:
<?php
class mailer{
var $email_to;
var $email_subject;
var $headers;
var $mime_boundary;
var $email_message;
//sets up variables and mail email
function mailer($email_to,$email_subject,$email_message,$headers){
$this->email_to=$email_to;
$this->email_subject=$email_subject;
$this->headers = $headers;
$semi_rand = md5(time());
$this->mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$this->headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$this->mime_boundary}\"";
$this->email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$this->mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n".
$email_message . "\n\n";
}
//adds attachment
function attach($fileatt_type,$fileatt_name,$fileatt_content){
$data = chunk_split(base64_encode($fileatt_content));
$this->email_message .= "--{$this->mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$this->mime_boundary}\n";
unset($data);
unset($file);
unset($fileatt);
unset($fileatt_type);
unset($fileatt_name);
}
//send email
function send(){
return mail($this->email_to, $this->email_subject, $this->email_message, $this->headers);
}
//extra functions to make life easier
//send email with imap
function imap_send(){
return imap_mail($this->email_to, $this->email_subject, $this->email_message, $this->headers);
}
//read file and add as attachment
function file($file){
$o=fopen($file,"rb");
$content=fread($o,filesize($file));
fclose($o);
$name=basename($file);
$type="application/octet-stream";
$this->attach($type,$name,$content);
}
//read directory and add files as attachments
function dir($dir){
$o=opendir($dir);
while(($file=readdir($o)) !==false){
if($file != "." && $file != ".."){
if(is_dir($dir."/".$file)){
$this->dir($dir."/".$file);
}else{
$this->file($dir."/".$file);
}
}
}
}
}
message populate:
$message = "<span style='color:red;'>NOTICE: test tesx..";
$message .= "test text";
$message .= "Please text test.</span>";
$message .= "If you encounter any problems please contact at contact#shibby.co.il";
calling the class:
$mailer=new mailer($mail_to,$subject,$message,"From: $from_mail");
$mailer->file($fileName);
$test=$mailer->send();
For sending mail with attachment.
<?php
//If there is no error, send the email
if(isset($_POST['ur_submit_button_name'])) {
$uname=$_POST['uname'];
$to = $_POST['mailid'];
$mobileno=$_POST['mobile'];
$location=$_POST['location'];
$from = "Yourname <Yourname#domainname.com>";
$subject = "This is the subject";
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "ip.zip";//store that zip file in ur root directory
$attachment = chunk_split(base64_encode(file_get_contents('ip.zip')));
// main header
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
// send message
if (mail($to, $subject, $body, $headers)) {
$mail_sent=true;
echo "mail sent";
} else {
$mail_sent=false;
echo "Error,Mail not sent";
}
}
?>