I would like to send an email with attachment.
I have no problem to send an email by using file path, but I prefer to set my attachment by using URL.
The email can sent successfully to my email without any attachment.
My PDF URL example: http://store/index.php/Store/GI/OrderID.pdf
Hope some can help me. Really Appreciate. Thank You.
Controller::
public function SendInvoice(){
$orderid = $this->uri->segment(3);
$result = $this->order->GetCustOrder($orderid);
unset($data);
$data = array(
$OrderID,
$result[0]->cust_id,
$result[0]->cust_name,
$result[0]->cust_email
);
$this->store_email->SendInvoiceToCust($data); //library
$this->session->set_flashdata('sent_email','Email has been sent to customer.');
redirect('Store/Index');
}
Library::
public function SendInvoiceToCust($data){
$message = "Dear Valued Customer, <br><br>Thank you for Purchased at Store. <br/><br/>Kindly refer the attachment to view your Invoice.";
//$attached_file = base_url(). 'index.php/Store/GI/' .$data[0]. '.pdf';
$form = base_url(). 'index.php/Store/GI/' .$data[0]. '.pdf';
$attachment = chunk_split(base64_encode($form));
$separator = md5(time());
$message .= "--" . $separator . PHP_EOL;
$message .= "Content-Type: application/octet-stream; name=\"\" . $data[0]. '.pdf'\"" . PHP_EOL;
$message .= "Content-Transfer-Encoding: base64" . PHP_EOL;
$message .= "Content-Disposition: attachment" . PHP_EOL . PHP_EOL;
$message .= $attachment . PHP_EOL . PHP_EOL;
$message .= "--" . $separator . "--";
$this->email->from('email#gmail.com', 'Store');
$this->email->to('email2#gmail.com');
$this->email->subject('Store INVOICE [Do not reply]');
$this->email->message($message);
$this->email->attach($attachment);
$this->email->send();
}
I cant comment yet but you should try setting content type and encoding the pdf.
The below is what I am currently using to send out an email with an attachement in codeigniter.
I have updated the response. This is currently how I am taking a pdf form and attaching it.
Its all done within the $message field with different content transfer and types being set.
$sender = "email#gmail.com";
$emails = "email2#gmail.com";
$subject = "Store INVOICE [Do not reply]";
$headers = "From: " . $sender . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$message = "";
$message = "--" . $separator . PHP_EOL;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"" . PHP_EOL;
$message .= "Content-Transfer-Encoding: 8bit" . PHP_EOL . PHP_EOL;
$message .= ($content = "Dear Valued Customer, <br><br>Thank you for Purchased at Store. <br/><br/>Kindly refer the attachment to view your Invoice.");
$message .= PHP_EOL . PHP_EOL;
$form = base_url(). 'index.php/Store/GI/' .$data[0]. '.pdf';
$attachment = chunk_split(base64_encode($form));
$separator = md5(time());
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: 7bit";
$message .= "--" . $separator . PHP_EOL;
$message .= "Content-Type: application/octet-stream; name=\"" . $data[0]. '.pdf'\"" . PHP_EOL;
$message .= "Content-Transfer-Encoding: base64" . PHP_EOL;
$message .= "Content-Disposition: attachment" . PHP_EOL . PHP_EOL;
$message .= $attachment . PHP_EOL . PHP_EOL;
$message .= "--" . $separator . "--";
$result = mail($emails, $subject, $message, $headers);
Related
I want to send multiple attachments with an email. The below is my code
$file = 'C:/Users/pdf/Testing.pdf';
$mailto = 'mail#mail.com';
$subject = 'Subject';
$message = 'My message';
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
// I can give $content to only one file and I have to give multiple pdf files here
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (RFC)
$eol = PHP_EOL;
// main header (multipart mandatory)
$headers = "From: name <test#test.com>" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $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;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
print_r( error_get_last() );
}
This code send me only one files as an attachment and I have to send the other file as well.
$file2 = 'C:/Users/pdf/sample1.pdf'; // The path for 2nd pdf file
Please try below code. You can create attachment array like
$attachement = array();
$attachement['data'][0] = 'pdfdata' // Pass PDF content with base64_encode
$attachement['data'][1] = 'tpPdfdata';
$attachement['name'][0] = 'sample1.pdf';
$attachement['name'][1] = 'sample2.pdf';
enter code here
<?php
public function send($to, $from, $subject, $message,
$cc, $attachement='') {
$mail_header = "From: $from\n";
if (isset($cc)) {
$mail_header .= "Cc:$cc\n";
}
$mail_header.= "Reply-To: noreply#demo.com\n";
$mail_header .= "MIME-Version: 1.0";
// boundary
$semi_rand = md5(time());
$boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$mail_header .= "\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$boundary}\"";
// multipart boundary
$message = "--{$boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
// preparing attachments
if (count($attachement) > 0) {
for ($i = 0; $i < count($attachement); $i++) {
$message .= "--{$boundary}\n";
$data = $attachement['data'][$i];
$message .= "Content-Type: application/octet-stream; name=\"" . $attachement['name'][$i] . "\"\n" .
//"Content-Description: ".basename($files[$i])."\n" .
"Content-Disposition: attachment;\n" . " filename=\"" . $attachement['name'][$i] . "\"; size=" . filesize($attachement['name'][$i]) . ";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
$message .= "--{$boundary}--";
return mail($to, $subject, $message, $mail_header);
}
?>
If you using normal mail function you can achieve using above code.You can pass argument like to, from, subject,attachment etc... Please try. Thank you
The issue: Emails sent with HTML and attachment do not show content. There is no issue with the attachment. Before I attempted to send it as an HTML email, the message and attachment were fine.
The code: I am using the following code to send emails with attachments:
//Email to Customer or Tech
$file = "../PDF/phptopdf/".$_GET['pdf'];
$lgth = strlen($_GET['pdf']);
$intsta = strpos($_GET['pdf'],'/')+1;
$intend = $lgth - strpos($_GET['pdf'],'.');
$intendname = $lgth - strpos($_GET['pdf'],'_');
$filename = substr($_GET['pdf'],$intsta,-$intend);
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (RFC)
$eol = "\r\n";
// main header (multipart mandatory)
$headers = "From: ".$_SESSION['userFName']." ".$_SESSION['userLName']." <".$_SESSION['email'].">" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 8bit" . $eol;
// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/html; charset=iso-8859-1" . $eol;
$body .= "Content-Transfer-Encoding: quoted-printable" . $eol;
if ($_POST['sendToTech'] == '1') {
$mailto = $_POST['techEmail'];
$subject = 'New Work Order for '.$compName;
$pos = strpos($_POST['techName'],' ');
$techFName = substr($_POST['techName'],0,$pos);
$message = "<html><body>";
$message .= "<table style='border:1px solid #000;font-size:18px;line-height:20px;'><tr><td style='text-align:center;padding:10px;'><img src='MY LOGO URL' /></tr></td>";
$message .= "<tr><td style='padding:10px;'>";
$message .= "Hello ".$techFName.", <br/><br/>";
$message .= "I have assigned work order #00-".$_GET['idQ']." for ".$compName." to you. <br/>";
$message .= "The work is scheduled for ".date('F jS, Y \a\t g:i A',strtotime($serviceDate))." at ".$address.". <br/>";
$message .= "Click on the attachment to view the work order or <a href='MYURL".$_GET['pdf']."'>Click Here</a>. <br/><br/>";
$message .= "Thank you, <br/>";
$message .= $_SESSION['userFName']."<br/><br/></tr></td></table></body></html>";
$body .= $message . $eol;
// attachment
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/pdf; name=\"" . $filename . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
echo "<span class='message mg'>Work Order #00-".$idQ." has been emailed to ".$_POST['techName']." at ".$_POST['techEmail']."</span><br/>";
} else {
$msg[] = "Error: " . $mysqli->error;
echo "<span class='message alert'>An error occured while attempting to send Work Order #00-".$idQ." to ".$_POST['techEmail']."</span>";
}
}
When the email arrives it looks like this:
Email in Gmail
But, when I look into the original message I see the content that is not displayed:
Original Message View
Solutions that I have attempted:
Removing "This is a multi-part message in MIME format" (PHP mail function html content not visible in email)
Changing $body .= "Content-Type: text/html; charset=iso-8859-1" . $eol; -- to text/plain (just to see if that would do anything...)
Changing $headers .= "Content-Transfer-Encoding: 8bit" . $eol; -- from 7bit and quoted-printable
Removing <html><body> and closing tags
Text mail works fine but I want to attach file too.
Attachment is not working.
My code is:
<form method="post" enctype="multipart/form-data">
<input name="filepc" type="file" id="filepc" class="listEm" />
</form>
if (isset($_POST['submit'])) {
$attachments = '';
if (!empty($_FILES['filepc']['tmp_name'])) {
$attachments = array($_FILES['filepc']['name']);
}
headers = "From:" . $your_email . " < " . $email . ">" . "\r\n";
$headers .= "Reply - To:" . $ct_email . "\r\n";
$headers .= "Content-Disposition: attachment; filename=\"".$attachments."\"\r\n\r\n";
//$headers = "Bcc: someone#domain . com" . "\r\n";
$headers = "X - Mailer: PHP / " . phpversion();
mail($to , $subject , $msg , $headers,$attachments);
$successMsg = '<h5 style="color:red;">Sent successfully</h5>';
// }
}
Above code works but file is not attached. Please help me find the solution for this problem.
if(!empty($_FILES['resume']['name'])) {
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
$tmp_name = $_FILES['resume']['tmp_name'];
$type = $_FILES['resume']['type'];
$file_name = $_FILES['resume']['name'];
$size = $_FILES['resume']['size'];
// Check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){
// Now Open the file for a binary read
$file = fopen($tmp_name,'rb');
// Now read the file content into a variable
$data = fread($file,filesize($tmp_name));
// close the file
fclose($file);
// Now we need to encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}
$mybody = "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" .
$mybody . "\n\n";
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
$mybody .= "--{$mime_boundary}\n" .
"Content-Type: {$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";
$bodys .= "$mybody <br>";
$subject = "Attachment";
$body = $body . $bodys;
mail($to, $subject, $body, $headers);
}
I've got a problem sending email with php. The first line of my base64 encoded image disappears during mail delivery. What am I doing wrong here?
This is part of the printed message before sending:
...
--67e5a910fa8cffc2b52b2aec743f9332
Content-Disposition: attachment
Content-Type: image/svg; name="aaa.svg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="aaa.svg"
PHN2ZyB2aWV3Qm94PSIwIDAgMjk2NjkgMjA5OTAiIHhtbG5zOmRjPSJodHRwOi8vcHVybC5vcmcv
ZGMvZWxlbWVudHMvMS4xLyIgeG1sbnM6Y2M9Imh0dHA6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL25z
...
This is the message i receive:
...
--67e5a910fa8cffc2b52b2aec743f9332
Content-Disposition: attachment
Content-Type: image/svg; name="aaa.svg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="aaa.svg"
ZGMvZWxlbWVudHMvMS4xLyIgeG1sbnM6Y2M9Imh0dHA6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL25z
...
my code:
<?php
if($_POST) {
date_default_timezone_set('Europe/Berlin');
$uid = md5(uniqid(time()));
$eol = PHP_EOL;
$name = $_POST['name'];
$gliderName = $_POST['glider'];
$gliderSize = $_POST['size'];
$subject = $gliderName . "_" . $gliderSize . "_" . $name . "_" . date('d-m-Y_h:i:s', time());
$subject = str_replace(' ', '_', $subject);
$comment = $_POST['comment'];
$attachment = $_POST['image'];
$sendToSwing = $_POST['sendToSwing'];
$mail = $_POST['email'];
$mail_to = $mail;
if ($sendToSwing) {
$mail_to .= "," . "ZIELADRESSE#aaa.DE";
}
$mail_from = "ABSENDERADRESSE#aaa.DE";
$from_name = "ABSENDERNAME";
$header = "From: " . $from_name . " <" . $mail_from . ">" . $eol;
$header .= "Reply-To: " . $mail_from.$eol;
$header .= "MIME-Version: 1.0" . $eol;
$header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"" . $eol . $eol;
$msg = '<html><head><title>' . $subject . '</title></head><body>' . $eol;
$msg .= '<b>glider:</b> ' . $gliderName . $eol;
$msg .= '<b>size:</b> ' . $gliderSize . $eol;
$msg .= '<b>customer name:</b> ' . $name . $eol;
$msg .= '<b>customer email:</b> ' . $mail . $eol;
$msg .= '<b>message from customer:</b> ' . $comment . $eol;
$msg .= '</body></html>' . $eol;
$message = "--" . $uid . $eol;
$message .= "Content-type:text/html; charset=utf-8" . $eol;
$message .= "Content-Transfer-Encoding: utf-8" . $eol . $eol;
$message .= $msg . $eol;
// attachment
//echo $eol.$attachment.$eol;
$attachment = chunk_split(base64_encode($attachment));
//echo $eol.$attachment.$eol;
$message .= "--" . $uid . $eol;
$message .= "Content-Type: image/svg; name=\"" . $subject . ".svg\"" . $eol;
$message .= "Content-Transfer-Encoding: base64" .$eol;
$message .= "Content-Disposition: attachment; filename=\"" . $subject . ".svg\"" . $eol;
$message .= $attachment . $eol;
$message .= "--".$uid."--";
echo $eol.$message.$eol;
if (mail($mail_to, $subject, $message, $header)) {
echo "success ";
} else {
echo "error sending email";
}
}
?>
Like this it should work (two new line before the payload):
$message .= "Content-Disposition: attachment; filename=\"" . $subject . ".svg\"" . $eol.$eol;
I want to generate and send pdf file in mail. When I receive mail, the content inside pdf file is not correct. Here is my code:
$txt = 'hello';
$dompdf = $this->get('slik_dompdf');
$dompdf->getpdf($txt);
$dompdf->stream('karan.pdf');
$pdfoutput = $dompdf->output();
$a = chunk_split(base64_encode($pdfoutput));
// echo"<pre>";print_r($pdf) ;die;
$filename = $pdfoutput;
$email = 'abhinandank#ocodewire.com';
$date = date("Y/m/d.");
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <support#rdrp.com>' . "\r\n";
$headers .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n";
$to = $email;
$subject = "Registrar Admin Password Reset";
$txt= 'hello your information is in attachment';
mail($to,$subject,$txt,$headers);
Please Help.
Try this...!!
$content="<html>html content here</html>" ;
$html2pdf = Yii::app()->ePdf->HTML2PDF();
$html2pdf->WriteHTML($content);
$to = "dheerajchouhan85#gmail.com";
$from = "no-reply#email.com";
$subject = "Thank you for your Contribution";
$message = "<p>Your Message</p>";
$separator = md5(time());
$eol = PHP_EOL;
$filename = "example.pdf";
$pdfdoc = $html2pdf->Output('', 'S');
$attachment = chunk_split(base64_encode($pdfdoc));
$headers = "From: " . $from . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;
$body .= "Content-Transfer-Encoding: 7bit" . $eol;
$body .= "This is a MIME encoded message." . $eol;
$body .= "--" . $separator . $eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
$body .= $message . $eol;
$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 . "--";
mail($to, $subject, $body, $headers);
I suggest you to use PHPMailer for sending email because it's very simple to use and mpdf library for creating PDFs (html2pdf conversion + utf-8 support). I have created application that sends dinamically created PDFs via email and it works perfectly.
PHPMailer:
https://github.com/PHPMailer/PHPMailer
mpdf:
http://www.mpdf1.com/mpdf/index.php