$_POST a input value in two parts (explode?) - php

I'm really new to PHP, so this is probably a pretty dumb question.
I'm using PHP to submit an email form, and would like the email to contain the values of some of the form's inputs. Here's a stripped down version:
<?php
if(isset($_POST['submit'])) {
$to = 'address#gmail.com' ;
$subject = 'Subject';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message =
//here are the values that the email will send me
"<p>".$_POST('some-name')."</p>
<p>".$_POST('some-name')."</p>" ;
mail($to, $subject, $message, $headers);
header('Location: ../estimate.html');
} ?>
The value of input some-name is, say, 10-widgets posted from the form.
Here's the question: instead of listing "10-widgets" twice (as the above code will do), how do I list the first part in the first <p> (so it would be "10") and the second part in the second <p> (so it would be "widgets")?
Something like the following seems promising:
$wholeVal = $_POST('some-name');
$partVal = explode("-",$wholeVal);
and then, somewhere, $_POST($partVal[0]); and $_POST($partVal[1]);
But I don't know where this should take place, and anywhere I put it seems to make the whole thing break.
Thanks for your help.

First, you should access $_POST with brackets, like $_POST['some-name']. explode returns an array. So in your example, explode('-', '10-widgets')[0] will return '10' and explode('-', '10-widgets')[1] will return 'widgets'
So your code will be something like this:
<?php
if(isset($_POST['submit'])) {
$to = 'address#gmail.com' ;
$subject = 'Subject';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$parts = explode('-', $_POST['some-name']);
//here are the values that the email will send me
$message =
"<p>".$parts[0]."</p>
<p>".$parts[1]."</p>";
mail($to, $subject, $message, $headers);
header('Location: ../estimate.html');
}
?>

I know you're looking for functionality at the moment but keep in mind that if you're splitting a string by a "-" it'd be really easy for a script kiddie (or even a well intentioned person for that matter) to use a hyphenated word and your script will break.
Just a heads up :D

Try that
$wholeVal = $_POST('some-name');
$list($ammount, $type) = explode("-",$wholeVal);
Then into your email:
$message = "<p>" . $ammount . "</p><p>" . $type . "</p>" ;

<?php
if(isset($_POST['submit'])) {
$to = 'address#gmail.com' ;
$subject = 'Subject';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message =
$wholeVal = $_POST('some-name');
$partVal = explode("-",$wholeVal);
//here are the values that the email will send me
"<p>".$partVal[0]."</p>
<p>".$partVal[1]."</p>" ;
mail($to, $subject, $message, $headers);
header('Location: ../estimate.html');
} ?>

Related

php mail function is not working even its showing true condition part

my php mail() function is not working even its right here it is
$to = $email_id;
$sub = "FORGOT PASSWORD";
$msg = $password;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'From: Your name <example#gmail.com>' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$mail= mail($to,$sub,$msg,$headers);
if($mail)
{
?>
<script>alert('Your password has been successfully sent to you');</script>
<?
}
else
{
?>
<script>alert('Please try again later');</script>
<?
}
its alert if part i.e. Your password has been successfully sent to you but i did not received any email please help me and i am not using any HTML css in sending this mail.
your headers are incorrect use your headers in this way it will resolved your problem getting true part is not a big deal
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'From: Your name <example#you_domain_name.com>' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
if not resolved give me the result you get using var_dump($mail); showing bool(true)
use
$mail= mail($to, $sub, $msg, $headers);
You dont need string as all variables are Already string.
Use this mail($to, $sub, $msg, $headers),
If it returns true, then check your spam folder.
Write your domain name
$headers .= 'From: Your name <example#yorudomainname>' . "\r\n";
example <example#stackoverflow.com/>
no need to use "" in the mail function if you are storing it in a variable allready.
second thing check what you are getting in $email_id; variable is it the correct email address you are trying to send email .
simply use like this
<?php
$to = $email_id;
$sub = "FORGOT PASSWORD";
$msg = $password;
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: somebodyelse#example.com";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$mail=mail($to,$sub,$msg ,$headers);
if($mail)
{
?>
<script>alert('Your password has been successfully sent to you');</script>
<?
}
else
{
?>
<script>alert('Please try again later');</script>
<?
}
?>
Update
also please check your spam folder. as if you are using other domain as from email these emails may go to the spam folder . or you can use these headers
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: somebodyelse#yourdimain.com"; // enter your domain email
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

