I am using following code for sending email with attachment but the proper file is not getting attach with mail.
$UnidID = $_COOKIE['UniqueID'];
$guid = $_COOKIE['guid'];
$target_path = "userdata/".$UniqueID."/".$iGuid."/Outputs";
$fname = getpathmail($UnidID,$guid);
$target_path = $target_path.$filname;
$fileatt_type = "application/fbf"; // File Type
$fileatt_name = $fname;
$data = $target_path;
$email_from = "EHPAdmin#fugro.in";
$email_subject = "EHP/PPP process";
$email_message = "Processed result for EHP/PPP processing";
$email_to = $_GET['Email'] ;
$headers = "From: ".$email_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}\"";
$email_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" .
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
$ok = #mail($email_to, $email_subject, $email_message, $headers);
I'd suggest to use a library for sending eMails as it will handle all the header related stuff. Have a look at Zend_Mail. Sending attachments is as easy as
$mail = new Zend_Mail();
$at = $mail->createAttachment($myImage);
$at->type = 'image/gif';
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
$at->encoding = Zend_Mime::ENCODING_8BIT;
$at->filename = 'test.gif';
$mail->send();
You need to put all of your multipart message into $email_message:
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers = "From: ".$email_from;
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_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" .
"--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
chunk_split(base64_encode($data)) .
"--{$mime_boundary}--\n";
May be you missed this in your code
...
$file = fopen($data,'rb');
//saves content in $data itself
$data = fread($file,filesize($data));
fclose($file);
...
should work havn't executed myself.
give it a try
You should give your attachment file as:
"Content-Disposition: attachment; filename='/path/to/the/file/filename'";
Related
This my code plz provide me solution
<?php
class email
{
function emailWithAttach($fromaddress,$toAddress,$mailSubject,$mailMessageHead,$mailMessageMain,$mailMessageSign,$filePath,$fileName)
{
$fileatt_name = $fileName;
$fileatt = $filePath.$fileName;
$fileatt_type = "application/octet-stream";
$email_from = $fromaddress;
$email_subject = $mailSubject;
$email_message = $mailMessageHead."<br>";
$email_message .= $mailMessageMain."<br>";
$email_message .= $mailMessageSign;
$headers = '';
$email_to = $toAddress;
$headers = "From: ".$email_from;
$file = fopen($fileatt, 'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
empty($mime_boundary); /*i also tried empty function still getting notice.*/
$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}\"";
$email_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" .
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\" {$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n". /*i m getting error on this line */
"--{$mime_boundary}--\n";
if(#mail($email_to, $email_subject, $email_message, $headers))
{
return true;
}
}
}
?>
You can try using this:
$mime_boundary = isset($mime_boundary) ? $mime_boundary : '';
in place of:
empty($mime_boundary);
but i think this is no sense...this variable ($mime_boundary) is not exist in your function, so this is empty. You can skip this check and remove this line.
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";
?>
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;
?>
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);
The PHP below will attach the file from the server, but will not allow me to edit the message input for the body of the email.
<?php
$fileatt = './dwrdocuments/dwr.fdf'; // Path to the file
$fileatt_type = "application/octet-sdiveam"; // File Type
$fileatt_name = date(mdy_his).'_dwr.fdf';
$email_from = $_POST['From']; // Who the email is from
$email_subject = 'DWR Submittal'; // The Subject of the email
$email_txt = $_POST['Comments']; // Message that the email has in it
$email_to = 'admin#example.com'; // Who the email is to
$headers = "From: ".$email_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}\"";
$email_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-divansfer-Encoding: 7bit\n\n" .
$email_message . "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
$ok = #mail($email_to, $email_subject, $email_message, $headers);
header ("Location: ../confirm.html");
?>
If I do input a message for the body of the email by replacing:
$email_message . "\n\n";
the attachment does not come through, but the message will... along with some gibberish
--==Multipart_Boundary_xd05fe3fbc7afe814087a952a9c31f1fax Content-Type: application/octet-sdiveam; name="061711_080900_dwr.fdf" Content-Transfer-Encoding: base64 JUZERi0xLjIKJe+/ve+/ve+/ve+/vQoxIDAgb2JqCjw8IAovRkRGIDw8IC9GaWVsZHMgWyA8PC9U KFdvcmtfRGF0ZSkvVihramgpPj48PC9UKE1haW50ZW5hbmNlX0FyZWEpL1YoKT4+PDwvVChSb3V0 ZV9OdW1iZXIpL1YoKT4+PDwvVChEdWVfRGF0ZSkvVigyMDExLTA2LTE2IDIyOjI2OjAxKT4+PDwv VChSb2FkX1NlZ21lbnRfSUQpL1YoKT4+PDwvVChDcm9zc19TZWN0aW9uX1Bvc2l0aW9uKS9WKCk+ Pjw8L1QoTG9jYWxfTmFtZSkvVigpPj48PC9UKEZhY2lsaXR5X05hbWUpL1YoKT4+PDwvVChGYWNp bGl0eV9BZGRyZXNzKS9WKCk+Pl0gCi9GIChodHRwOi8vY2FnZWRuYXRpb24uY29tL2pjcy9waHAv ZHdyX2Zvcm0ucGRmKSAvSUQgWyA8ZjIxODgyZTJmOGQxOGJjYjY5OGRhYzlmNTllNzIwYWM+Cl0g Pj4gCj4+IAplbmRvYmoKdHJhaWxlcgo8PAovUm9vdCAxIDAgUiAKCj4+CiUlRU9GCg==
I would like to include a body to the email for comments and notes.
Why don't you use a mailer library? There are a lot out there, like Swiftmailer, PHPMailer or Zend_Mail. You'd save yourself the headache of dealing with the email details.
Have you tried ending headers with \r\n?
http://php.net/manual/en/function.mail.php