php and html email forms - php

I am creating a website for my church choir in HTML. I have a "contact us" page written in HTML. It has a form for the user to send the choir director an email from the website. I am aware that I have to write the email in php in order for the email to send. Do I need to duplicate the html file and rewrite it in PHP?
<form name="Send-mail" action="mailto:email#email.com" method="post">
<label>Name:</label>
<input class="nice" name="name" type="text"/>
<label>Email:</label>
<input class="nice" name="email" type="text"/>
<label>Subject:</label>
<input class="nice" name="subject" type="text"/>`enter code here`
<label>Message:</label>
<text-area type="text" name="message"></text-area>
<input class="centered" type="submit" name="submit" value="Send email"/>
</form>
I need the server to send the email from the website to the email of our choir director.

You would need to create the PHP file where there is an email sending logic,eg: sendEmail.php. The form tag would change to:
<form name="Send-mail" action="sendEmail.php" method="post">

Below is how you can send the mail in php from form inputs.
<form action="mail.php" method="post">
<label>Name:</label>
<input class="nice" name="name" type="text"/>
<label>Email:</label>
<input class="nice" name="email" type="text"/>
<label>Subject:</label>
<input class="nice" name="subject" type="text"/>`enter code here`
<label>Message:</label>
<text-area type="text" name="message"></text-area>
<input class="centered" type="submit" name="submit" value="Send email"/>
</form>
You will have to name your php file mail.php like in case below
mail.php
<?php
$to = $_POST['email'];
$subject = $_POST['subject'];
$sender = $_POST['name'];
$message = $_POST['message'];
$message .= "<h1>This is Message from $sender.</h1>";
$header = "From:senderemail#somedomain.com \r\n";
$header .= "Cc:senderemail#somedomain.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
?>
You will need to ensure that email for this line of code is correctly set.
$header = "From:senderemail#somedomain.com \r\n";
$header .= "Cc:senderemail#somedomain.com \r\n";
In otherword, if your site email is support#hey.com
Try Replacing it snderemail#somedomain and it will work. You can also try the replacement with some other emails like good working gmail and let me know what happen. Thanks

Related

PHP Form mail redirecting page not working

The form works and I get the email but when you click submit it won't take you to the thank you page, it gives me the following error message:
Error 404 - Not Found
The document you are looking for may have been removed or re-named.
Please contact the web site owner for further assistance.
This is the code for the form:
<section class="contactForm">
<h1><strong>Contact Andrea</strong></h1>
<form action="http://ddion.com/webcert_s17/andreas/DW_FINAL/formmail.php" method="post" name="form1" id="form1">
<label for="name">Name:</label>
<input name="name" type="text" required class="formStyle" id="name" form="form1">
<br>
<label for="email">Email:</label>
<input name="email" type="email" required class="formStyle" id="email" form="form1">
<br>
<label for="comments">Comments:</label>
<textarea name="comments" cols="5" required class="formStyle" id="comments" form="form1"></textarea>
<br>
<input type="submit" class="formStyle" name="submit" id="submit" value="Submit">
</form>
</section>
This is the PHP
<?php
$name=addslashes($_POST['name']);
$email=addslashes($_POST['email']);
$comments=addslashes($_POST['comments']);
// you can specify which email you want your contact form to be emailed to here
$toemail = "anlsimental#gmail.com";
$subject = "from AndreaSimentalPhotogragy.com";
$headers = "MIME-Version: 1.0\n"
."From: \"".$name."\" <".$email.">\n"
."Content-type: text/html; charset=iso-8859-1\n";
$body = "Name: ".$name."<br>\n"
."Email: ".$email."<br>\n"
."Comments:<br>\n"
.$comments;
if (!ereg("^[a-zA-Z0-9_]+#[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $email))
{
echo "That is not a valid email address. Please return to the"
." previous page and try again.";
exit;
}
mail($toemail, $subject, $body, $headers);
echo "Thanks for submitting your comments";
?>
My thankyou.html is located next to the php. But somehow doesn't work... Any ideas why?
What you want to do is redirect to http://ddion.com/webcert_s17/andreas/DW_FINAL/thankyou.html instead of http://ddion.com/webcert_s17/andreas/DW_FINAL/0;URL=thankyou.html. Fix it by removing the preceding 0;URL= from the filename in the URL.
The deprecated ereg() function to preg_match()
Use this
preg_match()

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>

Contact form not working while click on submit button

I was working with an HTML contact form, its action is a mail sending php script. But it is not working when i am clicking on the send button. I couldn't find any possible here. Please someone help me to fix this.
HTML form
<div class="contact-form">
<form action="sendmail.php" method="post">
<div class="control-group"><label class="nameLabel" for="name">Name</label> <input id="name" name="name" type="text" /></div>
<div class="control-group"><label class="emailLabel" for="email">Email</label> <input id="email" name="email" type="text" /></div>
<div class="control-group"><label class="messageLabel" for="message">Message</label><textarea id="message" name="message"></textarea></div>
<div class="control-group"><button type="submit">Send</button></div>
</form>
<h1 class="status-message-contact"></h1>
</div>
PHP code
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$query = $_POST['message'];
$email_from = $name.'<'.$email.'>';
$to="sarath.sarigama#gmail.com";
$subject="WEB Enquiry!";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$email_from."\r\n";
$message="
Name:
$name
<br>
Email-Id:
$email
<br>
Message:
$query
";
if(mail($to,$subject,$message,$headers))
header("Location:../index.php?msg=Successful Submission! Thankyou for contacting us.");
else
header("Location:../index.php?msg=Error To send Email !");
//contact:-your-email#your-domain.com
}
?>
you dont have submit button name ,change name of the button
<button name='submit' type="submit">Send</button>
or use input tag
<input name='submit' type='submit'>
Replace
<button type="submit">Send</button>
with
<input type="submit" value="Send" />
And maybe add
<input type="hidden" name="submit" value="1" />
For the isset( $_POST['submit'] )

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 header redirect doesn't work

I've created a mail form. A PHP page reads the input, sends it out to a mail and then it should redirect to a certain page. Pretty straight forward, done it before. The mail gets send, but you don't get redirected.
HTML form:
<form action="mailversturen.php" method="post">
<input type="text" name="naam" placeholder="Naam" class="inputtype" /><br />
<input type="text" name="email" placeholder="Email" class="inputtype" /><br />
<textarea name="bericht" placeholder="Bericht"></textarea>
<input type="reset" value="Reset" class="buttontype" />
<input type="submit" value="Verstuur" class="buttontype" />
</form>
PHP code:
<?php
$name = $_POST['naam'];
$email = $_POST['email'];
$message = $_POST['bericht'];
$to = "name#domain.com";
$subject = "Bericht van $name";
$headers = "From: $email \r\n";
mail($to,$subject,$message,$headers);
header('Location: http://www.newlocation.nl/');
?>
What am I doing wrong?
Try adding #ob_start(); after your opening <?php tag.
This turns on output buffering. I use it in combination with all header(...) redirects.

Categories