Don't know what to write in my message variable? - php

I am a PHP noob, so i don't know what to write in my message variable. I need my data from the form to be sent to the email. I got a textarea, 2 inputs (name, email). Want the text from input to be sent to my email. Here code i have:
<?
if((isset($_POST['name'])&&$_POST['name']!="") && (isset($_POST['phone'])&&$_POST['phone']!="")){
$to = 'rayetzkiillya#gmail.com';
$subject = 'Обратный звонок';
$message;
$headers = "Content-type: text/html; charset=utf-8 \r\n";
$headers .= "From: Отправитель <from#example.com>\r\n";
mail($to, $subject, $message, $headers);
}
?>
<form action="send.php" class="postcard" method="post">
<span>Your message:</span>
<textarea type="text" value="" required></textarea>
<div id="stripe1"></div>
<div id="stripe2"></div>
<img src="./images/Seal_of_the_United_States_Department_of_the_Post_Office.svg. png" alt="Oops" id="seal" />
<img src="./images/stamp.jpg" alt="Stamp" id="stamp" />
<div class="inputs">
<div class="inputs" id="input1"><label for="to" type="text" id="to">to: </label> <input type="text" value=" Me" readonly><div id="stripe3"></div></div>
<div class="inputs" id="input2"><label for="from" type="text" id="from">from: </label> <input type="text" id="input2"><div id="stripe3"></div></div>
<div class="inputs" id="input3"><label for="email" type="text" id="email">email: </label> <input type="text" id="input3"><div id="stripe3"></div></div>
<button type="button" class="btn btn-primary" id="send_button">Send</button>
</form>

You can make your message an HTML string, or just plain text.
For plain text, you can do something like
$message .= "name: ".$_POST['name']."\r\n";
$message .= "Message: ".$_POST["theMessage"];
However you need to name your inputs in the HTML as well,
so
<input type="text" id="input2" name="name">
<textarea type="text" value="" required name="theMessage"></textarea>
Or similar.
For the from address, you have to use change your header line
change
$headers .= "From: Отправитель <from#example.com>\r\n";
to
$headers .= "From: ".$_POST["name"]." <".$_POST["from"].">\r\n";
And obviously name your input accordingly:
<input type="text" id="input3" name="from">
Also, as suggested by others, you should be sanitizing/validating these values before using them.

Related

How to get the value of the form name that requested execution of php file

I have a form that requests execution of php file
Here is the form code:
<form name="First Form" method="post" action="email_script.php">
<input type="text" name="name" placeholder="Ваше Имя">
<input type="text" name="telephone" placeholder="Ваш Телефон">
<input type="submit" value="ОСТАВИТЬ ЗАЯВКУ">
</form>
Here is the php code:
<?php
// the message
$message .= $_POST["name"];
$message .= $_POST["telephone"];
// use wordwrap() if lines are longer than 70 characters
$message = wordwrap($message,70);
// send email
mail("nsaann#gmail.com","Заказ",$message);
?>
How can I add the form name to be a part of the message I email?
How can I make values of name, telephone, form name to be in differrent rows of the email body?
(Rigt now what I receive as email is this:
Name+1234567890
but I rather want it to be like this:
Name
+1234567890)
The reason I ask is because I want to create multiple html files with multiple forms that all requests execution of the same email_script.php and I want to know how each individual page performs in collecting orders. Thanks
So I edited the code and here it is:
<form name="First_Name" method="post" action="email_script.php">
<input type="hidden" name="First Name">
<input type="text" name="name" placeholder="Ваше Имя">
<input type="text" name="telephone" placeholder="Ваш Телефон">
<input type="submit" value="ОСТАВИТЬ ЗАЯВКУ">
</form>
php code:
<?php
if(isset($_POST["name"]) and isset($_POST["telephone"])){
$message = $_POST["name"]."<br>"; //use <br> and set the email headers to: Content-type: text/html
$message .= $_POST["telephone"]."<br>";
$message .= key($_POST); //form name
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail("nsaann#gmail.com", "Заказ", $message, $headers);
}
?>
And here is what I got as a result: http://postimg.org/image/4gj5gh22p/
Unfortunately I still got "name" instead of "First_Name"
Getting the name of a form is not possible as it's not sent as part of the POST data.
An alternative from this post suggests adding a hidden input element inside the form with the same name as the form:
<form name="First Form" method="post" action="email_script.php">
<input type="hidden" name="First Form">
<input type="text" name="name" placeholder="Ваше Имя">
<input type="text" name="telephone" placeholder="Ваш Телефон">
<input type="submit" value="ОСТАВИТЬ ЗАЯВКУ">
</form>
You can then use $_POST['First Form']; to get the name.
<form name="First Form" method="post" action="emailscript.php">
<input type="hidden" name="form" value="First Form">
<input type="text" name="name" placeholder="Ваше Имя">
<input type="text" name="telephone" placeholder="Ваш Телефон">
<input type="submit" value="ОСТАВИТЬ ЗАЯВКУ">
</form>
and use it using
$message .= $POST['form']
So finally I have the solution (My brother helped me with this).
here is my html code:
<form name="First_Name" method="post" action="email_script.php">
<input type="hidden" name="form_name" value="First Name">
<input type="text" name="name" placeholder="Ваше Имя">
<input type="text" name="telephone" placeholder="Ваш Телефон">
<input type="submit" value="ОСТАВИТЬ ЗАЯВКУ">
</form>
here is my php code:
<?php
if(isset($_POST["name"]) and isset($_POST["telephone"])){
$message = $_POST["name"]."<br>"; //use <br> and set the email headers to: Content-type: text/html
$message .= $_POST["telephone"]."<br>";
$message .= $_POST["form_name"]."<br>";
//$message .= key($_POST); //form name
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail("nsaann#gmail.com", "Заказ Парня", $message, $headers);
}
?>
It works via hidden input and the resulted email looks like this:
Nazar
+380953648591
First Name