send html in an email via php, can't seem to do it

I have a list of subscribers who I am trying to send a HTML type email to (essentially a news letter). But currently, it is just sending plainly and I can't seem to figure how to make it look better.
I've searched through other questions but can't seem to apply the answers to my own code, so could someone help? Code is shown below:
<?php
$user = "example";
$password = "example";
$host = "example";
$dbase = "example";
$table = "example";
$from= 'example';//specify here the address that you want email to be sent from
$subject= $_POST['subject'];
$body= $_POST['body'];
// Connection to DBase
$dbc= mysqli_connect($host,$user,$password, $dbase)
or die("Unable to select database");
$query= "SELECT * FROM $table";
$result= mysqli_query ($dbc, $query)
or die ('Error querying database.');
while ($row = mysqli_fetch_array($result)) {
$firstname= $row['firstname'];
$lastname= $row['lastname'];
$email= $row['email'];
$msg= "Dear $firstname $lastname,\n$body";
mail($email, $subject, $msg, 'From:' . $from);
echo 'Email sent to: ' . $email. '<br>';
}
mysqli_close($dbc);
?>
So I have a message box where I can type a message but how would I make that message box, html style? So I can add in H2 etc tags? Or just make the email like a html newsletter
I have put example in on purpose
You need to send headers with html declared: example
$to = 'person#example.com';
$subject = 'Your subject';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: you#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = // your html code here
mail($to, $subject, $message, $headers);
For that you need to set your headers as text/html
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
and pass that to your mail() function.
Something like this..
mail($to, $subject, $msg, $headers);
Your Modified Code
$msg= "<h3>Dear $firstname $lastname</h3>,<br><br><b>$body</b>"; //<-- Added some basic formatting
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Yourname <'.$from.'>' . "\r\n";
mail($to, $subject, $msg, $headers);
Refer : PHP Manual
Use PHP Mailer Class instead of mail()
$mail = new PHPMailer();// defaults to using php "mail()"
$mail->SetFrom("frommail");
$mailAdmin->AddAddress("tomail");
$mailAdmin->Subject = $subject;
$mailAdmin->MsgHTML($message);
$mailAdmin->IsHTML(true);
$mailAdmin->Send();
You just need to include a class file phpmailer class file wich can be downloaded online.

How to include a call to a file in PHP mail() function

