sending email upon hitting submit in html or php - php

I have a simple site with 3 input forms: a first name, last name, and email address fields for the user to fill out. I want them to be able to fill those 3, and when hitting submit, an email to be generated with some autofilled text, for example:
email: what the user put as their email
the subject will be something I make, like, "regarding your _"
and the body of the email to use their first and last name they entered like so:
Dear __ __,
click here to fill out this bla bla bla etc
How would I go about doing this?
any helpful links or tutorials would be fantastic

you can make a simple html and process it by php. You can do like this :
index.php:
<form action='sendmail.php' method='post'>
<table>
<tr>
<td>Firstname</td>
<td><input name="firstName" /></td>
</tr>
<tr>
<td>LastName</td>
<td><input name="firstName" /></td>
</tr>
<tr>
<td>Lastname</td>
<td><input name="lastName" /></td>
</tr>
<tr>
<td>Email</td>
<td><input name="email" /></td>
</tr>
<tr>
<td colspan="2"><input type='submit' value='send' /></td>
</tr>
</table>
</form>
sendmail.php:
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];
//build email content
$emailSubject = "Email subject";
$mailContent = "Regarding ".$firstName." ".$lastName;
$mailContent .= "more content here";
//send mail
if(mail($email,$emailSubject,$mailContent))
echo "mail send successful";
else
echo "mail send fail";
NOTICE: I suppose that all the input param is valid for send mail. You should add more validation for input field.

I would not use the basic mail function within PHP as it does not protect you against header inject (open relay etc).
Take a look at one of these:
- http://framework.zend.com/manual/2.0/en/modules/zend.mail.message.html
- http://symfony.com/doc/2.0/cookbook/email/email.html
NB: They can be used outside of the MVC framework, i.e just as a mail library

This short code for this...i hope its clear to you and will help to you, there is a lot of tutorials ......just try to search in google
foreach($_POST as $field_name=>$field_val)
{
$message = $message. $field_name . " : $field_val \n";
}
//email code
$headers = "From: Feedback <".$email.">";
mail($email_to, $subject, $message, $headers);

Related

Mail sending is failure in php contact us form [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I am creating a contact us form, In that i am using HTML and PHP .Now my doubt is after click the submit button the details are not sending to mail,I want to know where i am mistaken on my code.
Here my HTML code for create the form
<form action="sendmail.php" method="post" >
<table width="100%" border="0"align="center"cellpadding="3"cellspacing="1">
<tr>
<td width="10%">Subject</td>
<td width="2%">: </td>
<td width="82%"><input name="name" type="text" id="name" size="50"></td>
</tr>
<tr>
<td>Detail</td>
<td width="2%">: </td>
<td width="82%"><textarea name="detail" cols="50" rows="4"id="detail"></textarea></td>
</tr>
<tr>
<td>Email</td>
<td>: </td>
<td><input name="customer_mail" type="text" id="customer_mail" size="50"></td>
</tr>
<tr>
<td>Name</td>
<td>: </td>
<td><input name="name" type="text" id="name" size="50"></td>
</tr>
<tr>
<td><input type="submit" name="send" value="Submit">
<input type="reset" name="submit" value="Reset">
</td>
</tr>
</table>
</form>
PHP code is below:
<?php
$subject="$subject";
$message="$detail";
$mail_from="customer_mail";
//From
$header="from: $name <$mail_from>";
//Enter your email address
$to="simon#abcinfomedia.in";
$sendmail=mail($to,$subject,$message,$header);
//check mail send to ur mail
if($sendmail){
echo"success";
}
else{
echo"Error";
}
?>
please try this:
<?php
if(isset($_POST['send']) && !empty($_POST['send'])) {
$subject = $_POST['subject'];//if you sent by post method else put manually
$message = $_POST['detail'];
$mail_from = $_POST['customer_mail'];
$header = "from: $name <$mail_from>";
//Enter your email address
$to = "simon#abcinfomedia.in";
$sendmail = mail($to, $subject, $message, $header);
//check mail send to ur mail
if ($sendmail) {
echo "success";
} else {
echo "Error";
}
}
?>
This is not working because you're not filling the variables with the data from the form by using $_POST[''].
The HTML doesn't change, but try this PHP:
<?php
$subject = $_POST['subject'];
$message = $_POST['detail'];
$mail_from = $_POST['customer_mail'];
$header = "from: $name <$mail_from>";
$to = "simon#abcinfomedia.in";
//check mail send to ur mail
if(mail($to,$subject,$message,$header)){
echo"success";
}
else{
echo"Error";
}
?>
I have defined subject, message and mail_from using the $_POST data from the form. For the if statement, I have dropped the part where you store the mail() function as a variable and just put it straight into the if.
This will simultaneously send the email and check it at the same time.
In my test, the email worked but there were flaws because some of your fields are missing and not defined, but I believe the above solves your initial problem. :)

PHP email formatted content

