Send html table in email body in PHP - php

im trying to send mail in php,so that mail receiver receives mail with html table, im receiving mail with table but data is missing in mail.. im recieving variable instead of data from html form.
Name: $name
Email: $email
Phone: $phone
Services: $services
Address: $address
Subject: $subject
Message: $message
The code i write is below
<?php
$name = $email = $address = $phone = $service = $subject = $message ="";
$contactErr = "";
$contactsuccess = "";
if (isset($_POST['submit'])) {
$fm_name = $_POST['name'];
$fm_email = $_POST['email'];
$fm_address = $_POST['address'];
$fm_phone = $_POST['phone'];
$fm_service = $_POST['service'];
$fm_subject = $_POST['subject'];
$fm_message = $_POST['message'];
$name = contact_input($fm_name);
$email = contact_input($fm_email);
$address = contact_input($fm_address);
$phone = contact_input($fm_phone);
$service = contact_input($fm_service);
$subject = contact_input($fm_subject);
$message = contact_input($fm_message);
if (empty($fm_name)) {
$contactErr = "Name is Required.";
}
elseif (empty($fm_email)) {
$contactErr = "Email. is Required.";
}
elseif (!filter_var($fm_email, FILTER_VALIDATE_EMAIL)) {
$contactErr = "Invalid email format";
}
elseif (empty($fm_address)) {
$contactErr = "Enter you address.";
}
elseif (empty($fm_phone)) {
$contactErr = "Phone No. is Required.";
}
elseif (empty($fm_service)) {
$contactErr = "Select your desired service !";
}
elseif (empty($fm_subject)) {
$contactErr = "Subject is Required.";
}
elseif (empty($fm_message)) {
$contactErr = "Enter your message!";
}
else{
$info = '<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<table border="1" cellspacing="3" width="60%">
<tr>
<td>Name:</td>
<td>$name</td>
</tr>
<tr>
<td>Email:</td>
<td>$email</td>
</tr>
<tr>
<td>Address:</td>
<td>$address</td>
</tr>
<tr>
<td>Phone:</td>
<td>$phone</td>
</tr>
<tr>
<td>Subject:</td>
<td>$subject</td>
</tr>
<tr>
<td>Services:</td>
<td>$service</td>
</tr>
<tr>
<td>Message:</td>
<td>$message</td>
</tr>
</table>
</body>
</html>';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$mailto = "satish.innovstudio#gmail.com";
$sub = "Get In Touch With Us";
mail($mailto,$sub,$info,$headers);
$contactsuccess = "Your message has been sent successfully! We will contact you shortly.";
$name = $email = $address = $phone = $service = $subject = $message ="";
}
}
function contact_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

You have all of your variables just written out in the string. You're going to need to concatenate them.
instead of
<td>$message</td>
it should be
<td>.$message.</td>

Just change the ' to " at the start and end of $info variable string declaration (you then need to escape every " character in your string or change it to ') OR change your variables to, for example, '. $name .'.
In single quotes variables are not interpreted as they are in double quoted strings.
Read answer to this question about the difference between single and double quotes in PHP strings to get what you are doing wrong:
https://stackoverflow.com/a/3446286/15182618

Related

Warning: implode(): Invalid arguments passed in /var/www/xxxxx.com/contact.php on line 35