HTML contact form, PHP code

I was trying to send multiple vars from my html form to my email by using PHP. The problem is that I do not know anything about PHP. I found a simple form which worked for me, but it contained only 3 vars, subject, email and message. This time however I have 7.
Here's the code.
<?php
$projectname = htmlentities($_POST['projectname']);
$projectemail = trim(strip_tags($_POST['projectemail']));
$projectphone = htmlentities($_POST['projectphone']);
$projectcompany = htmlentities($_POST['projectcompany']);
$projecttype = trim(strip_tags($_POST['projecttype']));
$projecttimeline = htmlentities($_POST['projecttimeline']);
$aboutproject = htmlentities($_POST['aboutproject']);
$message = "{$projectname}{$projectphone}{$projectcompany}{$projecttimeline}{$aboutproject}";
$subject = $projecttype;
$to = 'myemail#gmail.com';
$body = <<<HTML
$message
HTML;
$headers = "From: $projectemail\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $body, $headers);
header('Location: thanks.html');
?>
And the corresponding HTML
<form id="formproject" action="thank_you_project.php" method="post">
<label for="name">Name</label>
<input type="text" id="projectname" name="name">
<label for="email">Email</label>
<input type="text" id="projectemail" name="email">
<label for="phone">Phone</label>
<input type="text" id="projectphone" name="phone">
<label for="company">Company</label>
<input type="text" id="projectcompany" name="company">
<label for="typeofproject">Type of project</label>
<input type="text" id="projecttype" name="typeofproject">
<label for="timeline">Timeline</label>
<input type="text" id="projecttimeline" name="timeline">
<label for="message">Message</label>
<textarea name="message" id="aboutproject" cols="30" rows="10"></textarea>
<input type="submit" id="projectsend" value="Send"></input>
</form>
</div> <!-- end form -->
Edited the PHP, left out the body and put message in mail instead, still not working.
<?php
$projectname = htmlentities($_POST['projectname']);
$projectemail = trim(strip_tags($_POST['projectemail']));
$projectphone = htmlentities($_POST['projectphone']);
$projectcompany = htmlentities($_POST['projectcompany']);
$projecttype = trim(strip_tags($_POST['projecttype']));
$projecttimeline = htmlentities($_POST['projecttimeline']);
$aboutproject = htmlentities($_POST['aboutproject']);
$message = "{$projectname}{$projectphone}{$projectcompany}{$projecttimeline}{$aboutproject}";
$subject = $projecttype;
$to = 'albermy145#alberttomasiak.be';
$headers = "From: $projectemail\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $headers);
header('Location: thanks.html');
?>
Try this,
<?php
$to = 'myemail#gmail.com';
$projectname = htmlentities($_POST['projectname']);
$projectemail = trim(strip_tags($_POST['projectemail']));
$projectphone = htmlentities($_POST['projectphone']);
$projectcompany = htmlentities($_POST['projectcompany']);
$projecttype = trim(strip_tags($_POST['projecttype']));
$projecttimeline = htmlentities($_POST['projecttimeline']);
$aboutproject = htmlentities($_POST['aboutproject']);
$header = "From: $projectemail \r\n";
$subject = $projecttype;
$message = "
<html>
<head>
<title></title>
</head>
<body>
<p>Your HTML</p>
Project Name : $projectname
<br/>
Project Email : $projectemail
<br/>
Project Phone : $projectphone
...
...
...
...
</body>
</html>
";
$message .= "";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if($retval == true)
{
header('Location: thanks.html');
exit();
}
?>
}
HTML
<form id="formproject" action="thank_you_project.php" method="post">
<label for="name">Name</label>
<input type="text" id="projectname" name="projectname">
<label for="email">Email</label>
<input type="text" id="projectemail" name="projectemail">
<label for="phone">Phone</label>
<input type="text" id="projectphone" name="projectphone">
<label for="company">Company</label>
<input type="text" id="projectcompany" name="projectcompany">
<label for="typeofproject">Type of project</label>
<input type="text" id="projecttype" name="projecttype">
<label for="timeline">Timeline</label>
<input type="text" id="projecttimeline" name="projecttimeline">
<label for="message">Message</label>
<textarea name="aboutproject" id="aboutproject" cols="30" rows="10"></textarea>
<input type="submit" id="projectsend" value="Send"></input>
</form>
</div> <!-- end form -->
Add all variables into $body (which is the third param of mail function).
//$body = <<<HTML
//$message <br>
//$projectname <br>
//$projectcompany<br>
//...
//HTML;
Or the second way is to add these vars into the third mail param directly.
mail($to, $subject, $message, $headers);
// this code is updated as I wrote in my comment below.
the form posted the value of input is using 'name' attribute as identifier of value,
<form id="formproject" action="thank_you_project.php" method="post">
<label for="name">Name</label>
<input type="text" id="projectname" name="name">
<label for="email">Email</label>
<input type="text" id="projectemail" name="email">
<label for="phone">Phone</label>
<input type="text" id="projectphone" name="phone">
<label for="company">Company</label>
<input type="text" id="projectcompany" name="company">
<label for="typeofproject">Type of project</label>
<input type="text" id="projecttype" name="typeofproject">
<label for="timeline">Timeline</label>
<input type="text" id="projecttimeline" name="timeline">
<label for="message">Message</label>
<textarea name="message" id="aboutproject" cols="30" rows="10"></textarea>
<input type="submit" id="projectsend" value="Send"></input>
</form>
<!-- end form -->
<?php
$projectname = htmlentities($_POST['name']);
$projectemail = trim(strip_tags($_POST['email']));
$projectphone = htmlentities($_POST['phone']);
$projectcompany = htmlentities($_POST['company']);
$projecttype = trim(strip_tags($_POST['typeofproject']));
$projecttimeline = htmlentities($_POST['timeline']);
$aboutproject = htmlentities($_POST['message']);
?>

