Email all values from an array - php

I am collecting information through a basic HTML form. Some of the fields are arrays indicated with [] after their name.
I have successfully written to a MySQL database and I am now trying to put the array values into a PHP-generated email. The email is being sent successfully with some of the variables printing, but none of the array values are coming through. How do I go about this?
$name, $position and $email are the values I am having issues with:
<?php
$to = "myemail#email.com";
$subject = "Media Request";
$message = "A media credential request has been submitted for $outlet. The editor is $editor and their contact info is $Eemail and $phone.";
$message .= '<html><body>';
$message .= "<h2>Credential Request</h2>";
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr><td align=right><strong>Name:</strong> </td>
<td>" . strip_tags("$name") . "</td>
<td align=right><strong>Position:</strong> </td>
<td>" . strip_tags("$position") . "</td>
<td align=right><strong>Email:</strong> </td>
<td>" . strip_tags("$email") . "</td></tr>";
$message .= "</table>";
$message .= ("Here are the comments and special instructions: $notes ");
$from = "email#email.com";
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to,$subject,$message,$headers);
?>

Assuming you have captured the form data in the variables you mentioned, you can use one of the following methods to output the array values:
Loop through the elements
foreach ($name as $name_field) {
$message .= "Your text here. $name_field. More Text";
}
Implode() and echo
You can convert the entire array into a string in one line using implode():
$message .= "Your text here." . implode(',', $name) . "More text here";
Note:
Verify that you have the expected data in $name, $position and $email. You can do this with var_dump():
var_dump($name, $position, $email);

Related

PHP form giving Errors

