Email Client doesn't open attachment but will download and open - php

I'm using PHP to send an attachment in an email, all works as expected except in the email client (with other email attachments) I can just click on it and it would launch the external application to view the file, or at least give me an option to select a program to try and view it. I'm not getting this as nothing happens when I click on the attachment. I can download it and view it and that works as expect.
Wanted to know if I'm missing something in the header.
Here is my function (it's in a class):
public function mail() {
if(!empty($this->attachment)) {
$filename = empty($this->attachment_filename) ? basename($this->attachment) : $this->attachment_filename;
$path = dirname($this->attachment);
$mailto = $this->to;
$from_mail = $this->from;
$from_name = $this->from_name;
$replyto = $this->reply_to;
$subject = $this->subject;
$message = $this->message;
$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);
$mime_type = $this->getMimeType($file); // function returns the MIME type
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$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: ".$mime_type."; name=\"".$filename."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
return (mail($mailto, $subject, "", $header) ? true : false);
} else {
$header = "From: ".($this->from_name)." <".($this->from).">\r\n";
$header .= "Reply-To: ".($this->reply_to)."\r\n";
return (mail($this->to, $this->subject, $this->message, $header) ? true : false);
}
}
How I'm calling it (which works and send the email w/ the attachment as expected)
$sendit = new MailAttachment(
$to,
$subject,
$message,
$excel_report,
basename($excel_report)
);
if(!$sendit->mail()) {
return 'Error';
}

Well it turns out everything is working as expected. The issue with the mail client is the file extension.
The file(s) with the extension .xls open up on double click from the email client,
The file(s) with the extension .xlsx do not open up on double click from the email client and need to be downloaded and opened.
Hope this helps someone out.

Related

Php mail send with attachment

I have written code for sending mail with attachment , i am getting mail but wil noname file with icon. I am providing path to file name and rest details to send mail.
I have written code for this migth be there is header problem i am not gettin g this problem.
Code:
$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()));
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$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=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}

I am using following code to send form data to email along with attachment but it only send attachment not data filled in form