I have the following function for sending an email:
function send_email($email){
$subject = "TITLE";
$message = "HERE IS THE MESSAGE";
// 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: <emaily>' . "\r\n";
mail($email,$subject,$message,$headers);
}
Instead of $message being a string, I want to call in the file email.html which holds my template.
I have added this:
require 'email.html';
But how can I call in the file?
$message = [call in email.html here]
Require is used when you want to call functions within another php file, or when you want to include some data to an HTTP response.
For this problem, file_get_contents('email.html') is the preferred option. This would be the method I would use:
function send_email($email){
$subject = "Subject of your email";
$message = "";
if(file_exists("email_template.html")){
$message = file_get_contents('email_template.html');
$parts_to_mod = array("part1", "part2");
$replace_with = array($value1, $value2);
for($i=0; $i<count($parts_to_mod); $i++){
$message = str_replace($parts_to_mod[$i], $replace_with[$i], $message);
}
}else{
$message = "Some Default Message";
/* this likely won't ever be called, but it's good to have error handling */
}
// 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: <doNotReply#myDomain.com>' . "\r\n";
$headers .= "To: <$email>\r\n";
$header .= "Reply-To: doNotReply#myDomain.com\r\n";
mail($email,$subject,$message,$headers);
}
I modified your code a little bit and added in both the file_get_contents and file_exists. file_exists confirms that the file is there. If it's not, it avoids the potential error from trying to read it in and can be changed to use some default. My next addition was a for loop. In the $parts_to_mod array, enter in the default values from the template that need to be replaced. In the $replace_with array, put in the unique values that you want to replace parts of the template with.
As an example where I use this, I have a template URL for one of my programs that says id=IDENTIFIER&hash=THEHASH so in my program, my parts_to_mod says $parts_to_mod = array("IDENTIFIER", "THEHASH"); and my replace_with says $replace_with = array($theUsersIdentifier, $theUsersHash);. It then enters the for-loop and replaces the those values in parts_to_modify with the values in replace_with.
Simple concepts and they make your code much shorter and easier to maintain.
Edit:
Here is some sample code:
Let's the say the template is:
<span>Dear PUTNAMEHERE,</span><br>
<div>PUTMESSAGEHERE</div>
<div>Sincerely,<br>PUTSENDERHERE</div>
So, in your php code you'd say:
$parts_to_mod = array("PUTNAMEHERE", "PUTMESSAGEHERE", "PUTSENDERHERE");
$replace_with = array($name, $theBodyOfYourEmail, $whoYouAre);
just use file_get_contents('email.html') This method returns a string with the file contents
You can use this function to call custom email template.
function email($fields = array(),$name_file, $from, $to) {
if(!empty($name_file)) {
$mail_tem_path = 'templates/mails/mail--'.$name_file.'.php'; //change path of files and type file.
if(file_exists($mail_tem_path)) {
$headers = "MIME-Version: 1.0". "\r\n";
$headers .= "Content-Type: text/html;charset=UTF-8". "\r\n";
// Additional headers
$headers .= "From:".$fields["subject_from"]." <$from>". "\r\n";
$headers .= "Content-Transfer-Encoding: 8Bit". "\r\n";
$message = file_get_contents($mail_tem_path);
$message = preg_replace("/\"/", "'", $message);
foreach ($fields as $field) {
// Replace the % with the actual information
$message = str_replace('%'.$field["name"].'%', $field["value"], $message);
}
$send = mail($to, $fields["subject"], $message, $headers);
} else {
$send = mail($to, "Error read mail template", "Contact with admin to repair this action.");
}
} else {
$send = mail($to, "Error no exist mail template", "Contact with admin to repair this action.");
}
return $send;
}
Template html
<html>
<body>
TODO write content %value_on_array%
</body>
</html>
Array and execute function.
$fields = array(
"subject" => "tienda_seller_subject",
"subject_from" => "tienda_email_subject_from",
0 => array(
"name" => "value_on_array",
"value" => "result before change"
),
);
//email($fields = array(),$name_file, $from, $to);
email($fields, 'admin', 'owner#email.com', 'client#email.com');
Result

Sending a Confirmation Email in PHP