I am getting this warning when the form is submitted without entering any of the fields. The error seems to me from line 35 which uses the implode(). Any help will be really appreciated. Thank you so much for looking.
Thank you so much for looking.
Thank you so much for looking.
Thank you so much for looking.
<?php
$your_email ='email#gmail.com';// <<=== update to your email address
session_start();
$errors = '';
$name = '';
$lastname = '';
$phone = '';
$address = '';
$zipcode = '';
$city = '';
$state = '';
$interested_in = array();
$interested_in = '';
$visitor_email = '';
$user_message = '';
$ansday = '';
$hidansday='';
$qnday=rand(1,7);
$hidansday=$qnday+1;
if($hidansday==8) {$hidansday=1;}
$whatday=jddayofweek ( cal_to_jd(CAL_GREGORIAN, 1,$qnday, 2012) , 1 );
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$zipcode = $_POST['zipcode'];
$city = $_POST['city'];
$state = $_POST['state'];
$interested_in = implode(",", $_POST['interested_in']);
$visitor_email = $_POST['visitor_email'];
$user_message = $_POST['message'];
$hiddenansday= $_POST['hidansday'];
$ansday = $_POST['ansday'];
$dw1=trim(strtolower($ansday));
$dw2=strtolower(trim(jddayofweek (cal_to_jd(CAL_GREGORIAN, 1,$hiddenansday, 2012) , 1 )));
///------------Do Validations-------------
$email = htmlspecialchars($_POST['visitor_email']);
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/",$visitor_email))
{
$errors .= "\n E-mail address not valid ";
}
if ($dw1!==$dw2)
{
$errors .= "\n Please enter valid day for the Security Question";
}
if(empty($name))
{
$errors .= "\n First Name is a required field. ";
}
if(empty($address))
{
$errors .= "\n Please enter your address. ";
}
if(empty($zipcode))
{
$errors .= "\n Please enter your ZIP Code. ";
}
if(empty($city))
{
$errors .= "\n Please enter your City. ";
}
if(empty($state))
{
$errors .= "\n Please enter your State. ";
}
if( !preg_match("/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $phone) ) {
$errors .= "\n Please enter a valid phone number ";
}
if(empty($errors))
{
//send the email
$to = $your_email;
$subject="Web Form";
$from = $visitor_email;
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$body = "A user $name submitted the contact form:\n".
"First Name: $name\n".
"Last Name: $lastname \n".
"Phone: $phone \n".
"Email: $visitor_email \n".
"Address: $address \n".
"Zip Code: $zipcode \n".
"City: $city \n".
"State: $state \n".
"Interested In (Check all that apply): $interested_in \n".
"Message: \n ".
"$user_message\n".
"IP: $ip\n";
$headers = "From: $from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to, $subject, $body,$headers);
header('Location: thankyou.php');
}
}
?>
This is the HTML part.
<p>
Service Interested In <small>(Check all that apply):</small> <br/>
<input name="interested_in[]" type="checkbox" id="interested_in1" value="Internet"/> Internet<br>
<input name="interested_in[]" type="checkbox" id="interested_in2" value="Telephone" /> Telephone<br>
<input name="interested_in[]" type="checkbox" id="interested_in3" value="Key System" /> Key Systems<br>
</p>
If you tick at least one of the checkboxes and submit the form , there will be no error/warning because $_POST['interested_in'] will not be empty.
However, if you do not tick any of the checkboxes, then system will prompt warning because now $_POST['interested_in'] is empty but you try to use implode() on it.
Hence, you need to check whether $_POST['interested_in'] has value(s) or not first, say by using isset
So, change the line
$interested_in = implode(",",$_POST['interested_in']);
to
if (isset($_POST['interested_in'])){
$interested_in = implode(",",$_POST['interested_in']);
}

Sending an email to 2 variables in PHP

Im trying to send an email to 2 variables currently I have tried to add 3 emails to an array and put that into a variable and get the other email from the form, I then dont know how to put both of those variables together when sending so they each have their own "to" but this does not work or something doesnt and I dont know what?? And yes I have real emails that I use these are placeholders for this!!!
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['submit'])) {
$email = $_POST['LOGSE'];
$email2 = array("test#test.com", "test#test.com", "test#test.com");
$name = $_POST['full_name'];
$eventtitle = $_POST['EventT'];
$InCharge = $_POST['InCharge'];
$Venue = $_POST['Venue'];
$VenY = $_POST['VenR'];
if($VenY != "Yes"){
$ava = " Have not checked if";
}
else{
$ava = "";
}
$dates = $_POST['dateS'];
$datee = $_POST['dateE'];
$adults = $_POST['Adults'];
$children = $_POST['Children'];
$catreq = $_POST['CateReq'];
if (catreq != ''){
$catreq = $catreq;
}
else{
$catreq = "No Catering Needed";
}
$logreq = $_POST['LogReq'];
if (logreq != ''){
$logreq = $logreq;
}
else{
$logreq = "No Logistic Equipment Needed";
}
$itreq = $_POST['ITReq'];
if (itreq != ''){
$itreq = $itreq;
}
else{
$itreq = "No IT Needed";
}
$tran = $_POST['TransR'];
if($tran != Yes){
$tran = "NO ";
}
else{
$tran = "";
}
$Risk = $_POST['RiskR'];
if($Risk != Yes){
$Risk = "NO ";
}
else{
$Risk = "";
}
$othern = $_POST['OtherN'];
// The Email:
$from = 'test#test.com';
$to = $email;
$to = $email2;
$subject = 'Event Form ' .$eventtitle;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type:text/html;charset=UTF-8' . "\r\n";
$headers .= 'From: Event Form<test#test.com>' . "\r\n";
$headers .= 'Cc: test#test.com, test#test.com' . "\r\n";
$body = '<html>
<head>
<title>Event</title>
<style>
h1 {
font-family: "Century Gothic", CenturyGothic,
AppleGothic, sans-serif;
}
h3 {
font-family: "Century Gothic", CenturyGothic,
AppleGothic, sans-serif;
}
</style>
</head>
<body>
<h1>Event Details</h1>
<h3>Name: '.$name.'</h3>
<h3>Event Title: '.$eventtitle.'</h3>
<h3>Event Manager: '.$InCharge.'</h3>
<h3>Venue: '.$Venue.' - '.$ava.' Available</h3>
<h3>Date Start: '.$dates.'</h3>
<h3>Date End: '.$datee.'</h3>
<h3>Adults Attending: '.$adults.' Children Attending:
'.$children.'</h3>
<h3>Catering Requirements: '.$catreq.'</h3>
<h3>Logistic Requirements/Equipment: '.$logreq.'</h3>
<h3>IT Requirements: '.$itreq.'</h3>
<h3>Other Notes: '.$othern.'</h3>
<h3><font color="red">'.$tran.'</font>Transport Has Been
Booked</h3>
</body>
</html>' . "\r\n";
mail( $to, $subject, $body ,$headers );
/* echo "An Email Has Been Sent<br>Thank You."; */
header('Location: ./thanks.html');
}
}
The $to in mail function should be a string. If there are more than one email address then those should be comma separated. In your case you might need to do like following.
$to = $email . ','. implode(',', $email2);
Add email address with the comma
$email_to = "test#test.com,some#other.com,yet#another.net";
Also you can add
$headers = "Bcc: someone#domain.com";
Use a foreach on the array:
foreach($email2 as $to){
mail( $to, $subject, $body ,$headers );
}
Or like others are suggesting:
$to = $email.",".implode(",",$email2);
That doesn't make sense:
$to = $email;
$to = $email2;
Parmaeter $to should be a string of comma-seperated email-addresses, e.g.:
$to = $email1 . ", " . $email2 . ", " . $email3;

