How to send HTML Dynamic Table as mail in php? - php

Here i have a code like getting the data from index.php , if user enter the data it will show like table and as well as it should go to email to some predefined mail. But here i am getting data Dynamically , i tried with $_REQUEST and $_POST both methods to get the data , but in mail function i am trying to change message parameter but by php tags its is not taking and showing some syntax errors.
please go through code here
<?php
$to="xxxxxxx#gmail.com";
$fn="Fisrt Name";
$ln="Last Name";
$name=$fn.' '.$ln;
$from="xxxxx#xxx.com";
$subject = "Welcome to Website";
include('newsmtp/smtpwork.php');
?>
<?php
$message = 'Dear $firstName,
Your Welcome Message.'.'
<table border=1>
<tr>
<td>First Name:</td>
<td><?php $firstName=$_POST['firstname'];
echo $firstName;?></td>
</tr>
<tr>
<td>Last Name:</td>
<td><?php $lastname=$_POST['lastname'];
echo $lastname;?></td>
</tr>
<tr>
<td>Title:</td>
<td><?php $title=$_POST['title'];
echo $title;?></td>
</tr>
<tr>
<td>Address:</td>
<td><?php $address=$_POST['address'];
echo $address;?></td>
</tr>
<tr>
<td>Phone Number:</td>
<td><?php $phone=$_POST['phone'];
echo $phone;?></td>
</tr>
<tr>
<td>Course Name:</td>
<td><?php $course=$_POST['coursename'];
echo $course;?></td>
</tr>
<tr>
<td>Website:</td>
<td><?php $website=$_POST['website'];
echo $website;?></td>
</tr>
</table>
Thanks
xxxxxxxxxxxx
';
?>

Try this method, assign the posted values in variables, outside the $message variable and print the variable inside your $message variable
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$title = $_POST['title'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$course = $_POST['course'];
$website = $_POST['website'];
$message = 'Dear '.$firstName.',
Your Welcome Message.'.'
<table border=1>
<tr>
<td>First Name:</td>
<td>'.$firstname.' </td>
</tr>
<tr>
<td>Last Name:</td>
<td>'.$lastname.'</td>
</tr>
<tr>
<td>Title:</td>
<td>'.$title.'</td>
</tr>
<tr>
<td>Address:</td>
<td>'.$address.'</td>
</tr>
<tr>
<td>Phone Number:</td>
<td>'.$phone.'</td>
</tr>
<tr>
<td>Course Name:</td>
<td>'.$course.'</td>
</tr>
<tr>
<td>Website:</td>
<td>'.$website.'</td>
</tr>
</table>
Thanks
xxxxxxxxxxxx
';
?>
$to ="xxxxxxx#gmail.com";
$subject = "Welcome to Website";
$headers = 'From: xxxxxx <noreply#xxxxxxx.com>' . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;\n\tcharset=\"iso-8859-1\"\r\n";
$sent = #mail($to,$subject,$message,$headers);
if ($sent) {
return true;
} else {
return false;
}
Also add necessary header to send HTML mail as Vinoth Babu, Naveen suggested

$to = "naveen#gmail.com";
$subject = $name ." is contact ";
$headers = "MIME-Version: 1.0" . "\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\n";
#mail($to,$subject,$message,$headers);
set headers to mime version & type of your message like text/html

To send HTML Content/Tables you have to add the following in the header of that mail,
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $message, $headers);
please refer the link below,
Mail Function

Related

