PHP function success but mail not recieved [duplicate] - php

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.

Related

Send multiple attachments with an email PHP

I want to send multiple attachments with an email. The below is my code
$file = 'C:/Users/pdf/Testing.pdf';
$mailto = 'mail#mail.com';
$subject = 'Subject';
$message = 'My message';
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
// I can give $content to only one file and I have to give multiple pdf files here
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (RFC)
$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
$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() );
}
This code send me only one files as an attachment and I have to send the other file as well.
$file2 = 'C:/Users/pdf/sample1.pdf'; // The path for 2nd pdf file
Please try below code. You can create attachment array like
$attachement = array();
$attachement['data'][0] = 'pdfdata' // Pass PDF content with base64_encode
$attachement['data'][1] = 'tpPdfdata';
$attachement['name'][0] = 'sample1.pdf';
$attachement['name'][1] = 'sample2.pdf';
enter code here
<?php
public function send($to, $from, $subject, $message,
$cc, $attachement='') {
$mail_header = "From: $from\n";
if (isset($cc)) {
$mail_header .= "Cc:$cc\n";
}
$mail_header.= "Reply-To: noreply#demo.com\n";
$mail_header .= "MIME-Version: 1.0";
// boundary
$semi_rand = md5(time());
$boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$mail_header .= "\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$boundary}\"";
// multipart boundary
$message = "--{$boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
// preparing attachments
if (count($attachement) > 0) {
for ($i = 0; $i < count($attachement); $i++) {
$message .= "--{$boundary}\n";
$data = $attachement['data'][$i];
$message .= "Content-Type: application/octet-stream; name=\"" . $attachement['name'][$i] . "\"\n" .
//"Content-Description: ".basename($files[$i])."\n" .
"Content-Disposition: attachment;\n" . " filename=\"" . $attachement['name'][$i] . "\"; size=" . filesize($attachement['name'][$i]) . ";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
$message .= "--{$boundary}--";
return mail($to, $subject, $message, $mail_header);
}
?>
If you using normal mail function you can achieve using above code.You can pass argument like to, from, subject,attachment etc... Please try. Thank you

Unable to read PHP Mail Attachment

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();
?>

HTML/PHP mail() : Content Not Showing in Email with Attachment sent Through PHP

The issue: Emails sent with HTML and attachment do not show content. There is no issue with the attachment. Before I attempted to send it as an HTML email, the message and attachment were fine.
The code: I am using the following code to send emails with attachments:
//Email to Customer or Tech
$file = "../PDF/phptopdf/".$_GET['pdf'];
$lgth = strlen($_GET['pdf']);
$intsta = strpos($_GET['pdf'],'/')+1;
$intend = $lgth - strpos($_GET['pdf'],'.');
$intendname = $lgth - strpos($_GET['pdf'],'_');
$filename = substr($_GET['pdf'],$intsta,-$intend);
$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: ".$_SESSION['userFName']." ".$_SESSION['userLName']." <".$_SESSION['email'].">" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 8bit" . $eol;
// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/html; charset=iso-8859-1" . $eol;
$body .= "Content-Transfer-Encoding: quoted-printable" . $eol;
if ($_POST['sendToTech'] == '1') {
$mailto = $_POST['techEmail'];
$subject = 'New Work Order for '.$compName;
$pos = strpos($_POST['techName'],' ');
$techFName = substr($_POST['techName'],0,$pos);
$message = "<html><body>";
$message .= "<table style='border:1px solid #000;font-size:18px;line-height:20px;'><tr><td style='text-align:center;padding:10px;'><img src='MY LOGO URL' /></tr></td>";
$message .= "<tr><td style='padding:10px;'>";
$message .= "Hello ".$techFName.", <br/><br/>";
$message .= "I have assigned work order #00-".$_GET['idQ']." for ".$compName." to you. <br/>";
$message .= "The work is scheduled for ".date('F jS, Y \a\t g:i A',strtotime($serviceDate))." at ".$address.". <br/>";
$message .= "Click on the attachment to view the work order or <a href='MYURL".$_GET['pdf']."'>Click Here</a>. <br/><br/>";
$message .= "Thank you, <br/>";
$message .= $_SESSION['userFName']."<br/><br/></tr></td></table></body></html>";
$body .= $message . $eol;
// attachment
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/pdf; 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 "<span class='message mg'>Work Order #00-".$idQ." has been emailed to ".$_POST['techName']." at ".$_POST['techEmail']."</span><br/>";
} else {
$msg[] = "Error: " . $mysqli->error;
echo "<span class='message alert'>An error occured while attempting to send Work Order #00-".$idQ." to ".$_POST['techEmail']."</span>";
}
}
When the email arrives it looks like this:
Email in Gmail
But, when I look into the original message I see the content that is not displayed:
Original Message View
Solutions that I have attempted:
Removing "This is a multi-part message in MIME format" (PHP mail function html content not visible in email)
Changing $body .= "Content-Type: text/html; charset=iso-8859-1" . $eol; -- to text/plain (just to see if that would do anything...)
Changing $headers .= "Content-Transfer-Encoding: 8bit" . $eol; -- from 7bit and quoted-printable
Removing <html><body> and closing tags

Send email attachment received blank document

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.

php mail() : attachments sent but not body text

I have seen similar questions, but nothing directly on topic...
I have a multi part mail script with attachments. The attachments get sent fine, but the main body text, which is populated from a form, isn't sent. I tried sending with the attachment function commented out and the form elements went through. My code is:
if (empty($_POST['RadioGroup1'])){
echo "PLease select a version of message";
} else {$selected_msg = $_POST['RadioGroup1'];}
if (isset($_FILES) && (bool) $_FILES){
$files = array();
// Check for attachments
$questions = $_POST['questions'];
//loop through all the files
foreach($_FILES as $name=>$file){
// define the variables
$file_name = $file['name'];
$temp_name = $file['tmp_name'];
//check if file type allowed
$path_parts = pathinfo($file_name);
//move this file to server
$server_file = "reports/$path_parts[basename]";
move_uploaded_file($temp_name, $server_file);
//add file to array of file
array_push($files,$server_file);
}
// define mail var
$to = $email;
$from = "[server]";
$subject = "Closed Case: $case_id $casename";
$headers = "From: $from";
//define boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Header know about boundary
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n";
$headers .= " boundary=\"{$mime_boundary}\"";
// Define plain text mail
$message .= "--{$mime_boundary}\n";
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n" . $message . "\r\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
foreach($files as $file){
$aFile = fopen($file, "rb");
$data = fread($aFile,filesize($file));
fclose($aFile);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"}\n";
$message .= " name=\"$file\"\n";
$message .= "Content-Disposition: attachment;\n";
$message .= " filename=\"$file\"\n";
$message .= "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
} // END foreach attachment
$message .= $questions;
$message .= $selected_msg;
$ok = #mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p class=\"success\">mail sent to $to!</p>";
The script runs without errors and does sent the file attachments OR the body text. Any insight would be appreciated.
Instead of calling the form variables from within --- $message .= $questions;. $message .= $selected_msg; Hard code the variables into the $message.=
Like-- $message .= "Content-Transfer-Encoding: 7bit\r\n" . $questions . "\r\n" . $selected_msg . "\r\n";
If you like add both text and attachment in email, then used bellow code
$bodyMeaasge = 'Your Body Meaasge';
$filename = yourfile.pdf;
$path = '../';
$mpdf->Output($path.$filename,'F');
$file = $path . "/" . $filename;
$to = "senderemail#gmail.com";
$subject = "My Subject";
$random_hash = md5(date('r', time()));
$headers = "From:your#domain.com\r\n" .
"X-Mailer: PHP" . phpversion() . "\r\n" .
"MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $random_hash\r\n\r\n";
if(file_exists($file))
{
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
//plain text
$body = "--$random_hash\r\n";
$body .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($bodyMeaasge));
//attachment
$body .= "--$random_hash\r\n";
$body .="Content-Type: application/octet-stream; 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 .= $content;
}else{
//plain text
$body = "--$random_hash\r\n";
$body .= "Content-Type: text/html; charset=utf-8\r\n"; // use different content types here
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($bodyMeaasge));
}
if (mail($to,$subject,$body,$headers)) {
header("Location:".$url.'&success=successfully mail send.');
} else {
header("Location:".$url.'&error=There was a problem sending the email.');
}

Categories