Send HTML Form data Via PHP

can anybody help me fix my php code so that it will send the form data to the email address? i am using MAMP Server to check wether it works the website works as planned however once i fill the form out and press submit it takes me to the PHP page however no email is sent.
PHP CODE
<?php
if(isset($_POST['submit'])){
$emailSubject = 'Customer Has a Question!';
$webMaster = 'r1bos#hotmail.com';
$nameField = $_POST ['Full_Name'];
$emailField = $_POST['E-Mail'];
$phoneNumber = $_POST['Phone_Number'];
$questionField = $_POST ['Query'];
$body = <<<EOD
<br><hr><br>
Name: $name <br>
Email: $email <br>
Questions: $question <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
#mail($webMaster, $emailSubject, $body, $headers);
exit;
}
?>
HTML FORM
<form action="Php/form.php" method="post" name="Contact_Form" >
<label for="fullname">Full Name: </label>
<input type="text" name="Full_Name" id="name"><br>
<label for="E-Mail">E-Mail: </label>
<input type="text" name="E-Mail" id="email" ><br>
<label for="PhoneNumber">Phone Number: </label>
<input type="text" name="Phone_Number" id="phonenumber" ><br>
<label for="Query">Query: </label>
<textarea name="Query" rows="4" cols="21" id="query" ></textarea><br>
<input type="submit" name="submit" value="Send" >
</form>

Can someone find where's wrong in this code PHP email