Tell a friend script not sending email php [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I am trying to execute "Tell a friend" php script, but that is not sending any emails, not to admin, friends, sender. Other emails on same server working fine.
I don't understand why this is not working, as other emails pages like contact us, registration (which send confimation email) all working on same server, please help me..
Html Code:
<table>
<tr>
<td>
<span>Complete the details below to send this link to a friend:</span>
<?php
$refurl = $_SERVER['HTTP_REFERER'];?>
<span><? print $refurl;?></span>
<form name="tellafriend" action="send_group.php" method="post" onSubmit="return checkfields()">
<table>
<tr>
<td> Your name*:</td>
<td> <input name="name" size="30" maxlength="45"> </td>
</tr>
<tr>
<td>Your email*:</td>
<td><input name="email" size="30" maxlength="45"></td>
</tr>
<tr>
<td colspan="2"><p align="center">Enter your friend's email addresses:</p>
</td>
</tr>
<tr>
<td>Email 1*:</td>
<td><input name="fmail1" class="bordesolid1" size="30" maxlength="50"></td>
</tr>
<tr>
<td>Email 2*:</td>
<td><input name="fmail2" size="30" maxlength="50"></td>
</tr>
<tr>
<td>Email 3*:</td>
<td><input name="fmail3" size="30" maxlength="50"></td>
</tr>
<tr>
<td colspan="2"><p align="center"><span>This message will contain your name & email address.</span>
<br>
<input onClick="validate();" type="button" value="click once to send">
<input type=hidden name=refurl value="<? print $refurl;?>">
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
PHP Code:
<?php
if(count($_POST)) {
foreach(array('fmail1','fmail2','fmail3','email','name') as $key) $_POST[$key] = strip_tags($_POST[$key]);
if(!is_secure($_POST)) {
die("Peace People! Stop Spamming!");
}
$name = $_POST[name];
$email = $_POST[email];
$fmail1 = $_POST[fmail1];
$fmail2 = $_POST[fmail2];
$fmail3 = $_POST[fmail3];
$refurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$to = "arvindsri123#yahoo.com";
$subject = "Recommendation form submission";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$email."\r\n";
$headers .= 'Reply-To: '.$email."\r\n".
'X-Mailer: PHP/' . phpversion();
$message = '<html><body>';
$message.='<p style="margin-top:10px;">'.$name.' has used your recommendation form using an email address of '.$email.' </p>';
$message.='<p style="margin-top:10px;">The people the recommendation has been submitted to are: </p>';
$message.='<p style="margin-top:10px;">'.$fmail1.' </p>';
$message.='<p style="margin-top:10px;">'.$fmail2.' </p>';
$message.='<p style="margin-top:10px;">'.$fmail3.' </p>';
$message.='<p style="margin-top:10px;">The page recommended:</p>';
$message.='<p style="margin-top:10px;">'.$refurl.'</p>';
$message .= '</body></html>';
$sentmail = mail($to, $subject, $message, $headers);
// $thankyoupage = "thankyou.htm";
//echo $sentmail;
if($sentmail) {
$name = $_POST[name];
$email = $_POST[email];
$fmail1 = $_POST[fmail1];
$fmail2 = $_POST[fmail2];
$fmail3 = $_POST[fmail3];
$refurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$tsubject = "A web page recommendation from $_POST[name]";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$email."\r\n";
$headers .= 'Reply-To: '.$email."\r\n".
'X-Mailer: PHP/' . phpversion();
$message = '<html><body>';
$message.='<p style="margin-top:10px;">Hi, '.$name.' whose email address is $_POST[email] thought you may be interested in this web page. '.$email.' </p>';
$message.='<p style="margin-top:10px;">'.$refurl.'</p>';
$message .= '</body></html>';
$sentmail = mail($fmail1,$fmail2,$fmail3, $tsubject $message, $headers);
echo '<h4>You have sent emails...</h4>';
//header("Location: $thankyoupage");
exit;
}
function is_secure($ar) {
$reg = "/(Content-Type|Bcc|MIME-Version|Content-Transfer-Encoding)/i";
if(!is_array($ar)) {
return preg_match($reg,$ar);
}
$incoming = array_values_recursive($ar);
foreach($incoming as $k=>$v) if(preg_match($reg,$v)) return false;
return true;
}
function array_values_recursive($array) {
$arrayValues = array();
foreach ($array as $key=>$value) {
if (is_scalar($value) || is_resource($value)) {
$arrayValues[] = $value;
$arrayValues[] = $key;
}
elseif (is_array($value)) {
$arrayValues[] = $key;
$arrayValues = array_merge($arrayValues, array_values_recursive($value));
}
}
return $arrayValues;
}
?>
I am using Bluehost web hosting, and other emails properly working as I said Contact us, Registration confirmation emails.. etc.
Thanks!
If you are running on localhost then it won't send email.. to do so you need to host on server
Try to host on free hosting server.. it will work!

PHP Form - not receiving email

I'm not understanding why Im not receiving email from the form after filling out the simple form. After clicking the submit button it redirected to the thank you page with no problem, but no email.
HTML
<form class="action" name="form1" method="POST" action="_sendmail2.php" onSubmit="return CheckAll(this);">
<label class="nick-2">Full Name:</label><br>
<input type="text" class="name" name="full_name">
<label class="nick">Email Address:</label><br>
<input type="text" class="email" name="email"><br>
<div class="radio-toolbar">
<input type="radio" id="radio1" name="agent_type" value="Buyer" checked>
<label for="radio1">Buyer</label>
<input type="radio" id="radio2" name="agent_type" value="Seller">
<label for="radio2">Seller</label>
<input type="radio" id="radio3" name="agent_type" value="Investor">
<label for="radio3">Investor</label>
</div><br>
<input type="submit" class="btn" value="SUBMIT" name="Submit">
</form>
PHP (
<?php
$to = "cluneborg#hotmail.com";
$subject = "New Agent Inquries";
$full_name = $_POST['full_name'];
$email = $_POST['email'];
$agent_type = $_POST['agent_type'];
if($_SERVER['REQUEST_METHOD']=="POST") {
$full_name=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['full_name']));
$email=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['email']));
$agent_type=str_replace ( array("\n"), array(" <br>"),trim($_REQUEST['agent_type']));
$contentmsg=stripslashes("<br><b><font style=color:#CC3300>$subject</font></b><br>
<table width=708 border=0 cellpadding=2 cellspacing=1 bgcolor=#CCCCCC>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Full Name: </b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF> $full_name</td>
</tr>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Email Address: </b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF> $email</td>
</tr>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Type of Agent:</b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF> $agent_type</td>
</tr>
</table>
");
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= 'To: Eric <eluneborg#gmail.com>' . "\r\n";
$headers .= 'From: Texas Real Estate Agent Website' . "\r\n";
if(mail($to,$subject,$contentmsg,$headers)){
header("Location: http://www.magnixsolutions.com/clients/tas/thanks.html");
}
else
{
echo "Mail was not sent!";
}
}
?>
Sometimes it sends email to my hotmail and most of time it get this (checked on cpanel)
This message was created automatically by mail delivery software.
A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:
cluneborg#hotmail.com
Domain magnixsolutions.com has exceeded the max defers and failures per hour (5/5 (55%)) allowed. Message discarded.
(Tested) - There were a few issues with your code.
The most important thing, is the # symbol in #mail - This will not execute, it needs to be removed.
Now, this line: (in PHP)
$_REQUEST['type_agent']
should be:
$_REQUEST['agent_type']
as per: (in HTML form)
<input type="radio" id="radio3" name="agent_type" value="Investor">
Then your headers were incorrect, where I added a few \r\n
One of your headers (in PHP)
$headers .= "From: ".$from."";
has been changed to:
$headers .= "From: $full_name <$email>\r\n";
Sidenote: It could be replaced with
$headers .= "From: $fromemail <$email>\r\n";
if you want the name to appear as "New Agent" in mail, instead of the person's name sending the Email.
Using this $fromemail="New Agent"; in conjunction with $from=$fromemail; and $headers .= "From: ".$from."";
would have resulted in mail going to SPAM, being it's not an actual Email address.
Plus, upon testing your original code, it did not come in as proper HTML, but the codes themselves showed up in the Email; that has been corrected.
If you want the Email and the name, you need to use two different variables.
I.e.:
$headers .= 'From: YourName <YourName#domain.com>' . "\r\n";
and in your case:
$headers .= "From: $full_name <$email>\r\n";
Rewrite: (PHP)
<?php ob_start();
// commented out - is not needed for the time being
// $fromemail="New Agent"; // change here if you want
$toemail="email#example.com"; // change here if you want
$sub="Agent Inquiries"; // change here if you want
$success_page_name="thanks.html";
////// do not change in following
if($_SERVER['REQUEST_METHOD']=="POST")
{
$full_name=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['full_name']));
$email=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['email']));
$type_agent=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['agent_type']));
$contentmsg=stripslashes("<br><b><font style=color:#CC3300>$sub</font></b><br>
<table width=708 border=0 cellpadding=2 cellspacing=1 bgcolor=#CCCCCC>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Full Name: </b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF>$full_name</td>
</tr>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Email Address: </b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF>$email</td>
</tr>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Type of Agent:</b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF>$type_agent</td>
</tr>
</table>
");
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "From: $full_name <$email>\r\n";
#mail($toemail,$sub,$contentmsg,$headers);
header("Location:$success_page_name");
}
?>
Footnotes:
Including the # symbol in #mail suppresses errors and does not execute the function, so you will want to remove it..
In my testing, I removed onSubmit="return CheckAll(this); since your full code didn't include that function. Should it fail, then you may need to remove it also.
Remove the # sign from the #mail command and it might give you a helpful error. The # sign there is suppressing errors:
http://www.php.net/manual/en/language.operators.errorcontrol.php
You need to add EOL character \n in headers to separate. Don't know whether this is the solution, but it is at least one problem that needs attention.
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$from=$fromemail;
$headers .= "From: ".$from."\n";
In addition to the error suppression answers/comments, you can also make sure that mail() returns true indicating your server has accepted it and will attempt delivery.
$success = mail($toemail,$sub,$contentmsg,$headers);
var_dump( $success ); // should be true

