Send mail with an attachment in PHP - php

i would like to send a mail with an attachment with the php mail function. I can receive a mail. Unfortunately the image is broken. The image is in the same directory as the php script.
My source code for the script:
$name = "Name goes here";
$email = "recipient#gmail.com";
$to = "$name <$email>";
$from = "Sender";
$subject = "Here is your attachment";
$fileatt = "test.jpg";
$fileatttype = "image/jpeg";
$fileattname = "test.jpg";
$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/plain; charset=\"iso-8859-1\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message .= "\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.";
}
Followed this tutorial:
http://www.texelate.co.uk/blog/send-email-attachment-with-php/

$fileatt = "your folder name/test.jpg";
<?php
$files = array(
"yourfoldername/yourfilename.png",
"yourfoldername/yourfilenamepng"
);
//1431347258selfie_1431347013130.png
// email fields: to, from, subject, and so on
$to = "example#gmail.com";
$from = "support#mail.com";
$subject = "My subject";
$message = "My message";
$headers = "From: $from";
// 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 = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
for( $x=0; $x<count($files); $x++ )
{
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
// send mail
if( mail($to, $subject, $message, $headers) )
{
$somthing = "Mail successfully Send.";
}
else
{
$somthing = "Mail sending fail.";
}
echo $somthing;
?>

Related

Empty attachment using PHP mailer

I am trying to send a mail with a attachment using PHP. When I send the mail I am recieving it with a empty attachment (the attachment file size = 0 bytes). The filepath that I am using is correct.
Does someone know what is wrong with my script. Why am I recieving an empty attachment?
Here is my script:
$to = $destination;
$from = 'test#test.test';
$fromName = 'Test';
$subject = $subject;
$file = "./image-based-pdf-sample.pdf";
$htmlContent = $content;
$headers = "From: $fromName"." <".$from.">";
$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 = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\r\n" . $htmlContent . "\r\n";
$message .= "--{$mime_boundary}\n";
$fp = #fopen($file,"rb");
$data = #fread($fp,filesize($file));
#fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($file)."\"\n" .
"Content-Description: ".basename($file)."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($file)."\"; size=".filesize($file).";\n" .
"Content-Transfer-Encoding: base64\r\n" . $data . "\r\n";
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $from;
$mail = #mail($to, $subject, $message, $headers, $returnpath);
echo $mail?"<h1>Success</h1>":"<h1>Error</h1>";

Email in php/html is showing all the user's email , how do i add CC so the user can't see other user's email

i can send email but all the user kan se all the email i try to add cc but it is not vorking so nav i hope that you smart user kan help me with my problem
$to = $_POST['email'];
$from = "bladnyt#dhl-hund.dk";
$subject = $_POST['sub'];
$message = $_POST['msg'];
$headers = "From: $from";
// 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 = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
for ($x = 0; $x < count($files); $x++) {
$file = fopen($files[$x]['tmp_name'], "rb");
$data = fread($file, filesize($files[$x]['tmp_name']));
fclose($file);
$data = chunk_split(base64_encode($data));
$name = $files[$x]['name'];
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
// send
$ok = mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>mail sent to $to!</p>";
} else {
echo "<p>mail could not be sent!</p>";
}
}

Generate PDF and sent it through Email as Attachment (PDF is not opening)