I am creating an email form for my website which will allow a user to create an email and appear to be sent from any email address they wish. i.e. davidcameron#downingstreet.com.
The form works fine, until I made one small change. I previously allowed the user to submit the content of the email but was finding difficulty in formatting the content (i.e. making areas bold, using a table etc).
(Form code now)
<html>
<body>
<form method="GET" action="send.php">
<p>To: <input type="text" name="to" /></p>
<p>From-Name: <input type="text" name="name" /></p>
<p>From-Email: <input type="text" name="from" /></p>
<p>Subject: <input type="text" name="subject" /></p>
<input type="submit" value="Send E-Mail" ></p>
</form>
</body>
</html>
So I thought it would be easier to submit the content myself in my 'send.php' code that actually sends the email message. This is displayed below:
<html>
<body>
<?php
$to =$_REQUEST['to'];
$subject = $_REQUEST['subject'];
$name =$_REQUEST['name'];
$from = $_REQUEST['from'];
$content = ?><font face="arial"><b>Your question has been received/b>
Your booking has been confirmed with the supplier.
Please visit the Question Portal for more information.
<table>
<tr>
<td>Question subject</td>
<td> Room</td>
</tr>
<tr>
<td>Traveller</td>
<td>Maria Smith</td>
</tr>
<td>Requester</td>
<td>Tony Smith</td>
</tr>
</table>
<br/>
</font>
<?
$header="From: $from"."<$sender_email>\r\n";
mail($to,$subject,$content,$header);
echo 'sent successfully';
?>
</body>
</html>
However, when I click submit, my server finds an error. I can only presume it is because of my HTML element as this error was not occurring previously without it.
Can anyone advise how I can fix this / if you can suggest an easier way for me to format the email content I would like?
Many thanks
Set your content value to
$content = ''
Since your $content is undefined that is why you are getting error.
Hope this will fix your issue.
Try this
<?php
$to = $_REQUEST['to'];
$subject = $_REQUEST['subject'];
$name = $_REQUEST['name'];
$from = $_REQUEST['from'];
$content =
'<span style="font-family: arial"><b>Your question has been received</b>
<br>
Your booking has been confirmed with the supplier.
Please visit the Question Portal for more information.
<table>
<tr>
<td>Question subject</td>
<td> Room</td>
</tr>
<tr>
<td>Traveller</td>
<td>Maria Smith</td>
</tr>
<td>Requester</td>
<td>Tony Smith</td>
</tr>
</table>
<br/>
</span>';
$header = "From:".$from;
$result = mail($to,$subject,$content,$header);
if(isset($result))
{
echo 'sent successfully';
}
else
{
echo 'Failed';
}
?>
Note: <font> tag not supported in HTML5
EDIT 01
Include this lines too
$header .= 'MIME-Version: 1.0';
$header .= 'Content-Type: text/html; charset="ISO-8859-1';

How to send response email to user on form submit PHP

I have a rather basic php form that submits on the same page it is called "registration.php". I have it set to collect the form data, store it in the $to, $subject and $body variables then I call the mail() method to send off an email to myself.
please see code below ( Don't worry! there is a question comin' right up! ):
<?php
ob_start(); //output buffering because I like it.
if (isset($_POST['submit'])) {
// Process the form
$message = "Thank you for registering! We will respond to your request shortly";
$name = $_POST['name'];
$email = $_POST['email'];
$address = $_POST['address'];
$comments = $_POST['userComment'];
$date = gmdate("M d Y");
$to = "myemail#outlook.com";
$subject = "Registration Submission";
$body = " Date: $date \n Registrant Name/Name's: $name \n Registrant E-mail: $email \n \n User Comments: \n $comments \n \n";
mail($to,$subject,$body);
}
?>
<form id="contactForm" name="contactForm" action="registration.php" method="post">
<table>
<tr>
<td><label for="name"><strong>Registrant Name / Names</strong></label></td>
<td><input required='required' type="text" id="name" name="name" /></td>
</tr>
<tr>
<td><label for="email"><strong>Registrant E-mail</strong></label></td>
<td><input required='required' type="email" maxlength="40" id="email" name="email" /></td>
</tr>
<tr>
<td><label for="subject"><strong>Registrant Address</strong></label></td>
<td><input required='required' type="text" maxlength="100" id="address" name="address" /></td>
</tr>
<tr>
<td>Message:</td>
<td> </td>
</tr>
<tr>
<td id="textAreaInput" colspan="2"><textarea rows="3" id="userComment" name="userComment" placeholder="Let us know your thoughts!"></textarea></td>
</tr>
</table>
<br />
<button type="submit" name="submit" id="submit">Submit</button>
</form>
What I want to know is Can I send a custom response email to the users collected $email with a custom message using just php? can I send other variables through the mail() method? like $to2, $subject2, $body2 or must they be named $to, $subject & $body when passed?
I have a working solution using PHPMailer found here:
https://github.com/PHPMailer/PHPMailer
I am just curious as to weither or not there is an easy process using PHP that wouldn't rely on a required library.
I think all you have to do is
if(mail($to,$subject,$body)){
mail($email,"Thanks!!","Thank you very much for registering!!!")
}