PHP Form processing not send proper email

I have the follwing form which is being populated via autosuggest from a database
<table class="table table-striped" id="itemsTable">
<thead>
<tr>
<th></th>
<th>Item Code</th>
<th>Description</th>
<th>Qty</th>
<th>Price</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr class="item-row">
<td></td>
<form id="itemsForm" action="services/processOrder.php" method="post">
<td><input type="text" name="itemCode[]" value="" class="input-medium" id="itemCode"
tabindex="1"/>
</td>
<td><input type="text" name="itemDesc[]" value="" class="input-large" id="itemDesc"
readonly="readonly"/></td>
<td><input type="text" name="itemQty[]" value="" class="input-mini" id="itemQty" tabindex="2"/>
</td>
<td>
<div class="input-prepend input-append"><span class="add-on">€</span>
<input
name="itemPrice[]"
class=" input-small"
id="itemPrice"
type="text"></div>
</td>
<td>
<div class="input-prepend input-append"><span class="add-on">€</span><input
name="itemLineTotal[]" class=" input-small" id="itemLineTotal" type="text"
readonly="readonly"></div>
</td>
</tr>
</tbody>
</table>
and here is the php query for the autosuggest:
<?php
require_once('db_connection.php');
$return_arr = array();
$param = $_GET["term"];
$query = "SELECT field_id_5, exp_weblog_titles.title, field_id_57
FROM exp_weblog_data, exp_weblog_titles
WHERE exp_weblog_titles.entry_id = exp_weblog_data.entry_id AND field_id_5
LIKE '%". $param ."%'
LIMIT 10";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
/* Retrieve and store in array the results of the query.*/
while ($row = $result->fetch_assoc()) {
$row_array['jItemCode'] = $row['field_id_5'];
$row_array['jItemDesc'] = $row['title'];
$row_array['jItemPrice'] = $row['field_id_57'];
//$row_array['jItemWholesale'] = $row['itemWholesale'];
//$row_array['jItemRetail'] = $row['itemRetail'];
// $row_array['jQtyOnHand'] = $row['qtyOnHand'];
array_push( $return_arr, $row_array );
}
$result->free_result();
$mysqli->close();
/* Toss back results as json encoded array. */
echo json_encode($return_arr);
Finally here is the php for send the form as an email:
<?php
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: " . $_REQUEST['repName'] . ">\r\n";
$to = 'dominic.mcafee#pmcnttyyre.com';
$subject = "Web Contact Data";
$fields = array();
$fields{"itemCode[]"} = "Code";
$fields{"itemDesc[]"} = "Description";
$fields{"itemPrice[]"} = "Price";
$body = "We have received the following information:\n\n";
foreach($fields as $a => $b){
$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);
}
$headers2 = "From: noreply#example.com";
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.oursite.com";
$send = mail($to, $subject, $body);
if($send){
header( "Location:index.php" );
} else {
print "We encountered an error sending your mail, please try again";
}
?>
All I get in the email is "We have recieved the following information:
Code:
Description:
Price:
"
Please can someone help me out. Where am I going wrong. I know Im close and the problem lies somewhere in referencing the array of data that is in the form.
Hard to say from code, but main reason is that $_REQUEST[$a] is empty. So try to echo whole $_REQUEST right after submitting your form for better debugging.
Also I dont see submit button and also end of the form tag - it could be bug too.

