MIME email, blank message body - php

So I tried searching but couldn't find an answer that I was able to apply to my code. I have had a developer updating a page for me and these emails come through on my mobile device, but the body is always blank on my computer email, Thunderbird. The code in question looks like this:
$mailBody = "Dog Registration Form Data" . PHP_EOL . $strDogCName . ";" . $strDogFName . ";" . $strBreed . ";" . $strGender . ";" . $strHt . ";" . $strBdmm . "/" . $strBddd . "/" . $strBdyy . ";" . $strFName . ";" . $strLName . ";" . $strAddr1 . ";" . $strCity . ";" . $strState . ";" . $strZip . ";" . $strCountry . ";" . $strPhone . ";" . $strEMail . ";" . $member_num. ";" . $dog_num;
// injection test before setting message headers
$sender_name = $strFName . " " . $strLName;
$sender_name = injection_test($sender_name);
$sender_email = injection_test($strEMail);
// Set headers and send. This should be moved to a reusable function
$mime_boundary = md5(time());
$headers = '';
$msg = '';
$headers .= 'From: ' . $sender_name . ' <' . $sender_email . '>' . PHP_EOL;
$headers .= 'Reply-To: ' . $sender_name . ' <' . $sender_email . '>' . PHP_EOL;
$headers .= 'Return-Path: ' . $sender_name . ' <' . $sender_email . '>' . PHP_EOL;
$headers .= "Message-ID: <" . time() . "cform#" . $_SERVER['SERVER_NAME'] . ">" . PHP_EOL;
$headers .= 'X-Sender-IP: ' . $_SERVER["REMOTE_ADDR"] . PHP_EOL;
$headers .= "X-Mailer: PHP v" . phpversion() . PHP_EOL;
$headers .= 'MIME-Version: 1.0' . PHP_EOL;
$headers .= 'Content-Type: multipart/related; boundary="' . $mime_boundary . '"';
$msg .= '--' . $mime_boundary . PHP_EOL;
$msg .= 'Content-Type: text/plain; charset="UTF-8"' . PHP_EOL;
$msg .= 'Content-Transfer-Encoding: 8bit' . PHP_EOL . PHP_EOL;
$msg .= $mailBody . PHP_EOL . PHP_EOL;
$msg .= '--' . $mime_boundary . '--' . PHP_EOL . PHP_EOL;
ini_set('sendmail_from', $sender_email);
$msg = "Thank you for registering with NADAC.". PHP_EOL. " Here is the info you provided." . PHP_EOL ."Callname: ". $strDogCName . PHP_EOL. "Registered Name: " . $strDogFName . PHP_EOL . "Breed: " . $strBreed .PHP_EOL . "Gender: " . $strGender .PHP_EOL . "Height: " . $strHt .PHP_EOL . "Birthday: " . $strBdmm . "/" . $strBddd . "/" . $strBdyy .PHP_EOL . "Owner First Name: " . $strFName .PHP_EOL . "Owner Last Name: " . $strLName .PHP_EOL . "Address: " . $strAddr1 .PHP_EOL . "City: " . $strCity .PHP_EOL . "State: " . $strState .PHP_EOL . "Zip Code: " . $strZip .PHP_EOL . "Country: " . $strCountry .PHP_EOL . "Phone: " . $strPhone . PHP_EOL . "Email: " . $strEMail .PHP_EOL . "Associate number: " . $member_num. PHP_EOL . "Dog Number: " . $dog_num;
$mailSubject = "Thank you for registering.";
$send_status = mail($mailTo, $mailSubject, $msg, $headers);
$mailTo = $strEMail;
mail($mailTo, $mailSubject, $msg, $headers);
ini_restore('sendmail_from');
// should check send_status here and do something - TODO
unset($_POST['submitted']);
// Done with the mail, display confirmation
I'm sure it's something simple that I'm just missing. But I can't find it, and the programmer working on it doesn't seem to believe the issue. I don't believe it's a local issue with my email provider.

Have you tried with Content-Type: multipart/alternative instead of 'multipart/related'?
The weird thing if the double mail() calling and $send_status to unknown $mailTo var

Related

PHP email form with attachments not working

