I am new to PHP, I need to create a form such that it asks the user to enter several fields and upload his/her resume. When he/she submits the form, his/her submissions should be email to me with his/her resume as the attachment with the email. I have used PHP for sending the email using php mail function. Everything works fine, except that the file attachment is not able to read. Please see the screenshot attached.
https://imgur.com/a/UnUOyDR
Also the file uploaded is in odt format. I need users to upload all type of resume formats.
I am posting the Essential Part of the Code. Please correct me if I am wrong
if ($_POST['submit_x']) {
$cand_name = trim($_POST['cand_name']);
$appl_email = $_POST['email'];
$target_dir = "/home/test/public_html/new/job/hr/Resume/";
$file = $_FILES['my_file']['name']; // Resume-Test.odt
$path = pathinfo($file);
$ext = $path['extension']; // odt
$temp_name = $_FILES['my_file']['tmp_name']; // /tmp/phpqkLeuL
$path_filename_ext = $target_dir.$file.".".$ext;
move_uploaded_file($temp_name,$path_filename_ext);
$mailto = 'robert.k1254#gmail.com';
$subject = 'Subject';
$message = 'My message';
$content = file_get_contents($path_filename_ext);
$content = chunk_split(base64_encode($content));
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (RFC)
$eol = "\r\n";
// main header (multipart mandatory)
$headers = "From: name <test#test.com>" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $message . $eol;
// attachment
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $file . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
print_r( error_get_last() );
}
}
<input type="file" name="my_file" /><br /><br />
If you do it with PHP's mail() function,it difficult to find bugs.Download the PHPMailer script from here: http://github.com/PHPMailer/PHPMailer and include the main script file.
<?php
$email = new PHPMailer();
$email->From = 'From mail id';
$email->FromName = 'from name';
$email->Subject = 'Subject of message';
$email->Body = $bodytext;//body of the subject
$email->AddAddress( 'receiver email id' );
$file_path = 'path of the file you want to attach';
$email->AddAttachment( $file_path , 'filename' );
return $email->Send();
?>
Related
I'm new to PHP and as a web developer have only been using it to write simple contact forms. Recently I was making a contact form with a file upload feature, not knowing any PHP I found a solution online here. I like it quite a bit because it sends an email from the domain to a personal gmail, which wasn't possible with my old PHP code. I tried to change it up a bit so the $message formatting looks a bit better and so I can use it as a contact form WITHOUT file upload. This is what I got:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['tel'];
$subj = $_POST['subject'];
$usermessage = $_POST['message'];
// $message ="Name = ". $name . "\r\n Email = ". $email . "\r\n Phone = ". $phone . "\r\n Message = ". $usermessage; ------- original $message code with what looks like broken syntax
// new $message code that does not work
$message = "Name: ".$name. "\r\n";
$message .= "Email: ".$email. "\r\n";
$message .= "Phone: ".$phone. "\r\n";
$message .= "Message: ".$usermessage. "\r\n";
$subject = $subj;
$fromname ="Someone";
$fromemail = 'info#domain.com';
$mailto = 'personalemail#gmail.com';
// $content = file_get_contents($fileName);
// $content = chunk_split(base64_encode($content));
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (RFC)
$eol = "\r\n";
// main header (multipart mandatory)
$headers = "From: ".$fromname." <".$fromemail.">" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $message . $eol;
// attachment
// $body .= "--" . $separator . $eol;
// $body .= "Content-Type: application/octet-stream; name=\"" . $filenameee . "\"" . $eol;
// $body .= "Content-Transfer-Encoding: base64" . $eol;
// $body .= "Content-Disposition: attachment" . $eol;
// $body .= $content . $eol;
// $body .= "--" . $separator . "--";
//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
echo "mail send ... OK"; // do what you want after sending the email
header('Location: success.html');
} else {
echo "mail send ... ERROR!";
print_r( error_get_last() );
}
Basically I just commented out what looked like code for the file, so it wouldn't send a 'noname' attachment with no file extension. That worked, but the formatting had some weird indentations, random text color changes to a a:visited blue/purple, and the = wasn't visually appealing, I'd rather have a ':'. Plus, the syntax looked broken with quotations in the wrong place. So I made the new message code that can be clearly seen, and commented out the original broken one line $message code, and now the email sends with no content. How is this possible? How do I fix this code?
It seems the slightest thing breaks the code to where a message sends completely empty or only one field sends. Commenting a line out, replacing the '=' with ':' in the original $message code, deleting comments, all could possibly break this again.
Change to
// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $eol; // This line added!
$body .= $message . $eol;
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 1 year ago.
I made a form with PHP to send mail, When I test it locally it runs fine I receive an email with all the information but when I put it live on a host it says SUCCESS but I never receive the mail. Maybe is something wrong with the code because i have an other form without the ATTACH file and it runs perfecrly
$filenameee = $_FILES['file']['name'];
$fileName = $_FILES['file']['tmp_name'];
$name = $_POST['name'];
$email = $_POST['email'];
$title=$_POST['title'];
$prototip=$_POST['prototip'];
$description = $_POST['descripton'];
$message ="Name = ". $name . "\r\n Email = " . $email . "\r\n \r\n Naslov = ".$title . "\r\n Opis = ".$description;
$subject ="My email subject";
$mailto = 'levchegochev#gmail.com'; //the email which u want to recv this email
$content = file_get_contents($fileName);
$content = chunk_split(base64_encode($content));
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (RFC)
$eol = "\r\n";
// main header (multipart mandatory)
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $message . $eol;
// attachment
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $filenameee . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
echo "<script>
swal({
title: 'Ви благодариме за вашата апликација!',
text: 'Kе ве контактираме во рок од 24 часа',
icon: 'success',
button: 'Супер!',
});
</script>";
} else {
echo "<script>alert('Mail was not sent. Please try again later');</script>";
}
Updated answer
Set the following parameters in your html form:
<form action="your_target_php_file.php" method="post" enctype="multipart/form-data">
PHP file:
if ($_SERVER['REQUEST_METHOD'] == "POST") {
// file data from html element input[type='file']
$file = (object) [
'tmp_name' => $_FILES['file']['tmp_name'],
'name' => $_FILES['file']['name'],
'size' => $_FILES['file']['size'],
'type' => $_FILES['file']['type'],
'error' => $_FILES['file']['error'],
];
// other POST data
$post = (object) [
'name' => $_POST['name'],
'email' => $_POST['email'],
'title' => $_POST['title'],
'prototip' => $_POST['prototip'],
'description' => $_POST['descripton']
];
// email message
$message = "Name = ". $post->name . "\r\n Email = " . $post->email . "\r\n\r\n Naslov = ";
$message .= $post->title . "\r\n Opis = ".$post->description;
// email subject
$subject = "My email subject";
//the email which u want to recv this email
$mailto = 'levchegochev#gmail.com';
//read from the uploaded file
$handle = fopen($file->tmp_name, "r");
$content = fread($handle, $file->size);
fclose($handle);
$encoded = chunk_split(base64_encode($content));
// boundary
$boundary = md5("random");
// email headers
$headers[] = "MIME-Version: 1.0";
$headers[] = "From: " .explode("#", $post->email)[0]. " <{$post->email}>";
$headers[] = "Reply-To: {$mailto}";
$headers[] = "Content-Type: multipart/mixed;";
$headers[] = "boundary = {$boundary}";
//plain text
$body = "--{$boundary}\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message));
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: {$file->type}; name={$file->name}\r\n";
$body .="Content-Disposition: attachment; filename={$file->name}\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000, 99999)."\r\n\r\n";
// Attaching the encoded file with email
$body .= $encoded;
if (mail($post->email, $subject, $body, implode("\r\n", $headers)))
echo ("<script>
swal({
title: 'Ви благодариме за вашата апликација!',
text: 'Kе ве контактираме во рок од 24 часа',
icon: 'success',
button: 'Супер!',
});
</script>");
} else {
echo "<script>window.alert('Mail was not sent. Please try again later');</script>";
}
}
I changed your code a bit, I believe it will work for you.
I'm brand new to php and am using the following code to send an email with a PDF attachment.
The Code seems to almost be working. The Email is sent and the file is attached.
However when attempting to open the file it is saying that the file is corrupt.
Please can someone show me where I am going wrong? Thanks in advance.
<?php
$filename = 'My File.pdf';
$path = '/public_html';
$file = $path . "/" . $filename;
$mailto = 'themark#gmail.com';
$subject = 'Subject';
$message = 'My message';
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (RFC)
$eol = "\r\n";
// main header (multipart mandatory)
$headers = "From: Jennifer <jenniferk#gmail.co.uk>" . $eol;
$headers .= "Reply-To: Jennifer <jenniferk#gmail.co.uk>" .$eol;
$headers .= "Return-Path: Jennifer <jenniferk#gmail.co.uk>" .$eol; // these two to set reply address
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $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;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
print_r( error_get_last() );
}
?>
I m trying to send pdf attachment in gmail and webmail. In gmail it works properly but when it goes to webmail pdf file showing corrupt and 0 byte.it goes in gmail,yahoomail,and other mail is ok but in webmail it's not working file is going to webmail but when i m trying to open that pdf it showing file corrupt. Please help me out anyone.code bellow:-
$info_send_mail = $obj->select(TABLEPRIFIX.'send_mail','link',['sale_id'=>$info[0]['sale_id']]);// Fetch data
$filename = $info_send_mail[0]['link']; // fetch file name
$path = 'http://webfreakers.com/crm/admin/pdf/sendmail';
$file = $path . "/" . $filename;
$mailto = $info[0]['email'];
$subject = 'Invoice';
$message = 'Check your invoice';
// retrive file from the folder
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (RFC)
$eol = "\r\n";
// main header (multipart mandatory)
$headers = "From: Universal Survey & Analysis Services <sms#webfreakers.com>" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $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;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
echo "mail send ... OK"; // or use booleans here
$_SESSION['msg'] = '<p class="alert alert-success alert-dismissable fade in">×Mail sent successfully</p>';
header('location:../sales_list.php');
} else {
echo "mail send ... ERROR!";
print_r( error_get_last() );
$_SESSION['msg'] = '<p class="alert alert-success alert-dismissable fade in">×Mail not sent</p>';
header('location:../sales_list.php');
}
I try to add send email attachment using php,file attachment working fine but attachment file open blank document.How to solve this issue.Below mentioned my code.
$from_email = 'sender_mail#example.com'; //sender email
$recipient_email = 'manosk24#gmail.com'; //recipient email
$subject = 'Test mail'; //subject of email
$message = 'This is body of the message'; //message body
$filename = "file1.pdf";
$path = $_SERVER['DOCUMENT_ROOT'] . "/mail-function/upload/";
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "rb");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$boundary = md5(uniqid(time()));
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:" . $from_email . "\r\n";
$headers .= "Reply-To: " . $user_email . "" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message));
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: application/pdf; name=" . $filename . "\r\n";
$body .="Content-Disposition: attachment; filename=" . $filename . "\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: " . rand(1000, 99999) . "\r\n\r\n";
$body .= $encoded_content;
$sentMail = #mail($recipient_email, $subject, $body, $headers);
if ($sentMail) { //output success or failure messages
die('Thank you for your email');
} else {
die('Could not send mail! Please check your PHP mail configuration.');
}
Sorry for my spelling mistake..
try this code,
$filename = "file1.pdf";
$file = $path . "/" . $filename;
$message ="my message";
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
// 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: name <test#test.com>" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
// message
$headers .= "--" . $separator . $eol;
$headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$headers .= "Content-Transfer-Encoding: 8bit" . $eol;
$headers .= $message . $eol;
// attachment
$headers .= "--" . $separator . $eol;
$headers .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: base64" . $eol;
$headers .= "Content-Disposition: attachment" . $eol;
$headers .= $content . $eol;
$headers .= "--" . $separator . "--";
//SEND Mail
if (mail($mailto, $subject, "", $headers)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
i hope it will be helpful.