email form not sending emails without attachments

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wak0
Forum Newbie
Posts: 22
Joined: Wed Jan 20, 2010 2:31 pm

email form not sending emails without attachments

Post by wak0 »

I was able to make the form work to send emails with my form content and attachment.

but the form does not work (meaning i dont receive an email if there is no attachment)

how can i make the form dont have this type of requirement. here is the code i am using

<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){

// we'll begin by assigning the To address and message subject
$to="myemail@gmail.com";

$subject="Client Submission";

// get the sender's name and email address
// we'll just plug them a variable to be used later
$from = $_REQUEST['fromname'];
$email=$_REQUEST["fromemail"];
$occasion = $_REQUEST['occasion'];
$age = $_REQUEST['age'];
$flavor = $_REQUEST['flavor'];
$filling = $_REQUEST['filling'];
$people = $_REQUEST['people'];
$day = $_REQUEST['day'];
$month = $_REQUEST['month'];
$delivery = $_REQUEST['delivery'];
$text = $_REQUEST['text'];
$phone=$_REQUEST['phone'];

// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

// store the file information to variables for easier access
$tmp_name = $_FILES['filename']['tmp_name'];
$type = $_FILES['filename']['type'];
$name = $_FILES['filename']['name'];
$size = $_FILES['filename']['size'];

// here we'll hard code a text message
// again, in reality, you'll normally get this from the form submission
$message = $message = "
Email = $fromemail
Name = $fromname
Phone = $phone
Occasion = $occasion
Age = $age
Flavor = $flavor
Filling = $filling
People = $people
Month = $month
Day = $day
Delivery = $delivery
Text = $text";

// if the upload succeded, the file will exist
if (file_exists($tmp_name)){

// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){

// open the file for a binary read
$file = fopen($tmp_name,'rb');

// read the file content into a variable
$data = fread($file,filesize($tmp_name));

// close the file
fclose($file);

// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}

// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";

// next, we'll build the message body
// note that we insert two dashes in front of the
// MIME boundary when we use it
$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";

// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content and set another boundary to
// indicate that the end of the file has been reached
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";

// now we just send the message
if (@mail($to, $subject, $message, $headers))
echo "Message Sent";
else
echo "Failed to send";
}
} else {
?>
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: email form not sending emails without attachments

Post by Jade »

Please use the code tags when you post code, it makes it easier to read. Don't attach the content headers for encode-64 if there's no attachment to send.

Code: Select all

 
// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content and set another boundary to
// indicate that the end of the file has been reached
 
if (isset($data))
{
  $message .= "--{$mime_boundary}\n" .
  "Content-Type: {$type};\n" .
  " name=\"{$name}\"\n" .
  //"Content-Disposition: attachment;\n" .
  //" filename=\"{$fileatt_name}\"\n" .
  "Content-Transfer-Encoding: base64\n\n" .
  $data . "\n\n" .
  "--{$mime_boundary}--\n";
}
 
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: email form not sending emails without attachments

Post by AbraCadaver »

I think you need the closing boundary delimiter though:

Code: Select all

if (isset($data))
{
  $message .= "--{$mime_boundary}\n" .
  "Content-Type: {$type};\n" .
  "name=\"{$name}\"\n" .
  "Content-Transfer-Encoding: base64\n\n" .
  $data . "\n\n";
}
$message .=  "--{$mime_boundary}--\n";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
wak0
Forum Newbie
Posts: 22
Joined: Wed Jan 20, 2010 2:31 pm

Re: email form not sending emails without attachments

Post by wak0 »

try both solutions, and no luck. any other idea, my code look like this now

Code: Select all

if (isset($data))
{
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: email form not sending emails without attachments

Post by Jade »

Try this:

Code: Select all

 
function SendMail($From,$FromName,$To,$ToName,$Subject,$Text,$Html,$AttmFiles){
 $OB="----=_OuterBoundary_000";
 $IB="----=_InnerBoundery_001";
 $Html=$Html?$Html:preg_replace("/\n/","{br}",$Text) 
  or die("neither text nor html part present.");
 $Text=$Text?$Text:"Sorry, but you need an html mailer to read this mail.";
 $From or die("sender address missing");
 $To or die("recipient address missing");
   
 $headers ="MIME-Version: 1.0\r\n"; 
 $headers.="From: ".$FromName." <".$From.">\n"; 
 $headers.="To: ".$ToName." <".$To.">\n"; 
 $headers.="Reply-To: ".$FromName." <".$From.">\n"; 
 $headers.="X-Priority: 1\n"; 
 $headers.="X-MSMail-Priority: High\n"; 
 $headers.="X-Mailer: My PHP Mailer\n"; 
 $headers.="Content-Type: multipart/mixed;\n\tboundary=\"".$OB."\"\n";
 
 //Messages start with text/html alternatives in OB
 $Msg ="This is a multi-part message in MIME format.\n";
 $Msg.="\n--".$OB."\n";
 $Msg.="Content-Type: multipart/alternative;\n\tboundary=\"".$IB."\"\n\n";
 
 //plaintext section 
 $Msg.="\n--".$IB."\n";
 $Msg.="Content-Type: text/plain;\n\tcharset=\"iso-8859-1\"\n";
 $Msg.="Content-Transfer-Encoding: quoted-printable\n\n";
 // plaintext goes here
 $Msg.=$Text."\n\n";
 
 // html section 
 $Msg.="\n--".$IB."\n";
 $Msg.="Content-Type: text/html;\n\tcharset=\"iso-8859-1\"\n";
 $Msg.="Content-Transfer-Encoding: base64\n\n";
 // html goes here 
 $Msg.=chunk_split(base64_encode($Html))."\n\n";
 
 // end of IB
 $Msg.="\n--".$IB."--\n";
 
 // attachments
 if($AttmFiles){
  foreach($AttmFiles as $AttmFile){
   $patharray = explode ("/", $AttmFile); 
   $FileName=$patharray[count($patharray)-1];
   $Msg.= "\n--".$OB."\n";
   $Msg.="Content-Type: application/octetstream;\n\tname=\"".$FileName."\"\n";
   $Msg.="Content-Transfer-Encoding: base64\n";
   $Msg.="Content-Disposition: attachment;\n\tfilename=\"".$FileName."\"\n\n";
           
   //file goes here
   $fd=fopen ($AttmFile, "r");
   $FileContent=fread($fd,filesize($AttmFile));
   fclose ($fd);
   $FileContent=chunk_split(base64_encode($FileContent));
   $Msg.=$FileContent;
   $Msg.="\n\n";
  }
 }
   
 //message ends
 $Msg.="\n--".$OB."--\n";
 return mail($To,$Subject,$Msg,$headers);    
 //syslog(LOG_INFO,"Mail: Message sent to $ToName <$To>");
} 
 
Post Reply