Now, i can send out the form attachment to my email. But the problem now, if i don't upload any files, it will show the error 'Could not open file ''. how to make it not necessarily upload by the user?
These is my new code:
<? require_once("Connections/pamconnection.php");
if($_POST['Submit']=='Submit'){
$email_from = $_POST['email02']; // Who the email is from
$email_subject = " Booking Form"; // The Subject of the email
$email_message.='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<!--<style>
.title{ font-family:Verdana, Arial, Helvetica, sans-serif; font-size:14px; font-weight:bold; }
.content{ font-family:Verdana, Arial, Helvetica, sans-serifl; font-size:12px;}
</style>-->
<body>
<table width="600" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000">
<tr><td>
<table width="100%" border="0" align="center" cellpadding="4" cellspacing="6" bgcolor="#DCE1E9" class="content">
<tr>
<td colspan="2" class="title">Online Booking Form</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2" align="right">'.date("jS F Y").'</td>
</tr>
<tr class="style9">
<td> </td>
</tr>';
i<strong>ITINERARY</strong></td></tr>';
if($_POST['pdf_file']!=''){
$email_message.='<tr class="style9">
<td valign="top" class="title02">Itinerary File</td>
<td class="email_message">'.$_POST['pdf_file'].'</td>
</tr>';}
if($_POST['itinerary']!=''){
$email_message.='<tr class="style9">
<td valign="top" class="title02">Itinerary</td>
<td class="email_message">'.$_POST['itinerary'].'<br><br></td>
</tr>';}
$email_message.='<tr class="style9">
<td colspan="2" valign="top"><div align="center">
</div></td>
</tr>
</table>
</td></tr></table></body>
</html>';
$email_to = "myname@hotmail.com"; // Who the email is to
ini_set(SMTP, "mail.sarawakhost.com");
ini_set(smtp_port, "587");
ini_set(sendmail_from, $email);
//ini_set('display_errors', 1); // set to 0 for production version
//error_reporting(E_ALL);
$headers = "MIME-Version: 1.0" . "\r\n";
$headers.= "Content-type:text/html;charset=iso-8859-1" . "\r\n";// More headers
$headers.= 'From: <Website Visitors>' . "\r\n";
if($_FILES['pdf_file']!='')
{
$fileatt = $_FILES['pdf_file']['tmp_name'];
$fileatt_type = $_FILES['pdf_file']['type'];
$file_name = $_FILES['pdf_file']['name'];
$ext = substr(strrchr($fileatt_type, "/"), 1);
switch ( $ext )
{
case 'pdf':
$fileatt_name = $file_name;
break;
case 'msword':
$fileatt_name = $file_name;
break;
case 'vnd.openxmlformats-officedocument.wordprocessingml.document':
$fileatt_name = $file_name;
break;
}
}
$file = fopen($fileatt,'rb');
if($file == false) {
die("Could not open file '$fileatt'");
}
$data = fread($file,filesize($fileatt)) or die ("\n Unable to read file");
fclose($file) or die("Unable to close file");
$data = chunk_split(base64_encode($data));
$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";
$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);
if($ok) {
$send='<font color=#336600>Feedback sent</font>';
} else {
$send='<font color=#CC3300>Failed to send. Please try again.</font>';
}
}?>