Sending an email with pdf attachment in php - php

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
Store information to the database(done),
Sends the staff an email with the new customer information (done), and
Send the customer a "thank you message" email with a pdf file attachment (not working).
I mean, the customer does receive an email, but when he/she opens the pdf file, I get the following error message:
"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";
}

Related

php mail function can not send attached pdf file and message body

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.

PHP send mail with attachment destroyed

I have a simple script in PHP and FPDI : https://www.setasign.com/products/fpdi/downloads/
So I manually create a PDF and after that I'm trying to send it in email :
$headers = "From: xxxxxxxxxxxx <".$from.">".$eol;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
$message = "--".$separator.$eol;
$message .= "Content-Type: text/html; charset=ISO-8859-1".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$message .= $body.$eol;
$message .= "--".$separator.$eol;
$message .= "Content-Type: application/pdf; name=\"".$filename."\"".$eol;
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment; filename=\"".$filename."\"".$eol;
$message .= $attachment.$eol;
$message .= "--".$separator."--";
// send message
mail($to, "subject", $message, $headers);
When I try to send this email to my gmail account, it's ok, I receive email + pdf.
But if I try to send to a custom email and if I check on Thunderbird for example, my pdf is corrupted, and I get an error like : "This attachment is empty, please check the sender. Firewall or antivirus can destroy attachment"
Any ideas ? What's the problem ? My sender email ? My PHP script ? Other ?
Thanks !

Send email with the pdf attachment in php

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);

Emailing user works but not admin?

I have an order form that is processed and the customer receives the email, however, adding in the admin bit doesn't email the admin?
The following code is the bit that sends to the customer (which works fine) and also the code that should also email the admin but doesn't :(
// email stuff (change data below)
$to = $payment_data['sold_to_email'];
$from = $order_options['admin_email'];
$subject = "Your Order";
//include the message
$message = $order_options['email_message'];
//Build the Order out
$message .= '<br /><br />Thank you for purchasing from us';
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// main header (multipart mandatory)
$headers = "From: ". $company . " <" . $from . ">". $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;
// send message
$sendit = wp_mail($to, $subject, $message, $headers);
$admin_message = $payment_data['admin_field'];
wp_mail($admin_email, 'New Order', $admin_message);
//Code to troubleshoot email problems.
/* if(!$sendit) {
$tempOptions = get_option('order_options');
$tempOptions['admin_email'] = "EMAIL FAILED";
update_option('order_options', $tempOptions);
}
*/

Mail with Attachment dosen't send HTML message

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";
}
}
?>

Categories