This is my PHP code which I'm using and Error is coming between the First Echo command in the code. Can anyone please Help?
<?php
if(isset($_POST["submit"])){
// Checking For Blank Fields..
if($_POST["name"]==""||$_POST["email"]==""||$_POST["phone_number"]==""||$_POST["city"]==""||$_POST["gre_score"]==""||$_POST["toefl_score"]==""||$_POST["eng_marks"]==""||$_POST["country"]==""||$_POST["course_type"]==""||$_POST["department"]==""||$_POST["exp"]==""||$_POST["ug_details"]==""||$_POST["pg_details"]){
echo "Something Went Please Try Again!";
}else{
// Check if the "Sender's Email" input field is filled out
$email=$_POST['email'];
// Sanitize E-mail Address
$email =filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
$email= filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email){
echo "Invalid Sender's Email";
}
else{
$name = '$name';
$subject = 'Registration Form';
$headers = 'From: '. $email . "\r\n"; // Sender's Email
$headers .= 'Cc:'. $email . "\r\n"; // Carbon copy to Sender
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// Message lines should not exceed 70 characters (PHP rule), so wrap it
$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['name']) . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
$message .= "<tr><td><strong>Phone Number:</strong> </td><td>" . strip_tags($_POST['phone_number']) . "</td></tr>";
$message .= "<tr><td><strong>City: </strong></td><td>" . strip_tags($_POST['city']) . "</td></tr>";
$message .= "<tr><td><strong>GRE Score:</strong> </td><td>" . strip_tags($_POST['gre_score']) . "</td></tr>";
$message .= "<tr><td><strong>TOEFL/IELTS Score:</strong> </td><td>" . strip_tags($_POST['toefl_score']) .
"";
$message .= "Engineering Marks: " . strip_tags($_POST['eng_marks']) .
"";
$message .= "Country Planning: " . strip_tags($_POST['country']) . "";
$message .= "Department: " . strip_tags($_POST['department']) . "";
$message .= "Job Experience: " . strip_tags($_POST['exp']) . "";
$message .= "Under Graduation Details: " . strip_tags($_POST['ug_details']) .
"";
$message .= "Post Graduation Details:" . strip_tags($_POST['pg_details']) .
"";
$message .= "Course Selected: " . strip_tags($_POST['course_type']) .
"";
$message .= "</body></html>";
// Send Mail By PHP Mail Function
mail("yash119ambaskar#gmail.com", $subject, $message, $headers);
echo "Your mail has been sent successfuly ! You will be contacted Shortly!";
}
}
}
?>
Hello Syntax error in your first if condition.Your post pg_details, you have not compare it with empty string.
if($_POST["name"]==""||$_POST["email"]==""||$_POST["phone_number"]==""||$_POST["city"]==""||$_POST["gre_score"]==""||$_POST["toefl_score"]==""||$_POST["eng_marks"]==""||$_POST["country"]==""||$_POST["course_type"]==""||$_POST["department"]==""||$_POST["exp"]==""||$_POST["ug_details"]==""||$_POST["pg_details"] == ""){

Send Mail when SQL value is True

So, I'm kind of in a rut for over a day now because of this issue. I have a PHP wherein it gets called by Cron on a set amount. It gets called and executes just fine without much problems except for the styling. What it does is query the database on a set time for a value and if it's true then it proceeds to email that to the designated email that I put in the script. It's doing it's job but the problem is the format when the email gets delivered. The email looks like this when I open it:
The issue, as you can see is that only the first line or row gets formatted on the table and the rest of the results doesn't follow it.
Here's the current script I'm using with some omitted/changed details.
#!/path/to/php
<?php
//DB
$dbconnect=new mysqli('localhost', 'user', 'pass', 'db');
$result=$dbconnect->query("SELECT `uid`,`pid`,`pdesc`,`umail`,`name` FROM `db_table` WHERE `pid`<='6'");
//if there are any records matching this query send an email listing each one using 'uid' as the identifier
if($result->num_rows>=1) {
$to = 'test#test.com';
$subject = "TEST FOR PRIZES NOTIFICATION";
$headers = 'From: sender#test.com' . "\r\n" .
$headers = "MIME-Version: 1.0" . "\r\n" .
$headers = "Content-type:text/html;charset=iso-8859-1" . "\r\n" .
'Reply-To: rep#test.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = '<html><body>';
$message .= 'THIS IS A TEST!';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= '<tr>
<td>User ID</td>
<td>Display Name</td>
<td>Email</td>
<td>Price</td>
<td>Price Description</td></tr>';
while($row=$result->fetch_assoc()) {
$message .= "<tr><td>".$row['uid']."</td>";
$message .= "<td>".$row['name']."</td>";
$message .= "<td>".$row['umail']."</td>";
$message .= "<td>".$row['pid']."</td>";
$message .= "<td>".$row['pdesc']."</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
}
if(mail($to, $subject, $message, $headers)) {
//mail successfully sent
} else {
//mail unsuccessful
}
}
?>
Thank you for your time!
You're closing the table inside of your loop, so it's closed after the first entry. Change your code from this:
while($row=$result->fetch_assoc()) {
$message .= "<tr><td>".$row['uid']."</td>";
$message .= "<td>".$row['name']."</td>";
$message .= "<td>".$row['umail']."</td>";
$message .= "<td>".$row['pid']."</td>";
$message .= "<td>".$row['pdesc']."</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
}
to this:
while($row=$result->fetch_assoc()) {
$message .= "<tr><td>".$row['uid']."</td>";
$message .= "<td>".$row['name']."</td>";
$message .= "<td>".$row['umail']."</td>";
$message .= "<td>".$row['pid']."</td>";
$message .= "<td>".$row['pdesc']."</td></tr>";
}
$message .= "</table>";
$message .= "</body></html>";
to close the table at the end.

execute mail once all item have been iterated

I have the following issues.
I receive separate emails for each iteration of the loop. I want mail to be only sent once with all of the items iterated.
<?php
// Honey pot trap
// Create a hidden input that is only visible to bots. If it's empty than proceed.
if (empty($_POST['humancheck'])){
// Proceeed if submit button have been pressed
$fullName = $_POST['fname'];
$email = $_POST['email'];
$stage = $_POST['stage'];
include("db.php");
$resources = "select * from resources where stage LIKE '%".$stage."%'";
$run_query = mysqli_query($con, $resources);
while($row = mysqli_fetch_array($run_query)) {
$data[] = array(
'format' => $row['format'],
'title' => $row['title'],
'costs' => $row['cost'],
'stage' => $row['stage'],
'topic' => $row['topic'],
'link' => $row['link']
);
}
foreach($data as $item) {
// Sanitize input data
$clean_fullName = htmlspecialchars($fullName);
$clean_email = htmlspecialchars($email);
// Mail Set up
$to = $clean_email;
$to_us = "info#email.com";
// Email subject
$subject = "Your custom resource pack has arrived!";
$subject_us = "New custom resource delivered to: $clean_email";
$message = '<html><body>';
$message .= "<p>";
$message .= "Hi $clean_fullName, <br><br>";
$message .= " Based on your responses, we have created a custom resource pack tailored to your needs. <br><br>";
$message .= "<b>{$item['title']}</b><br>";
$message .= "{$item['format']} <br>";
$message .= "{$item['costs']} <br>";
$message .= "{$item['link']} <br><br>";
$message .= " If you have any questions, do not hesitate to reach out to us. <br><br>";
$message .= "</p>";
$message .= '</body></html>';
$message_us = "The below message was sent to $clean_fullName <br>
<i> Hi $clean_fullName <br>";
$message_us .= "\r\n Based on your responses, we have created a custom resource pack tailored to your needs: \r\n";
$message_us .= "\r\n If you have any questions, do not hesitate to reach out to us. \r\n";
// Headers
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <info#email.com>' . "\r\n";
}
mail($to,$subject,$message,$headers);
mail($to_us,$subject_us,$message_us,$headers);
}
?>
what happens is the while loops to the data that is stored in an array. That array is used in foreach. and outside of the loops, the mail is suppose to mail the result.
In theory this should of worked but its not working.
Try this. Edit last part of your code in this way. Take one time parameters of mail content outside the loop, and let loop make only the message content.
$clean_fullName = htmlspecialchars($fullName);
$clean_email = htmlspecialchars($email);
$to = $clean_email;
$to_us = "info#email.com";
$subject = "Your custom resource pack has arrived!";
$subject_us = "New custom resource delivered to: $clean_email";
$message = '<html><body>';
$message .= "<p>";
$message .= "Hi $clean_fullName, <br><br>";
$message .= " Based on your responses, we have created a custom resource pack tailored to your needs. <br><br>";
foreach($data as $item) {
$message .= "<b>{$item['title']}</b><br>";
$message .= "{$item['format']} <br>";
$message .= "{$item['costs']} <br>";
$message .= "{$item['link']} <br><br>";
}
$message .= " If you have any questions, do not hesitate to reach out to us. <br><br>";
$message .= "</p>";
$message .= '</body></html>';
$message_us = "The below message was sent to $clean_fullName <br><i> Hi $clean_fullName <br>";
$message_us .= "\r\n Based on your responses, we have created a custom resource pack tailored to your needs: \r\n";
$message_us .= "\r\n If you have any questions, do not hesitate to reach out to us. \r\n";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <info#email.com>' . "\r\n";
mail($to,$subject,$message,$headers);
mail($to_us,$subject_us,$message_us,$headers);

Include Variables in PHP mail command

I'm sure I'm wrong about something basic, but can not find where the error is. I will appreciate your help.
I have a php page with a single 4 fields contact form.
I need send the name and email as variable, so i can see it properly when email is received.
IT WORKS - BUT DONT SEND NAME AND EMAIL AS VARIABLE:
All messages arrives as "no-reply#luchodomain.com" and doesnt look professional (and can't do reply from my outlook)
if ($_POST) {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "lucho#hotmail.com";
$subject = "Website CONTACT";
$headers = "From: no-reply#luchodomain.com" . "\r\n" ;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<html><body>";
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['name']) . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
$message .= "<tr><td><strong>Subject:</strong> </td><td>" . strip_tags($_POST['subject']) . "</td></tr>";
$message .= "<tr><td><strong>Message:</strong> </td><td>" . strip_tags($_POST['message']) . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
mail($to,$subject,$message,$headers);
}
IT DOENST WORKS (but have the format and variables as i need)
I would like receive the messages like is normal Name, so i can do a quick reply. -This code don't send email, doesn't work-
if ($_POST) {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "lucho#hotmail.com";
$subject = "Website CONTACT";
$headers = "From: " . strip_tags($_POST['email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['email']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<html><body>";
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['name']) . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
$message .= "<tr><td><strong>Subject:</strong> </td><td>" . strip_tags($_POST['subject']) . "</td></tr>";
$message .= "<tr><td><strong>Message:</strong> </td><td>" . strip_tags($_POST['message']) . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
mail($to,$subject,$message,$headers);
}
I will appreciate your help with this "headers" issue!
Thank you.
Make sure that you are receiving data in your post array. Make sure your form action. You have declared $message variable to blank after assigning the post value in it before. That's not a good trick enough. Please check updated code and let me know.
if ($_POST) {
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$subject = strip_tags($_POST['subject']);
$message = strip_tags($_POST['message']);
$to = "lucho#hotmail.com";
$subject = "Website CONTACT";
$headers = "From: " . strip_tags($_POST['email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['email']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
//$message = "";
//$message .= '';
$message .= "\r\nName: " . $name . "";
$message .= "Email: " . $email . "\r\n";
$message .= "Subject: " . $subject . "\r\n";
$message .= "Message: " . $message . "";
//$message .= "";
//$message .= "";
mail($to,$subject,$message,$headers);
}
$to = 'pawanvikasitha2001#gmail.com, aravindasamapath87#gmail.com'; $subject = 'NEW ORDER HAS PLACED'; $message = "$name"; mail ($to,$subject,$message);
When you use the double quotes, You can simply add variables to it. Look at the $message

Including HTML in PHP mail Function [duplicate]

This question already has an answer here:
HTML in php mail function
(1 answer)
Closed 8 years ago.
I have read a couple of posts on here about this already, so I know that I need to use the file_get_contents function to do this, but I have a bit more advanced need. I need to be able to call in data submitted by a user as well as call in the needed HTML to make the email look nicer. Here is some of my code:
First off, know the structure I have, inside index.php is my html form, and action="complete.php".
At the top of complete.php, I have this:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$evaluator_email = $_POST["evaluator_email"];
$teacher_email = $_POST["teacher_email"];
$evaluator = $_POST["evaluator"];
$teacher = $_POST["teacher"];
$date = $_POST["date"];
$teacher_body = "Hello " . $teacher . ", recently, " . $evaluator . " created a teacher evaluation for your session on " . $date . ". ";
$evaluator_body = "Hello " . $evaluator . ", your evaluation of " . $teacher . " was successfully sent to " . $teacher_email . ". ";
mail($teacher_email , "Evaluation" , $teacher_body , "From: $evaluator");
mail($evaluator_email , "Evaluation" , $evaluator_body , "From: Teacher Evaluation Tool");
}
?>
Which all works fine, but now I want to be able to include HTML. How can I make $teacher_body and $evaluator_body equal a file that contains the HTML that I write, and still have access to the PHP variables such as $teacher, $evaluator, $date, etc?
Example send an HTML email:
<?php
$to = "somebody#example.com, somebodyelse#example.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <webmaster#example.com>' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
mail($to,$subject,$message,$headers);
?>
I usually use placeholders in situations like this. For example in the HTML file I'll put things like "TEACHER" and "EVALUATOR" etc. Then my PHP can be something along the lines of:
$html = file_get_contents("myFileWithPlaceholders.htm");
$html = str_replace ( "TEACHER", $teacher, $html );
$html = str_replace ( "EVALUATOR", $evaluator, $html );
Just make sure your placeholders are very unique and you should be fine.
It is highly recommended to use PHPMailer.
https://phpbestpractices.org/#email
From this best practices website:
"PHP provides a mail() function that looks enticingly simple and easy. Unfortunately, like a lot of things in PHP, its simplicity is deceptive and using it at face value can lead to serious security problems.
Email is a set of protocols with an even more tortured history than PHP. Suffice it to say that there are so many gotchas in sending email that just being in the same room as PHP's mail() function should give you the shivers.
PHPMailer is a popular and well-aged open-source library that provides an easy interface for sending mail securely. It takes care of the gotchas for you so you can concentrate on more important things."
The library can be found here:
https://github.com/PHPMailer/PHPMailer
Easy to use and works great for sending HTML and text-based emails!
This is same as sending text in email. Just add the extra tags with a dot(.) operator.
For example:
$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['req-name']) . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['req-email']) . "</td></tr>";
$message .= "<tr><td><strong>Type of Change:</strong> </td><td>" . strip_tags($_POST['typeOfChange']) . "</td></tr>";
$message .= "<tr><td><strong>Urgency:</strong> </td><td>" . strip_tags($_POST['urgency']) . "</td></tr>";
$message .= "<tr><td><strong>URL To Change (main):</strong> </td><td>" . $_POST['URL-main'] . "</td></tr>";
$addURLS = $_POST['addURLS'];
if (($addURLS) != '') {
$message .= "<tr><td><strong>URL To Change (additional):</strong> </td><td>" .strip_tags($addURLS) . "</td></tr>";
}
$curText = htmlentities($_POST['curText']);
if (($curText) != '') {
$message .= "<tr><td><strong>CURRENT Content:</strong> </td><td>" . $curText . "</td></tr>";
}
$message .= "<tr><td><strong>NEW Content:</strong> </td><td>" .htmlentities($_POST['newText']) . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
// Always set headers
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <webmaster#example.com>' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
now send the email with:
mail($to, $subject, $message, $headers);

Categories