Process order form with php to send email

I have the following code:
<table class="table table-striped" id="itemsTable">
<thead>
<tr>
<th></th>
<th>Item Code</th>
<th>Description</th>
<th>Qty</th>
<th>Price</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr class="item-row">
<td></td>
<td><input type="text" name="itemCode[]" value="" class="input-medium" id="itemCode"
tabindex="1"/>
</td>
<td><input type="text" name="itemDesc[]" value="" class="input-large" id="itemDesc"
readonly="readonly"/></td>
<td><input type="text" name="itemQty[]" value="" class="input-mini" id="itemQty" tabindex="2"/>
</td>
<td>
<div class="input-prepend input-append"><span class="add-on">€</span>
<input
name="itemPrice[]"
class=" input-small"
id="itemPrice"
type="text"></div>
</td>
<td>
<div class="input-prepend input-append"><span class="add-on">€</span><input
name="itemLineTotal[]" class=" input-small" id="itemLineTotal" type="text"
readonly="readonly"></div>
</td>
</tr>
</tbody>
</table>
What is the best way to process the inputs via php to send the order via email nicley formated into a table? This is an order form and I need to to simply be sent to an email once complete
Here is my processing code:
<?php
$to = $_REQUEST['xxx'] ;
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$headers = "From: $from";
$subject = "Web Contact Data";
$fields = array();
$fields{"itemCode"} = "Code";
$fields{"itemDesc"} = "Description";
$fields{"itemPrice"} = "Price";
$body = "We have received the following information:\n\n";
foreach($fields as $a => $b){
$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);
}
$headers2 = "From: noreply#example.com";
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.oursite.com";
$send = mail($to, $subject, $body);
if($send){
header( "Location:index.php" );
} else {
print "We encountered an error sending your mail, please try again";
}
?>
This code is not working please help
To process a form this way you need to have a form element somewhere in your markup to process.
<form method="POST" action="yourSecondScript.php">
your first markup here
<input type="submit">
<form>
Then to make the email nice with tables you need to set the email headers to html.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: " . $_REQUEST['Name'] . ">\r\n";
mail($to, $subject, $body, $headers);
Where is
$to = $_REQUEST['xxx'] ;
coming from? My guess is that you can set it to fixed as you probably don't need a dynamic e-mail to address, so something like:
$to = 'myname#mydomain.com';
But as Michael mentioned in a reaction to your question, we cannot be sure until you tell us which part is not working / what errors you receive and so on..