I'm trying to use a PHP email form I found online that supports attachments, with a bit of extra code, but it's not working, I get a message saying "Sent", but no emails (I checked spam already).
Here is the php file:
function multi_attach_mail($to, $subject, $message, $senderMail, $senderName, $files){
$from = $senderName." <".$senderMail.">";
$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 = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
// preparing attachments
if(count($files) > 0){
for($i=0;$i<count($files);$i++){
if(is_file($files[$i])){
$message .= "--{$mime_boundary}\n";
$fp = #fopen($files[$i],"rb");
$data = #fread($fp,filesize($files[$i]));
#fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" .
"Content-Description: ".basename($files[$i])."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $senderMail;
//send email
$mail = #mail($to, $subject, $message, $headers, $returnpath);
//function return true, if email sent, otherwise return fasle
if($mail){ return TRUE; } else { return FALSE; }
}
$message = "Customer Contact"
. "\n" . "Name: " . $_POST['fullName']
. "\n" . "Email: " . $_POST['email']
. "\n" . "Phone: " . $_POST['phone']
. "\n" . "Current Phone: " . $_POST['currentPhone']
. "\n" . "Address: " . $_POST['address']
. "\n" . "Retailer: " . $_POST['retailer']
. "\n" . "Product Type: " . $_POST['productType']
. "\n" . "Specific Item: " . $_POST['item']
. "\n" . "Purchase Date: " . $_POST['purchaseDate']
. "\n" . "Invoice Number: " . $_POST['invoiceNumber']
. "\n" . "Issue: " . $_POST['issue']
. "\n" . "How did the issue happen?: " . $_POST['how']
. "\n" . "When did the issue occur?: " . $_POST['when']
. "\n" . "Have you done anything to try correcting the issue?: " . $_POST['customerTry'];
$to = 'someEmail#gmail.com';
$from = $_POST['email'];
$from_name = $_POST['fullName'];
//attachment files path array
$files = $_FILES['files'];
$subject = 'Customer Contact From Service Form';
$html_content = $message;
//call multi_attach_mail() function and pass the required arguments
$send_email = multi_attach_mail($to,$subject,$html_content,$from,$from_name,$files);
//print message after email sent
echo $send_email?"<h1> Mail Sent</h1><br>".$message:"<h1> Mail not SEND</h1>";
My form looks like this:
<form NOVALIDATE action="processContact.php" enctype="multipart/form-data" id="claimsForm" method="post" name="claimsForm">
..lots of fields...
<div class="form-group">
<label class="control-label claimsForm-field-header" for="customerTry">Photos:</label>
<input type="file" id="files[]" name="files[]" multiple="multiple" />
</div>
<input class="green-btn" id="ss-submit" name="submit" type="submit" value="Submit">
</form>
The function came with the form I downloaded, so I don't think there is a problem with it. I've been playing around with the code and trying different things, but no luck (I'm not a PHP guy).
EDIT:
Based on the suggestion below, I tried using phpMailer, following the example they have, here is my php file now:
/**
* PHPMailer simple file upload and send example
*/
$msg = '';
$message = "Customer Contact"
. "\n" . "Name: " . $_POST['fullName']
. "\n" . "Email: " . $_POST['email']
. "\n" . "Phone: " . $_POST['phone']
. "\n" . "Current Phone: " . $_POST['currentPhone']
. "\n" . "Address: " . $_POST['address']
. "\n" . "Retailer: " . $_POST['retailer']
. "\n" . "Product Type: " . $_POST['productType']
. "\n" . "Specific Item: " . $_POST['item']
. "\n" . "Purchase Date: " . $_POST['purchaseDate']
. "\n" . "Invoice Number: " . $_POST['invoiceNumber']
. "\n" . "Issue: " . $_POST['issue']
. "\n" . "How did the issue happen?: " . $_POST['how']
. "\n" . "When did the issue occur?: " . $_POST['when']
. "\n" . "Have you done anything to try correcting the issue?: " . $_POST['customerTry'];
$to = 'murtorius#gmail.com';
$from = $_POST['email'];
$from_name = $_POST['fullName'];
//attachment files path array
$files = $_FILES['userfile'];
$subject = 'Customer Contact From Service Form';
if (array_key_exists('userfile', $_FILES)) {
// First handle the upload
// Don't trust provided filename - same goes for MIME types
// See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation
$uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name']));
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
// Upload handled successfully
// Now create a message
// This should be somewhere in your include_path
require 'php-mailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->setFrom($from, $from_name);
$mail->addAddress($to, 'TEST');
$mail->Subject = $subject;
$mail->msgHTML($message);
// Attach the uploaded file
$mail->addAttachment($uploadfile, 'Photos');
if (!$mail->send()) {
$msg .= "Mailer Error: " . $mail->ErrorInfo;
} else {
$msg .= "Message sent!";
}
} else {
$msg .= 'Failed to move file to ' . $uploadfile;
}
}
I changed the input file field name to 'userfile[]', but I get this:
Warning: sha1() expects parameter 1 to be string, array given in /home/zucora/service.zucora.com/processContact.php on line 35
Warning: move_uploaded_file() expects parameter 1 to be string, array given in /home/zucora/service.zucora.com/processContact.php on line 36
If I change the name to 'userfile' (without []) I get a blank screen and no email.

Multi attachment email issue

I have a problem with sending mail attachments , when you leave the empty not including any attachment. Message comes to mail with the extension .asc, it has file size 3 bytes , I would like to stop including this .asc file when attachment was not chosen.
This is the Form code
<?php
/*
error_reporting(E_ALL);
ini_set('display_errors', 1);
*/
if(isset($_FILES) && (bool) $_FILES) {
$allowedExtensions = array("pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt","");
$files = array();
foreach($_FILES as $name=>$file) {
$file_name = $file['name'];
$temp_name = $file['tmp_name'];
$file_type = $file['type'];
$path_parts = pathinfo($file_name);
$ext = $path_parts['extension'];
if(!in_array($ext,$allowedExtensions)) {
die("File $file_name has the extensions $ext which is not allowed");
}
array_push($files,$file);
}
$to = "kontakt#lookslike.pl";
$from = "www.jakasstrona.eu";
$subject = $_POST['imie'];
$message = 'Imię: ' . $_POST['imie'] . "\r\n" .
$message = 'Nazwisko: ' . $_POST['nazwisko'] . "\r\n" .
$message = 'PESEL: ' . $_POST['pesel'] . "\r\n" .
$message = 'NIP: ' . $_POST['nip'] . "\r\n" .
$message = 'Data urodzenia: ' . $_POST['data'] . "\r\n" .
$message = 'Data urodzenia: ' . $_POST['data2'] . "\r\n" .
$message = 'Data urodzenia: ' . $_POST['data3'] . "\r\n" .
$message = 'Obywatelstwo: ' . $_POST['obywatelstwo'] . "\r\n" .
$message = 'Typ: ' . $_POST['typ'] . "\r\n" .
$message = 'Numer Dokumentu: ' . $_POST['nr'] . "\r\n" .
$message = 'Data ważności dokumentu: ' . $_POST['datawaz'] . "\r\n" .
$message = 'Adres, ulica, numer budynku, mieszkania: ' . $_POST['long'] . "\r\n" .
$message = 'Miejscowosc: ' . $_POST['miejscowosc'] . "\r\n" .
$message = 'Kod: ' . $_POST['kod'] . "\r\n" .
$message = 'Poczta: ' . $_POST['poczta'] . "\r\n" .
$message = 'Województwo: ' . $_POST['woj'] . "\r\n" .
$message = 'Telefon komórkowy: ' . $_POST['telefonphon'] . "\r\n" .
$message = 'Telefon stacjonarny: ' . $_POST['telefonstac'] . "\r\n" .
$message = 'E-mail: ' . $_POST['email'] . "\r\n" .
$message = 'Data Wyjazdu: ' . $_POST['datawyj'] . "\r\n" .
$message = 'Data Wyjazdu: ' . $_POST['datawyj2'] . "\r\n" .
$message = 'Data Wyjazdu: ' . $_POST['datawyj3'] . "\r\n" .
$message = 'Doświadczenie zawodowe: ' . $_POST['doswiadczenie'] . "\r\n" .
$message = 'Doświadczenie w pracy zagranicą: ' . $_POST['doswiadczenieza'] . "\r\n" .
$message = 'Uprawnienia na wózki widłowe: ' . $_POST['uprawnienia'] . "\r\n" .
$message = 'Język Angielski: ' . $_POST['jezykang'] . "\r\n" .
$message = 'Język Niemiecki: ' . $_POST['jezyknie'] . "\r\n" .
$message = 'Uwagi: ' . $_POST['uwagi'] . "\r\n" .
$headers = "Od: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; // random
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multi boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"utf-8\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
for($x=0;$x<count($files);$x++){
/*
if (filesize($files[$x] == 3))
{
$y=5;
}
*/
$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";
}
// sending
$ok = mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>Mail został wysłany do $to! </p>";
} else {
echo "<p>Mail nie został wysłany!</p>";
}
}
?>
I think if you don't send an attachment, you don't want to add the extra hash. See a simple tutorial here: http://webcheatsheet.com/php/send_email_text_html_attachment.php
maybe put:
if(count($files))
$message .= "--{$mime_boundary}\n";
Though generally, I agree that if you aren't doing this to learn how it is done, use an open source library for this kind of thing as suggested in the comments.

Include date, IP and HTTP_HOST in Support form

I am trying to include the current date, senders IP and Host in my contact form submission.
This is the code:
<?php
if( isset($_POST['name']) )
{
$to = 'info#mydomain.com'; // Replace with your email
$subject = 'Contact Form Submission'; // Replace with your $subject
$headers = 'From: ' . $_POST['email'] . "\r\n" . 'Reply-To: ' . $_POST['email'];
$date = date('M jS, Y # h:i a');
$IP = $_SERVER['REMOTE_ADDR'];
$Host = $_SERVER['HTTP_HOST'];
$message = 'Name: ' . $_POST['name'] . "\n" .
'E-mail: ' . $_POST['email'] . "\n" .
'Subject: ' . $_POST['subject'] . "\n" .
'Department: ' . $_POST['department'] . "\n" .
'Message: ' . $_POST['message'] . "\n" .
'Date: ' . $_POST['$date'] . "\n" .
'IP: ' . $_POST['$IP'] . "\n" .
'Host: ' . $_POST['$Host'];
mail($to, $subject, $message, $headers);
if( $_POST['copy'] == 'on' )
{
mail($_POST['email'], $subject, $message, $headers);
}
}
?>
The Email is received, but without Date, IP and Host. Any help would be greatly appreciated.
You have already stored(ip, date etc.) in variables so use these variables for mail. also these values are not exists in POST data so try to replace
'Date: ' . $_POST['$date'] . "\n" .
'IP: ' . $_POST['$IP'] . "\n" .
'Host: ' . $_POST['$Host'];
to
'Date: ' . $date . "\n" .
'IP: ' . $IP . "\n" .
'Host: ' . $Host;
You should attach like this(bellow) because $date, $IP and $Host are not transmitted in a post request:
$createdDate = new DateTime();
$date= $createdDate->format('d.m.Y H:m');
$message = 'Name: ' . $_POST['name'] . "\n" .
'E-mail: ' . $_POST['email'] . "\n" .
'Subject: ' . $_POST['subject'] . "\n" .
'Department: ' . $_POST['department'] . "\n" .
'Message: ' . $_POST['message'] . "\n" .
'Date: ' . $date. "\n" .
'IP: ' . $IP. "\n" .
'Host: ' . $Host;

Redirecting from submission form to thank you page in PHP

I am in the process of changing from recaptcha to a honey pot to avoid spam on my site. www.dentistheadhunter.com. I am using www.dentistheadhunter.com/indexpot.php for my trial run and when I submit my form, the process form is working in inputting into my database and sending me an email, but the Thank you form redirect is not working and the process form (http://www.dentistheadhunter.com/resumefrontpot2.php) just ends in a blank page instead of redirecting to my thank_you.php page.
I have read up on previous questions and removed any white space before the starting php tag and that did not solve the problem.
Here is my code
<?php
require("includes/dbconnect/mob_db.php");
$robotest = $_POST['robotest'];
if($robotest)
echo "Thank you for your inquiry Robot.";
$cand_sql = 'INSERT INTO dent_leads (first_name,last_name,disc_id,city,state,zip,area_code,phone,email,location,setting,experience,timestamp,source,notes,resume_path) VALUES ("' . $_POST['first_name'] . '","' . $_POST['last_name'] . '","' . $_POST['disc'] . '","' . $_POST['city'] . '","' . $_POST['state'] . '","' . $_POST['zip'] . '","' . $_POST['area_code'] . '","' . $_POST['phone1'] . $_POST['phone2'] . '","' . $_POST['email'] . '","' . $_POST['location'] . '","' . $_POST['setting'] .'","' . $_POST['experience'] .'","' . time() . '","DentistHeadHunterFP.com","' . $_POST['comments'] . '","' . $target_path .'")';
$cand_res = mysql_query($cand_sql,$leads_db);
if(is_array($_POST['license'])){
foreach($_POST['license'] as $lic_state){
if($lic_state != ''){
$licensed .= ',("' . $dent_id . '","' . $lic_state . '")';
}
}
if(isset($licensed)){
$lic_sql = 'INSERT INTO dent_lic (dent_id,state) VALUES ' . ltrim($licensed,',');
$lic_res = mysql_query($lic_sql,$leads_db);
}
}
$msg .= 'Name: ' . $_POST['first_name'] . ' ' . $_POST['last_name'] . '<br />' . "\r\n";
$msg .= 'Discipline: ' . $_POST['disc'] . '<br />' . "\r\n";
$msg .= 'City: ' . $_POST['city'] . '<br />' . "\r\n";
$msg .= 'State: ' . $_POST['state'] . '<br />' . "\r\n";
$msg .= 'Zip: ' . $_POST['zip'] . '<br />' . "\r\n";
$msg .= 'Phone: ' . $_POST['area_code'] . '-' . $_POST['phone1'] . '-' . $_POST['phone2'] . '<br />' . "\r\n";
$msg .= 'Email: ' . $_POST['email'] . '<br />' . "\r\n";
$msg .= 'Experience: ' . $_POST['experience'] . '<br />' . "\r\n";
$msg .= 'Lead Source: First page ' . $_POST[''] . '<br />' . "\r\n";
$msg .= 'Settings: ' . $_POST['setting'] . '<br />' . "\r\n";
$msg .= ' ' . $_POST[''] . '<br />' . "\r\n";
$msg .= 'Location: ' . $_POST['location'] . '<br />' . "\r\n";
$msg .= ' ' . $_POST[''] . '<br />' . "\r\n";
$msg .= 'First Page Comments: ' . $_POST['comments'] . '<br />' . "\r\n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Return-Path: <dentistjobs#dentistheadhunter.com> ' . "\r\n";
$headers .= 'From: Dentist Head Hunter Candidate <dentistjobs#dentistheadhunter.com> ' . "\r\n";
$headers .= 'Reply-To: Dentist Head Hunter Candidate <dentistjobs#dentistheadhunter.com> ' . "\r\n";
$headers .= 'Bcc: Dentist Head Hunter Candidate <dentistjobs#dentistheadhunter.com> ' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail('dentistjobs#dentistheadhunter.com','NEW HEADHUNTER LEAD',$msg,$headers);
header( 'Location: http://www.dentistheadhunter.com/thank_you.php' ) ;
?>
Seems you are echo message before header()
If you want to echo the message, then write
ob_start();
echo "Thank you for your inquiry Robot.";
header('Location:http://www.dentistheadhunter.com/thank_you.php');
ob_end_flush();

Putting new line feed in the text for an email php

I am using the following code
$message = $mess0 . "</br>" . $mess1 . "</br>" . $mess2 . "</br>" . $mes1 . "</br></br>" . $mes2 . "</br>" . $mes23 . "</br></br>" . $mes3 . "</br></br>" . $mes4 . "</br>" . $mes5 . "</br>" . $mes6 . "</br>" . $mes7 . "</br>" . $mes8 . "</br>" . $mes9 . "</br></br>" . $mes10 ;
$message = "<html><body><p>".$message."</p></body></html>";
$this->Mail($storeEmail, $subject, $message);
function Mail($to, $subject, $message)
{
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: The Vow Engagement Ring Finder <thevow.engagement#gmail.com>' . "\r\n";
// Mail it
if(!mail($to, $subject, $message, $headers)) {
throw new Exception('There was a problem trying to send an email.');
}
}
The problem is all is i get is one paragraph. I have added <br>s but its like they don't work. The mail I get is simple paragraph without any new line feeds.
Mistake in <br /> tags
$message = $mess0 . "</br>" . $mess1...
^^^^^^
Replace all </br> with <br />
It will not work. You have to use \r\n for line breaks. Also try <br />.

Categories