I am generating pdf and sent it through email as attachment the code is given below for this i skipped some code for generating pdf (not needed).
Please check the code:
//pdf generated not given avobe code of how it comes
$pdfdoc = $pdf->Output("confirmation-".$client_code.".pdf", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
$to = $mail_to;
$from = "From: <info#ksilbd.com>";
$subject = "Here is your attachment";
$mainMessage = "BUY/SALE CONFIRMATION";
$fileatt = $attachment;
$fileatttype = "application/pdf";
$fileattname = "confirmation-".$client_code.".pdf";
$headers = "From: $from";
$file = fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
fclose($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/plain; charset=\"iso-8859-1\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$mainMessage . "\n\n";
//base_64
//encoding used
//to encode data
$data = chunk_split(base64_encode($data));
//message
//concat the
//message
$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 "EMAIL SENT SUCCESSFUL.";
}
else {
echo "There was an error sending the mail.";
The message showed email sent successful and i got email too but i failed to open the attached pdf file with the mail ....
please check the problem
You may use "phpmailer".
Download the PHPMailer script from here: http://github.com/PHPMailer/PHPMailer
and Enjoy it.

Attachment but no text php email?

I am making a piece of code for a website and made the following code, however i need to put html in the email however it doesn't send it, it only attaches the image, please help?
<?php
$emailSubject = 'Application - '.$_POST['full_name'].'';
$webMaster = 'email#webaddress.com';
$from = $_POST["email"];
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
$headers = "From: $fromName";
if (file($tmpName)) {
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
$data = chunk_split(base64_encode($data));
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
$html_message = "<html>"
$email_message = mail($webMaster, $emailSubject, $message, $headers);
$result = 'Result Code Here On Page';
echo "$result";
?>

mail with attachment not being sent (wierd case)?

This code to send an html file as an attachment is working properly. However in the next code snippet,when I cahnge the attachment to an image it is not being sent.Why is it so?I don't want to use phpmailer or swift mail!
<?php
$file_path = "file.html"; // server path where file is placed
$file_path_type = "text/html"; // File Type
$file_path_name = "newfilename.html"; // this file name will be used at reciever end
$from = "xyz#gmail.com"; // E-mail address of sender
$to = "abc#gmail.com"; // E-mail address of reciever
$subject = "Please check the Attachment."; // Subject of email
$message = "This is the message body.<br><br>Thank You!<br><a href='http://7tech.co.in'>7tech.co.in Team</a>";
$headers = "From: ".$from;
$file = fopen($file_path,'rb');
$data = fread($file,filesize($file_path));
fclose($file);
$rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$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" .
$message .= "\n\n";
$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$file_path_type};\n" .
" name=\"{$file_path_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$file_path_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
if(#mail($to, $subject, $message, $headers)) {
echo "File send!";
} else {
echo 'Failed';
}
?>
Now when I change the attached file to an image i.e. screenshot.png it fails to send the message.
<?php
$file_path = "screenshot.png"; // server path where file is placed
$file_path_type = "image/png"; // File Type
$file_path_name = "screenshot.png"; // this file name will be used at reciever end
$from = "xyz#gmail.com"; // E-mail address of sender
$to = "abc#gmail.com"; // E-mail address of reciever
$subject = "Please check the Attachment."; // Subject of email
$message = "This is the message body.<br><br>Thank You!<br><a href='http://7tech.co.in'>7tech.co.in Team</a>";
$headers = "From: ".$from;
$file = fopen($file_path,'rb');
$data = fread($file,filesize($file_path));
fclose($file);
$rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$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" .
$message .= "\n\n";
$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$file_path_type};\n" .
" name=\"{$file_path_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$file_path_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
if(#mail($to, $subject, $message, $headers)) {
echo "File send!";
} else {
echo 'Failed';
}
?>
Can you guys point out the error.I've tried to cahnge content type too at 1-2 places but it wasn't working.Am I missing anything?
Use
file_get_contents('http://www.example.com/');
Instead of
$file = fopen($file_path,'rb');
$data = fread($file,filesize($file_path));
fclose($file);
and try with this code..
$random_hash = md5(time());
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
$headers .= "\r\nMIME-Version 1.0";
$attachment = chunk_split(base64_encode(file_get_contents($filename)));
$message =
"--PHP-alt-$random_hash
Content-Type: text/plain
Dear Same,
We would like to thank you for your registration to be held on Saturday August 25, 2012 at the....
--PHP-alt-$random_hash
Content-Type: application/pdf; name=$filename
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-alt-$random_hash--";
#mail($to, $subject, $message, $headers);

Categories