I am quite new to PHP and am experimenting with an online ordering website in php. I have come across a problem when trying to send a confirmation email using a 'cc'.
Every time an order is processed, the order always sends to the 'CC' address specified as the '$from' and the sender (the from) is what is specified in the '$to' section. In the email confirmation received, it only displays the from section and the 'to' and 'cc' are empty as demonstrated below:
From: **$_SESSION['od_email']**
To: *This space is empty*
CC: orders#business.co.uk
Can anyone help to point out where I am going wrong? I have attached the code below.
//THIS IS CODE FOR THE EMAIL WHICH IS AUTOMATICALLY SENT TO THE CUSTOMER AND THE BUSINESS WHEN AN ORDER IS SUCCESSFULLY COMPLETED
//define the receiver of the email
$to = $_SESSION['od_email'];
//define the subject of the email
$subject = 'Order Confirmation | Ref No for Order: '. $_SESSION['orderId'];
//define the message to be sent. Each line should be separated with \n
$message = 'test';
//Who the message is from
$from = "orders#business.co.uk";
$cc = "orders#business.co.uk";
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From:" . $from;
//bcc header going to the to go to the business as confirmation
$headers = "cc:" . $cc;
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
What I need for it to show is:
From: **orders#business.co.uk**
To: **$_SESSION['od_email'];**
CC: **orders#business.co.uk**
Thanks in advance for any help given
When you're going to set the CC, you're overwriting what was in $headers. Try:
$headers = "From:" . $from . "\r\n";
$headers.= "Cc:" . $cc;
And if that doesn't help, there's always PHP's documentation on mail.
I would go all out with your headers and make sure you set everything you need to,
// 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";
// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
$headers .= 'Cc: '.$cc. "\r\n";
also, I believe mail() needs a valid email address, I do not know how you have the email set before $_SESSION['od_email'] but if you are running a test and literally expecting $_SESSION['od_email'] to come with it that wont work, it will see it as a not valid email and return false, thus returning nothing. To further diagnose that problem tho we would need to know how $_SESSION['od_email'] is setup
EDIT
Okay I am pretty sure the issue is caused how you are setting up $_SESSION['od_email]' based on your comment below.
Note: I am assuming your database is $db, but if not you need to change $db to the prober name
Let's try this and see how it works
// Get Data from the Database
$query = "SELECT `od_email` FROM `tbl_order`";
$result = $db->query($query);
while ($data = $db->fetch_array($result)) {
// Set Up the SESSION email
$_SESSION['od_email'] = $data['od_email'];
// Prepare the Email
$to = $_SESSION['od_email'];
$subject = 'Order Confirmation | Ref No for Order: '. $_SESSION['orderId'];
$message = 'test';
$from = "orders#business.co.uk";
$cc = "orders#business.co.uk";
// 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";
// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
$headers .= 'Cc: '.$cc. "\r\n";
// Send the email
mail($to,$subject,$message,$headers);
}
Couldn't he just add an #mail ? such as...
$mail_sent = #mail( $od_email, $subject, $message, $headers );
or
mail( $od_email, $subject, $message, $headers );
Replacing any variables.
Always worked for me.

send mail by bbc not working

hay
i used this code
$to = "mial#live.com,mail#yahoo.com";
$subject = "Mini-mass Emailer";
$message = "<a href='#'>Hello World</a>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Your Name <me#mydomain.com>' . "\r\n";
$headers .= 'Bcc: {$to}' . "\r\n";
if(mail($to, $subject, $message, $headers)){
echo 'ok';
}
but see what is happend
every user see the full list of the users
alt text http://img694.imageshack.us/img694/1289/21811933.gif
Your call to mail is passing the $to as the to parameter meaning those emails will be be in the to header try passing an empty string instead. You are passing the info into the bcc header so the email should still get to them that way.
That is because you have put all the users in the "to" line. You are also passing them into the "bcc" line too so just doing this may help you but as far as I know you need at least one address in the to line (although this may not be the case). It'll look pretty strange for each person doing it that way though.
The best way to avoid these issues would be to send the email multiple times, once to each user. To modify your code example to do this, I'd do something like the following:
$toAddresses = array("mial#live.com", "mail#yahoo.com");
$subject = "Mini-mass Emailer";
$message = "<a href='#'>Hello World</a>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Your Name <me#mydomain.com>' . "\r\n";
foreach ($toAddresses as $to) {
if(mail($to, $subject, $message, $headers)){
echo "OK - sent message to {$to}";
}
}
The easiest way is to take this Mail-Class of phpguru.org:
http://www.phpguru.org/static/htmlMimeMail5
There you can specify with setBcc() the addresses which should be "blind", it's pretty easy and works well. I use this class in every project.
Best Regards.

Categories