Multiple file upload together with excel form [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
We presently have a form that when submitted the user responses are emailed to us as an attached excel file. We also have another form that allows the user to upload multiple files of various types which are then emailed to us as attachments. However we have been unable to combine the two into one form, so that we can receive an email with both types of attachments. We are beginners...
Below is:
1. The existing form which attaches the single excel content
2. the existing mailer.php
3. the existing form which uploads multiple files
1. excel content form
<?php
error_reporting(E_ALL ^ E_NOTICE);
ob_start();
$taxyear = $_POST['taxyear'];
$company = $_POST['company'];
$tfn = $_POST['tfn'];
$director1 = $_POST['director1'];
$director2 = $_POST['director2'];
$email = $_POST['email'];
?>
<html>
<body>
<form>
<table width="720" border="0" align="center" bgcolor="#FFFFFF">
<tr align="center" valign="top">
<td height="1730">COMPANY RETURN
<table width="702" border="1" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
<tr>
<td colspan="6" align="left" class="whiteonred" >Company <strong><? echo $taxyear; ?> </strong></td>
</tr>
<tr class="excel7" style="height:18.0pt;">
<td colspan="4" align="left" class="centgothicstd" ><strong>PRINCIPAL DETAILS </strong></td>
<td colspan="2" align="right" class="centgothicsm"> </td>
</tr>
<tr style="height:18.0pt;">
<td align="left" class="centgothicsm" >COMPANY NAME & TFN</td>
<td colspan="3" align="left" class="borders"><strong><? echo $company; ?></strong></td>
<td colspan="2" align="left" class="borders"><strong><? echo $tfn; ?></strong></td>
</tr>
<tr style="height:18.0pt;">
<td width="213" align="left" class="centgothicsm" style="height:18.0pt;">DIRECTOR NAMES</td>
<td colspan="3" align="left" class="borders"><? echo $director1; ?></td>
<td colspan="2" align="left" class="borders"><? echo $director2; ?></td>
</tr>
<tr style="height:18.0pt;">
<td width="213" align="left" class="centgothicsm" style="height:18.0pt;">EMAIL ADDRESS</td>
<td colspan="5" align="left" class="borders"><? echo $email; ?></td>
</tr>
</table></td>
</tr>
</table>
</form>
</body>
</html>
<?php
$FILE_CONTENTS = ob_get_contents();
ob_clean();
include("mailer.php");
$recipient = "darren#eto.net.au";
$subject = "Company submission";
$myEmail = new EPDEV_Emailer($recipient, $email, $subject);
$myEmail->addFile("{$company}-{$taxyear}.xls", "application/vnd.ms-excel", $FILE_CONTENTS);
$myEmail->send();
Header("Location: thankyouapplic.htm");
?>
*2. the mailer.php code *
<?php
class EPDEV_Emailer
{
var $message;
var $FILES;
var $EMAIL;
function EPDEV_Emailer($to_address, $from_address, $subject, $reply_address=null, $mailer=null, $custom_header=null)
{
$this->EMAIL = array(
"to" => $to_address,
"from" => $from_address,
"subject" => $subject,
"reply" => (empty($reply_address) ? $from_address : $reply_address),
"mailer" => (empty($mailer) ? "X-Mailer: PHP/" . phpversion() : $mailer),
"header" => (empty($custom_header) ? "" : $custom_header),
"boundary" => "_mimeboundary_".md5(uniqid(mt_rand(), 1))
);
$this->message = "";
$this->FILES = array();
}
function addFile($filename, $type=null, $filecontents=null)
{
if ($filecontents !== null)
{
$index = count($this->FILES);
$this->FILES[$index]['data'] = chunk_split(base64_encode($filecontents));
$this->FILES[$index]['name'] = basename($filename);
if (empty($type))
$this->FILES[$index]['mime'] = mime_content_type($filename);
else
$this->FILES[$index]['mime'] = $type;
}
else if (file_exists($filename))
{
$index = count($this->FILES);
$this->FILES[$index]['data'] = chunk_split(base64_encode(file_get_contents($filename)));
$this->FILES[$index]['name'] = basename($filename);
if (empty($type))
$this->FILES[$index]['mime'] = mime_content_type($filename);
else
$this->FILES[$index]['mime'] = $type;
}
else
{
$this->Error_Handle("File specified -- {$filename} -- does not exist.");
}
}
function addText($text)
{
$this->message .= $text;
}
function getHeader()
{
$header = "From: {$this->EMAIL['from']}\r\n"
. "Reply-To: {$this->EMAIL['reply']}\r\n"
. "X-Mailer: {$this->EMAIL['mailer']}\r\n"
. "MIME-Version: 1.0\r\n"
. "Content-Type: multipart/mixed; boundary=\"{$this->EMAIL['boundary']}\";\r\n";
return $header;
}
function getEmail()
{
$content .= "--{$this->EMAIL['boundary']}\r\n"
. "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"
. "Content-Transfer-Encoding: 7bit\r\n\r\n"
. $this->message . "\r\n";
if (!empty($this->FILES))
{
foreach($this->FILES as $file)
{
$content .= "--{$this->EMAIL['boundary']}\r\n"
. "Content-Type: {$file['mime']}; name=\"{$file['name']}\"\r\n"
. "Content-Transfer-Encoding: base64\r\n"
. "Content-Disposition: attachment\r\n\r\n"
. $file['data'] . "\r\n";
}
}
$content .= "--{$this->EMAIL['boundary']}--\r\n";
return $content;
}
function send()
{
$result = mail($this->EMAIL['to'], $this->EMAIL['subject'], $this->getEmail(), $this->getHeader());
if (!$result)
$this->Error_Handle("The email failed to send.");
}
function Error_Handle($error)
{
die($error);
}
}
3. the upload form
<body>
<td height="353" colspan="9" align="left" valign="top">
<form enctype="multipart/form-data" name="send" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="hidden" name="action" value="send" />
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<tr>
<td width="300" align="center"> </td>
<table width="500" height="407" border="0" align="center">
<tr>
<td width="120" align="left" class="indextextlight">Name:</td>
<td colspan="2"><input name="fname" type="text" class="indextextlight" size="30" /></td>
</tr>
<tr>
<td width="120" height="24" align="left" class="indextextlight">E-mail:</td>
<td colspan="2"><label>
<input name="email" type="text" class="indextextlight" size="30" />
</label></td>
</tr>
<tr>
<td width="120" align="left" class="indextextlight">Telephone:</td>
<td colspan="2"><label>
<input name="tel" type="text" class="indextextlight" id="tel" value="" size="15" />
</label></td>
</tr>
<tr>
<td width="120" align="left" valign="top" class="indextextlight">Message:
</td>
<td colspan="2" valign="top"><textarea name="comments" " id="comments" cols="45" rows="4"></textarea></td>
</tr>
<tr>
<td width="120" valign="middle" class="indextextlight">Upload Files:</td>
<td colspan="2"><p>
<input name="attachment[]" type="file" multiple="" class="indextextlight" size="42">
<br />
<span class="centgothicmini"> note - hold the Ctrl key to select multiple files</span><br />
<span class="centgothicmini"> note - max total size of all files can not exceed 10mb</span></td>
</tr>
<tr>
<td width="120" align="left" class="indextextlight">Send:</td>
<td colspan="2" align="left"><input type="image" name="submit" value="Send Email" src="images/btnSubmit.gif" /></td>
</tr>
<tr>
<td width="120" class="indextextlight">Result:</td>
<td colspan="2" class="header"><?php
/* Mailer with Attachments */
$action = $_REQUEST['action'];
global $action;
function showform(){
?></td>
</tr>
<tr>
<td class="indextextlight"> </td>
<td colspan="2" class="header"> </td>
</tr>
<tr><td colspan="3" align="right" valign="baseline" class="footer"><span class="footerheadings"> </span> © 2009-13 BC Accountants Australia Pty Ltd</td>
</tr>
</table>
</form>
<script type="text/javascript">
var frmvalidator = new Validator("send");
frmvalidator.addValidation("fname","req","Please enter your Name");
frmvalidator.addValidation("email","maxlen=50");
frmvalidator.addValidation("email","req");
frmvalidator.addValidation("email","email");
frmvalidator.addValidation("tel","maxlen=15");
frmvalidator.addValidation("tel","numeric");
</script>
<?php
}
function sendMail() {
if (!isset ($_POST['email'])) { //Oops, forgot your email addy!
die ("<p>Oops! You forgot to fill out the email address! Click on the back arrow to go back</p>");
}
else {
$fname = stripslashes($_POST['fname']);
$email = $_POST['email'];
$phone = $_POST['tel'];
$comments = stripslashes($_POST['comments']);
$headers .= "\n";
//Uniqid session
$strSid = md5(uniqid(time()));
//Let's start our headers
$headers = "From: $fname<" . $_POST['email'] . ">\n";
$headers .= "Reply-To: <" . $_POST['email'] . ">\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "--".$strSid."\n";
$headers .= "Content-type: text/html; charset=utf-8\n";
$headers .= "Content-Transfer-Encoding: 7bit\n\n";
/* Add our message, in this case it's plain text. You could also add HTML by changing the Content-Type to text/html */
$headers .= "<table>";
$headers .= "<tr><td>CONTACT FORM</td><td> </td></tr>";
$headers .= "<tr><td>Check for attachments</td><td> </td> </tr>";
$headers .= "<tr>
<td> </td>
<td > </td>
</tr>";
$headers .= "<tr>
<td >Name: " . strip_tags($_POST["fname"]) . "</td><td > </td>
</tr>";
$headers .= "<tr>
<td>Email: " . strip_tags($_POST["email"]) . "</td><td > </td>
</tr>";
$headers .= "<tr>
<td>Phone: " . strip_tags($_POST["phone"]) . "</td><td> </td>
</tr>";
$headers .= "<tr>
<td> </td>
<td > </td>
</tr>";
$headers .= "<tr>
<td>MESSAGE</td><td> </td></tr>";
$headers .= "<tr> <td COLSPAN = '2'>" . strip_tags($_POST["comments"]) . "</td>
</tr>";
$headers .= "<tr>
<td> </td>
<td > </td>
</tr>";
$headers .= "</table>";
$headers .= "</body></html>";
$headers .= "\n";
//**multi attach**//
for($i=0;$i<count($_FILES["attachment"]["name"]);$i++)
{
if($_FILES["attachment"]["name"][$i] != "")
{
$file_name = $_FILES["attachment"]["name"][$i];
$data = chunk_split(base64_encode(file_get_contents($_FILES["attachment"]["tmp_name"][$i])));
$headers .= "--".$strSid."\n";
$headers .= "Content-Type: application/octet-stream;\n\tname=\"" . $file_name . "\"\n";
$headers .= "Content-Transfer-Encoding: base64\n";
$headers .= "Content-Disposition: attachment;\n\tfilename=\"" . $file_name . "\"\n\n";
$headers .= $data."\n\n"; //The base64 encoded message
}
}
$headers .= "\n";
$headers .= "------=MIME_BOUNDRY_main_message--\n";
$subject .= "Contact Form";
// send the message
mail("darrenmillbca#gmail.com", $subject, $message, $headers);
print "Mail sent. Thank you!";
$to = $email;
$subject = "Contact Form";
$message = '
<html>
<body>
<p>Dear ';
$message .= $fname;
$message .= '</p>
<p>Thankyou for your message. </p>
<p>Our staff will get back to you shortly. </p>
<p>Kind Regards <br />
<br />
</p>
</body>
</html>
';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To:' . "\r\n";
$headers .= 'From: darrenmillbca#gmail.com' . "\r\n";
//$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
//$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
}
}
switch ($action) {
case "send":
sendMail();
showForm();
break;
default:
showForm();
}
?>
</html>
What you're asking for is quite a significant change here, so I'll try my best to make it simple for you guys. Your excel content form makes use of an email wrapper (mailer.php) which converts attaching files to an email to just $myEmail->addFile("file name","mimetype","file content"). The upload form on the other hand is not using this wrapper and is instead generating a whole email seperately. So you'll want to focus on moving your uploads to utilising the wrapper; that's moving content from the uploads form to the excel form.
So, our focus is with the following block from the uploads file:
for($i=0;$i<count($_FILES["attachment"]["name"]);$i++)
{
if($_FILES["attachment"]["name"][$i] != "")
{
$file_name = $_FILES["attachment"]["name"][$i];
$data = chunk_split(base64_encode(file_get_contents($_FILES["attachment"]["tmp_name"][$i])));
$headers .= "--".$strSid."\n";
$headers .= "Content-Type: application/octet-stream;\n\tname=\"" . $file_name . "\"\n";
$headers .= "Content-Transfer-Encoding: base64\n";
$headers .= "Content-Disposition: attachment;\n\tfilename=\"" . $file_name . "\"\n\n";
$headers .= $data."\n\n"; //The base64 encoded message
}
Which is saying 'for each file upload, write it into the email we're building up'. Instead of this, we want to do 'for each file upload, attach it using the wrapper'. That would be done like so:
for($i=0;$i<count($_FILES["attachment"]["name"]);$i++){
if($_FILES["attachment"]["name"][$i] != "")
{
$file_name = $_FILES["attachment"]["name"][$i];
$data = file_get_contents($_FILES["attachment"]["tmp_name"][$i]);
$myEmail->addFile($file_name,"application/octet-stream",$data);
}
}
For this to work, the above code would go in your excel form right after the existing $myEmail->addFile call. In other words, right after you attach your excel sheet, look for any attachments, then add those to the email too:
$myEmail->addFile("{$company}-{$taxyear}.xls", "application/vnd.ms-excel", $FILE_CONTENTS);
// Drop it right here.
$myEmail->send();
So far so good - at this point, your excel form can handle multiple uploads, but it can't create them. To fix that, you'll then want to start transferring the html that allows your users to upload files over to your excel form. That's this part:
<tr>
<td width="120" valign="middle" class="indextextlight">Upload Files:</td>
<td colspan="2"><p>
<input name="attachment[]" type="file" multiple="" class="indextextlight" size="42">
<br />
<span class="centgothicmini"> note - hold the Ctrl key to select multiple files</span><br />
<span class="centgothicmini"> note - max total size of all files can not exceed 10mb</span></td>
</tr>
<tr>
A simple thing you could do is dump that wherever it fits on your excel form and as a result your excel form should then accept multiple uploads. From there, all that is required is transferring any other fields you want from upload to excel :)

Categories