Php mail send with attachment - php

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

Related

email shows html tags it`s not appear in html format

I am sending email using editor but it`s also send the html tags and i want to show only html view and This is my email attachment code --
$file = 'uploads/email_attachments/'.$row['email_image'];
$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()));
$from_name = "Tino Cesar";
$from_mail = "info#tino.co.in";
$replyto = $row['email'];
$message = $row['message'];
$header = "From: ".$name." <".$email.">\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";
$headers .= "Content-type:text/html;charset=UTF-8" . "\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."--";
$mailto = $row['email'];
$subject = $row['subject'];
if (mail($mailto, $subject, "", $header)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
change this
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
to
$header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
remove this line
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";

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

PHP “Could Not Access File:”

I am using this code in career.php page.
Warning: file_get_contents(/home/eigeny6s/public_html/text.txt) [function.file-get-contents]: failed to open stream: No such file or directory in /home/eigeny6s/public_html/career.php.
Also I had set permission to 0777 of public.html in ftp.
My code is:
function mail_attachment($filename, $path, $mailto, $from_mail,
$from_name, $replyto, $subject, $message)
{
$file = $path.$filename;
print_r($file);
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = file_get_contents($strFilesName);
$content = chunk_split(base64_encode($content));
$header .= "--".$uid ."\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\n\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename ."\"\n";
$header .= "Content-Transfer-Encoding: base64\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename ."\"\n\n";
$header .= $content."\n\n";
$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/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "Name: ".$_POST["name"]."\n\n";
$header .= "E-mail: ".$_POST["email"]."\n\n";
$header .= "Position: ".$_POST["position"]."\n\n";
$header .= "Phone: ".$_POST["phone"]."\n\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!";
}
}
According to what you're describing, it's likely that the file /home/eigeny6s/public_html/text.txt doesn't exist.
Your code is trying to read this file, likely because that's what $strFilesName is.
To fix it you could:
Ensure that this text.txt file exist
or change the value of $strFilesName
Alternatively, if you can't guarantee that this file exist, you could check for it first:
$content = "default text used if I don't have my file";
if ( file_exists($strFilesName) ){
$content = file_get_contents($strFilesName);
}

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
.

PHP Mail, CC Field

How can I include a CC field in this function?
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $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);
$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\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 "OK"; // or use booleans here
} else {
echo "ERROR!";
}
}
Add this while declaring headers:
$headers .= 'Cc: somebody#example.com' . "\r\n";
Add $header .= "CC: ".$cc."\r\n"; after the Reply-To-line.
http://www.w3schools.com/PHP/func_mail_mail.asp
Please don't build your own MIME emails. Use something like PHPMailer instead. Far easier to use and less brittle. Adding a CC with that is as simple as:
$mail = new PHPMailer();
$mail->AddCC('somebody#example.com');

Categories