I am using following code to send form data to email along with attachment but it only send attachment not data filled in form.
I also want to set the upload limit to 4mb
Any idea which could help me?
<?php
if(isset($_POST['submit']))
{
$name1=$_POST['name'];
$email=$_POST['email'];
$mobile=$_POST['mob'];
$applyfor= $_POST['applyfor'];
$address= $_POST['Address'];
if(is_uploaded_file($_FILES['resume']['tmp_name']))
{
$path = $_FILES['resume']['tmp_name'];
$filename = $_FILES['resume']['name'];
$file = $path;
$file_size = $_FILES['resume']['size'];
$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: $name1<$email>"."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$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=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
$message = $name1."\n" .$email."\n" .$mobile."\n" .$applyfor."\n" .$address;
$mailto = "uttamking#gmail.com";
$subject = "New resume Receive";
if (mail($mailto, $subject, $message, '', $header)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
}
}
?>

MPDF E-mail Attachment Sends Blank PDF

I have successfully generated a PDF using mpdf, which I have verified by downloading the PDF. However, when I send the PDF as an e-mail attachment I receive a blank PDF with an "Out of Memory" error by Adobe Reader. Below is my code:
<?php
include("MPDF57/mpdf.php");
ob_start();
include "Receipt_Template_2.php";
$template = ob_get_contents();
ob_end_clean();
$mpdf=new mPDF('','A4','','',32,25,27,25,16,13,'L');
mpdf->WriteHTML($template);
$content = $mpdf->Output($template, 'S');
$content = chunk_split(base64_encode($content));
$mailto = 'sample#sample.com';
$from_name = 'KIREA';
$from_mail = 'NoReply#kirea.ca';
$uid = md5(uniqid(time()));
$subject = 'KIREA Donation Receipt';
$message = "Thank you for your donation!\n\nAttached is the receipt concerning the donation. If you have any questions, please e-mail us at receipts#kirea.ca";;
$filename = $pdfName;
$header = "From: ".$from_name." <".$from_mail.">\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\n";
$header .= "This is a multi-part message in MIME format.\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\n";
$header .= "Content-Transfer-Encoding: 7bit\n";
$header .= $message."\n\r\n";
$header .= "--".$uid."\n";
$header .= "Content-Type: application/pdf; name=\"".$filename."\"\n";
$header .= "Content-Transfer-Encoding: base64\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\n\n";
$header .= $content."\n\n";
$header .= "--".$uid."--";
$is_sent = #mail($mailto, $subject, "", $header);
$mpdf->Output();
exit;
?>
Are there any ideas as to why the PDF is ending up blank after being sent as an e-mail attachment? Thank you.
mpdf->WriteHTML($template);
$content = $mpdf->Output($template, 'S');
You are wrong at here you are not taking the object on which your data is written
please replace below code with above to get correct result.
$pdfdata=mpdf->WriteHTML($template);
$content = $mpdf->Output('' , 'S');
use $content in your email
If you can use swiftmailer, you can attach a MPDF generated PDF to the email, quite easily as follows:
<?php
require_once $swift_mailer_path.'swift_required.php';
$transporter = Swift_SmtpTransport::newInstance($smtp_host, $smtp_port, $smtp_protocol)
->setUsername($smtp_username')
->setPassword($smtp_password');
$mailer = Swift_Mailer::newInstance($transporter);
$message = Swift_Message::newInstance('Email Subject')
->setFrom(array($from_email => $from_name))
->setTo($to_email)
->setBody($email_body);
$attachment = Swift_Attachment::newInstance($mpdf->Output($pdf_path, "S"), $pdf_file_name, 'application/pdf');
$message->attach($attachment);
$message->setContentType("text/html");
$result = $mailer->send($message);
?>
Here is the Reference.

mail gt marked as spam by gmail, any solutions?

i am using php code to send a mail with an attachment in my web application. all are working fine expect gmail marked it as spam. my code is look like below.
move_uploaded_file($_FILES["file"]["tmp_name"],$path . $_FILES["file"]["name"]);
$filename = $_FILES["file"]["name"];
//$path = "upload/";
$from_name = $_POST['name'];
$from_mail = $_POST['email'];
$mailto = $replyto = "nevinthomas153#gmail.com";
$subject = "Resume";
$message = $_POST['msg'];
$to = "nevinthomas153#gmail.com";
$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.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$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/html; 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=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
Please help me
the mail() function isn't exactly known to create the most perfect mails . Spam filters are very picky about mail headers, it's no easy task to build them all correctly yourself. better use a smtp library that creates the mail messages with all important headers (like phpmailer )
also make sure the server you send the mails from has a good IP reputation and correct mail setup (HELO / reverse DNS etc)

Php email - inline image and 2 attachments

I've been struggling with this script for 5 days now and I just can't get it to work. I want to send a mail using php mail function. It needs to have an inline image, and 2 attachments. What I've got does that, and it displays correcty in Thunderbird, but in Gmail client it shows the image as attachment, and not in the body of the message. Here's the code that I have:
<?php
$filename = "sharewood-lija-cjenik.xlsx";
$filename2 = "sharewood-lija-ponuda.pdf";
$inline = chunk_split(base64_encode(file_get_contents('../img/sharewoodlija.png')));
$sep = sha1(date('r', time()));
$uid = md5(uniqid(time()));
$subject = "Sharewood Lija";
$mailto = "mymail#gmail.com";
$message = '<img src="cid:image_identifier" alt="SWLBanner" /><br><br>';
$message .="<div>html message</div>";
$header = "From: asdf <asdf#asdf.hr>\r\n";
$header .= "Reply-To: asdf#asdf.hr\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/related; 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/html; charset=uft-8\r\n";
//$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
//image
$header .= "--".$uid."\r\n";
$header .= "Content-Type: image/png;\r\n";
$header .= "name=\"sharewoodlija.png\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-ID: <image_identifier>\r\n";
$header .= "Content-Disposition: inline;\r\n";
$header .= "filename=\"sharewoodlija.png\"\r\n\r\n";
$header .= $inline."\r\n";
//cjenik
$file = "../cjenik/sharewood-lija-cjenik.xlsx";
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$name = basename($file);
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; name=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
//ponuda
$file2 = "../cjenik/sharewood-lija-ponuda.pdf";
$file_size2 = filesize($file2);
$handle2 = fopen($file2, "r");
$content2 = fread($handle2, $file_size2);
fclose($handle2);
$content2 = chunk_split(base64_encode($content2));
$name2 = basename($file2);
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/pdf; name=\"".$filename2."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename2."\"\r\n\r\n";
$header .= $content2."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
?>
Any help is appreciated
The problem is not your program. Google mail will not allow images. I have seen 3 surveys of mail clients and most will not handle images like thunderbird. Some won't even indicate that there is an image or show the alt title or anything
.

Categories