Trouble with a Simple PHP Contact Form [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I'm having trouble figuring out why my contact form isn't working. I'm relatively new to PHP and from what I can see, everything is linked, but when I go to send the email, nothing is sent to the designated delivery address. Any help would be appreciated!! Thank you!!
My code is below:
HTML
<table width="400" border="0" align="left" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="mail.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td width="16%">Subject</td>
<td width="2%">:</td>
<td width="82%"><input name="subject" type="text" id="subject" size="50"></td>
</tr>
<tr>
<td>Message</td>
<td>:</td>
<td><textarea name="detail" cols="50" rows="4" id="detail"></textarea></td>
</tr>
<tr>
<td>Name</td>
<td>:</td>
<td><input name="name" type="text" id="name" size="50"></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="customer_mail" type="text" id="customer_mail" size="50"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
PHP
<?php
// Contact subject
$subject ="$subject";
// Details
$message="$detail";
// Mail of sender
$mail_from="$customer_mail";
// From
$header="from: $name <$mail_from>";
// Enter your email address
$to ='jordanmcowan#gmail.com';
$submit=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've recived your information"
if($submit){
echo "Thank you for contacting us at Allstar Therapies, Inc.<br /> We will be in touch shortly!";
}
else {
echo "ERROR";
}
?>
Your PHP Code seems wrong to me. This is a corrected version
<?php
// Contact subject
$subject = $_POST['subject'];
// Details
$message = $_POST['detail'];
// Mail of sender
$mail_from = $_POST['customer_mail'];
// From
$header = "from: " . $_POST['name'] . "<" . $_POST['customer_mail'] . ">";
// Enter your email address
$to = 'jordanmcowan#gmail.com';
$submit = mail($to, $subject, $message, $header);
// Check, if message sent to your email
// display message "We've recived your information"
if ($submit) {
echo "Thank you for contacting us at Allstar Therapies, Inc.<br /> We will be in touch shortly!";
} else {
echo "An error has been encountered while sending your message. We sincerely apologize and ask you to try again. If that fails as well, please contact us at XYZ-Y234-SADF";
}
The variables haven't been initialized. Please note that these symbols: "" mark the beginning and the end of a string.
These lines are wrong:
// Contact subject
$subject ="$subject";
// Details
$message="$detail";
// Mail of sender
$mail_from="$customer_mail";
// From
$header="from: $name <$mail_from>";
You're trying to assign the variables to themselves. Try:
$subject = $_POST['subject'];
$message = $_POST['detail'];
etc.
You need to use $_POST variable:
// Contact subject
$subject = isset($_POST["subject"]) ? ($_POST["subject"]) : "";
You haven't set your PHP variables (IE $subject) to your post variables yet. Try changing your PHP variables to use the $_POST variables.
<?php
// Contact subject
$subject = $_POST['subject'];
// Details
$message= $_POST['detail'];
// Mail of sender
$mail_from= $_POST['customer_mail'];
// From
$header="from: {$_POST['name']} <{$_POST['mail_from']}>";
// Enter your email address
$to ='jordanmcowan#gmail.com';
$submit=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've recived your information"
if($submit){
echo "Thank you for contacting us at Allstar Therapies, Inc.<br /> We will be in touch shortly!";
}
else {
echo "ERROR";
}
?>
You can access to POST parametars by $_POST / GET parametars by $_GET. First check if your redirect works correct.
type:
echo "welcome";
in mail.php
If redirect works the print all Post parametars:
print_r($_POST);
Last you can get one by one
$variable = $_POST['input_name_from_form'];
Hope this is solution for your problem

PHP contact form giving error

My basic contact form is giving me the error "error during sending mail". Of course, the code below is very simple and has no protection against spammers or SQL injection, but my main concern at the moment is getting the message to send.
Thanks in advance for any help given, despite the simplicity of the question.
HTML:
<form action="sendmail.php" method="post">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="Name" size="20" maxlength="40" /></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="Email" size="20" maxlength="60" /></td>
</tr>
<tr>
<td>Message:</td>
<td><textarea name="Message" maxlength="1000" cols="25" rows="6"></textarea></td>
</tr>
</table>
<div align="left">
<input name="submit" type="submit" />
</div>
</form>
sendmail.php PHP:
<?php
if(isset($_POST['submit']))
{
$name = $_POST['Name'];
$email = $_POST['Email'];
$message = $_POST['Message'];
$from = 'From: CWON Australia';
$to = "......#hotmail.com";
$subject = "CWON Message";
$content = "From: $name\n E-Mail: $email\n Message:\n $message";
if(mail ($to, $subject, $content, $from)) {
echo "mail has been sent";
}
else
{
echo "error during sending mail";
}
}
?>
Your code is functional, and I assume you're getting the "mail has been sent" response.
If you aren't, then you'll first want to check your php.ini for the correct sendmail path (this I believe is the default):
sendmail_path = /usr/sbin/sendmail -t -i
If sendmail path is good, do you see your email in mail logs? I'd also test by mailing your user account on the server itself.
It's more likely that you're getting owned by a spam filter, especially if you're emailing to an "#hotmail.com" address.
See this SO question for some guidance
The mail() function is not very reliable in php and it is hard to configure... So I recommend you use the PHPMailer to send your mails...
You can read all about how to send mail using PHPMailer in here : PHPMailer GitHUB

Categories