How to update in php using array after sending an email?

I have this link and then processed by this script:
<html>
<head>
</head>
<?php
/*//Disable error reporting
error_reporting(0);
*/
//Report runtime errors
//error_reporting(E_ERROR | E_WARNING | E_PARSE);
//<script>location.replace("http://www.mntr.xxx.com/update.php");</script>
//Report all errors
error_reporting(E_ALL);
//end of error reporting
if(isset($_POST['submit']) && $_POST['submit'] == 'Send') {
//var_dump($_POST['users']);
$size= sizeof($_POST['users']);
$i = 0;
$message = NULL;
// $message = null;
for($i=0; $i<$size; $i++){
$userId = $_REQUEST['users'][$i];
$message .= mailContent($userId);
}
$to = $_POST['email'];
$subject = 'This is a test';
$headers = 'From: xxx#live.com.ph' . "\r\n" .
'Reply-To: xxx#live.com.ph' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: text/html; charset=ISO-8859-1\r\n';
mail($to, $subject, $message, $headers);
}
function mailContent($userId) {
$con=mysqli_connect("localhost","xxx","xxx","xxx");
$stmt = "SELECT * from CV where idvisa = '$userId'";
$result = mysqli_query($con, $stmt);
$row=mysqli_fetch_array($result);
$fName = $row['fName'];
$lName = $row['lName'];
$visaNumber = $row['visanumber'];
$idNumber = $row['idnumber'];
$statusApp = $row['statusapp'];
$accntVisaPhotoPath = $row['accntVisaPhotoPath'];
$passportPath = $row['passportPath'];
$message = "Your Username: YOUR EMAIL (small caps) PASSWD: 1234
<table>
<tr>
<td>Name</td><td>$fName $lName</td>
</tr>
<tr>
<td>CVs</td><td>Click Here</td>
</tr>
<tr>
<td>Visa Number</td><td>$visaNumber</td>
</tr>
<tr>
<td>ID Number</td><td>$idNumber</td>
</tr>
<tr>
<td>Application Status</td><td>$statusApp</td>
</tr>
<tr>
<td>Visa Copy</td><td><a href='http://mntr.xxx.com/".$accntVisaPhotoPath."'>Click to Download</a></td>
</tr>
<tr>
<td>Passport Copy</td><td><a href='http://mntr.xxx.com/".$passportPath."'>Click to Download</a></td>
</tr>
</table>";
// OTHER LOGICS GO HERE
return $message;
}
//Additional var
//Time
date_default_timezone_set('Asia/Manila');
//End Time
$email = $_POST['email'];
$subdate = date("Y-m-d");
$subdate2 = date("Y-m-d");
if (empty($subdate)) {
$sql="UPDATE CV SET subdateEmail='$email',subdate='$subdate WHERE idvisa='$userId'";
} else {
$sql="UPDATE CV SET subdateEmail2='$email',subdate2='$subdate2' WHERE idvisa='$userId'";
}
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "<center>You have sent CV(s) </b><br/><br/><br/><br/>YOU MAY NOW CLOSE THIS WINDOW</center>";
?>
It is working but I want to update 4 columns out of the selected users (in this case, idvisa) AFTER email. Right now, it doesn't update. I am thinking on the array that has been SELECTED.
Any idea?
Use this
error_reporting(E_ALL);
$con=mysqli_connect("localhost","xxx","xxx","xxx");
if(#mail($to, $subject, $message, $headers))
{
ate_default_timezone_set('Asia/Manila');
//End Time
$email = $_POST['email'];
$subdate = date("Y-m-d");
$subdate2 = date("Y-m-d");
if (empty($subdate)) {
$sql="UPDATE CV SET subdateEmail='$email',subdate='$subdate WHERE idvisa='$userId'";
} else {
$sql="UPDATE CV SET subdateEmail2='$email',subdate2='$subdate2' WHERE idvisa='$userId'";
}
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "<center>You have sent CV(s) </b><br/><br/><br/><br/>YOU MAY NOW CLOSE THIS WINDOW</center>";
}else{
echo "Mail Not Sent";
}

Problems with my PHP Form

I can't get the value of the checkbox, the recipient(Administrator or Content Editor), to display it displays "Array" or "A". Also the page redirects automatically without the form first appearing. How do I rectify this?
Here is my code:
contact.php
<?php
$errnam = "";
$errmail = "";
$errsub = "";
$errrec = "";
$hasErrors = false;
if(isset ($_POST['submitted'])){
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$recipient = $_POST['recipient'];
$message = $_POST['message'];
if(preg_match("/^[\w\-'\s]/", $_POST['name'])){
$name = $_POST['name'];
}
else{
$errnam ='<strong>Please enter a name.</strong>';
$hasErrors = true;
}
if (preg_match("/^[\w.-_]+#[\w.-]+[A-Za-z]{2,6}$/i", $email)){
$email = $_POST['email'];
}
else{
$errmail = '<strong>Please enter a valid email.</strong>';
$hasErrors = true;
}
if(preg_match("/^[\w\-'\s]/", $_POST['subject'])){
$subject = $_POST['subject'];
}
else{
$errsub = "<strong>Please enter a subject.</strong>";
$hasErrors = true;
}
if (!empty($_POST['recipient'])) {
for ($i=0; $i < count($_POST['recipient']);$i++) {
$recipient = $_POST['recipient'];
}
}else{
$errrec = "<strong>Please select a recipient</strong>";
$hasErrors = true;
}
$message = $_POST['message'];
}
if ($hasErrors){
echo "<strong>Error! Please fix the errors as stated.</strong>";
}else{
header("Location: form.php?name=".$name."&email=".$email."&subject=".$subject. "&recipient=".$recipient. "&message=".$message);
exit();
}
?>
form.php
<?php
$name = $_GET['name'];
$email = $_GET['email'];
$subject = $_GET['subject'];
$recipient = $_GET['recipient'];
$message = $_GET['message'];
echo "<h2>Thank You</h2>";
echo "<p>Thank you for your submission. Here is a copy of the details that you have sent.</p>";
echo "<strong>Your Name:</strong> ".$name. "<br />";
echo "<strong>Your Email:</strong> ".$email. "<br />";
echo "<strong>Subject:</strong> ".$subject. "<br />";
echo "<strong>Recipient:</strong>" .$recipient. "<br />";
echo "<strong>Message:</strong> <br /> " .$message;
?>
you have the redirection outside the main if statement, just move it inside.
Also you are not accessing the right paramater in the array if you get "Array" back.
for ($i=0; $i < count($_POST['recipient']);$i++) {
$recipient = $_POST['recipient'][$i];
}
you will need to change your function

Sending mail php

Hey, no idea why this sint working, but the message is being sent as "0". I think the e-mail fIELD is what is causing it
<?php
if ($_POST['check'] == 'checked'){
header("location: /nospamplease.html");
exit();
}
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$date = $_POST['date'];
$children = $_POST['children'];
$hot = $_POST['hot'];
$comments = $_POST['comments'];
/*echo $name;
echo $email;
echo $phone;
echo $date;
echo $children;
echo $hot;
echo $comments;*/
if($name == "" || $email == "" || $phone == "" || $date == "" || $children == "" || $hot == "" || $comments == ""){
echo "Please ensure all fields were filled out!";
exit();
}else{
$to = "######";
$subject = "Birthday enquiry";
$message = "Name: ".$name;
$message += "Email: ".$email;
$message += "Phone: ".$phone;
$message += "Date: ".$date;
$message += "Children: ".$children;
$message += "Hot or cold: ".$hot;
$message += "Comments: ".$comments;
//echo $message;
if(mail($to, $subject, $message)){
echo "Thank you for your enquiry, we will contact you within the next 24 hours! <br /> Click <a href='###'> here</a> to go back to the website!";
}else{
echo "There was an error, contact us directly: <a href='mailto:##'>email</a>";
}
}
?>
I echoed out the variables, they all are getting posted fine
Thanks guys
You can't use + to concatenate strings in PHP. Use .
$message .= "Email: ".$email;

Categories