I'm not receiving mails on the email mail#example.com. Below is my form code and my send-mail.php code. Can anyone help me with this cause everything seems working great bu i'm not receiving any emails. I'm using localhost as the server.
Contact form:
<form id="contactForm" action="#" method="post">
<p>Email us by filling in the form below. Make sure you fill in the message and all fields.</p>
<fieldset>
<div>
<input name="name" id="name" type="text" class="form-poshytip" title="Enter your name" />
<label>Name</label>
</div>
<div>
<input name="web" id="web" type="text" class="form-poshytip" title="Enter your surname" />
<label>Surname</label>
</div>
<div>
<input name="email" id="email" type="text" class="form-poshytip" title="Enter your email address" />
<label>Email</label>
</div>
<div>
<textarea name="comments" id="comments" rows="5" cols="20" class="form-poshytip" title="Enter your comments"></textarea>
</div>
<!-- send mail configuration -->
<input type="hidden" value="mail#example.com" name="to" id="to" />
<input type="hidden" value="Enter the subject here" name="subject" id="subject" />
<input type="hidden" value="send-mail.php" name="sendMailUrl" id="sendMailUrl" />
<!-- ENDS send mail configuration -->
<p><input type="button" value="Send" name="submit" id="submit" /> <span id="error" class="warning">Message</span></p>
</fieldset>
</form>
<p id="sent-form-msg" class="success">Form data sent. Thanks for your feedback.</p>
<!-- ENDS form -->
and here is the send-mail.php
<?php
//vars
$subject = $_POST['subject'];
$to = explode(',', $_POST['to'] );
$from = $_POST['mail#example.com'];
//data
$msg = "NAME: " .$_POST['name'] ."<br>\n";
$msg .= "EMAIL: " .$_POST['email'] ."<br>\n";
$msg .= "WEBSITE: " .$_POST['web'] ."<br>\n";
$msg .= "COMMENTS: " .$_POST['comments'] ."<br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
//send for each mail
foreach($to as $mail){
mail($mail, $subject, $msg, $headers);
}
?>
$_POST['subject'];
$_POST['to'];
$_POST['myemail#gmail.com'];
$_POST['name'];
$_POST['email'];
$_POST['web'];
$_POST['comments'];
I didn’t find any of these elements in your form. That's the reason why nothing is happening.Try
echo '<pre>';
print_r($_POST);
This will give you the posted array when the form is submitted.
i have some suggestion.if u have kept the 'to' address as hidden in the form then why cant u try keeping it directly in sendmail function and in $from you try to keep
<?php
$to="kurtfarrugia92#gmail.com";
$from =$_POST['field_name'];
// not the mail id because i didn't see any field with name as "kurtfarrugia92#gmail.com"
?>
You cannot use this function to send mail from localhost. I am not sure but you should try PHP mailer for this task.

Php mail function won't work

After researching several questions and try multiple options, I couldn't make it work still!
I have this contact form that won't be sent.
<form id="contact" method="post" action="process.php">
<fieldset>
<label for="name">Name:</label>
<input type="text" name="name" placeholder="Your name" title="Your name" class="required">
<label for="email">E-mail:</label>
<input type="email" name="email" placeholder="yourmail#domain.com" title="Your e-mail address" class="required email">
<label for="phone">Phone number:</label>
<input type="tel" name="phone" placeholder="+34 111 22 33 44" title="Your phone number">
<p> </p>
<input type="radio" name="rsvp" value="si">
<span class="destacar-contacto">ACCEPT</span> the invitation<br>
<input type="radio" name="rsvp" value="no">
<span class="destacar-contacto">REJECT</span> the invitation<br>
<p> </p>
<p class="negrita-contacto">If you're coming: what would you like better?</p>
<input type="radio" name="menu" value="carne"> Calf sirloin with foie<br>
<input type="radio" name="menu" value="pescado"> Marinade salmon with dill<br>
<input type="radio" name="menu" value="vegetariano"> Fungus risotto<br>
<label for="mas">Is someone coming with you? Let us know their name and their prefered menu here:</label>
<textarea name="mas"></textarea>
<label for="message">Aditional message:</label>
<textarea name="message"></textarea>
<input type="submit" name="submit" class="boton" id="submit" value="Enviar" />
</fieldset>
</form>
And this is the PHP function:
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$phone = strip_tags($_POST['phone']);
$rsvp = strip_tags($_POST['rsvp']);
$menu = strip_tags($_POST['menu']);
$mas = strip_tags($_POST['mas']);
$message = strip_tags($_POST['message']);
mail( "formulario#ourdreamjourney.com", "rsvp",
"Name: $name\nEmail: $email\nPhone: $phone\nRsvp: $rsvp\nMenu: $menu\nMas: $mas\nMessage: $message\n",
"From: Our Dream Journey <mail#hotmail.com>" );
I tried randomly to send just "Name: $name" and I got an e-mail! But then put all the other options back and nothing again...
Someone can help me please?
Thank you so much in advance! :)
You can use phpMailer class. Download this library from here. Many example with to send email.
http://code.google.com/a/apache-extras.org/p/phpmailer/downloads/list
Edit:
Try this code
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From:Our Dream Journey <mail#hotmail.com>' . "\r\n";
if(mail( "formulario#ourdreamjourney.com", "rsvp",nl2br("Name: $name\nEmail: $email\nPhone: $phone\nRsvp: $rsvp\nMenu: $menu\nMas: $mas\nMessage: $message\n"),
$headers )) echo "Sent"; die;
Try to add all the information to the header element:
$headers = "From: myplace#here.com\r\n";
$headers .= "Reply-To: myplace2#here.com\r\n";
$headers .= "Return-Path: myplace#here.com\r\n";
$headers .= "CC: sombodyelse#noplace.com\r\n";
$headers .= "BCC: hidden#special.com\r\n";
Maybe there also other options that I don't remember like MIME
Maybe another problem is that some of the variables break the string(close the string). Try to print it somewhere,a file with file_put_contents or just echo it

Categories