Send an email from PHP from an html form to submit with the same script

I want to send an email with PHP when the user has finished filling out the HTML form, and then sent the email from the form. I want to do this from the same script that displays a webpage with a form.

I found this code, but mail does not send.

<?php if (isset($_POST['submit'])) { $to = $_POST['email']; $subject = $_POST['name']; $message = getRequestURI(); $from = "zenphoto@example.com"; $headers = "From:" . $from; if (mail($to, $subject, $message, $headers)) { echo "Mail Sent."; } else { echo "failed"; } } ?> 

What is the code for sending email in PHP?

+45
html php email
Aug 22 '13 at 11:33
source share
8 answers

EDIT (# 1)

If I understand correctly, you want to have everything on one page and execute it from the same page.

You can use the following code to send mail from one page, for example index.php or contact.php

The only difference between this and my original answer is <form action="" method="post"> , where the action is left blank.

Better use header('Location: thank_you.php'); instead of echo in the PHP handler to subsequently redirect the user to another page.

Copy all the code below into one file.

 <?php if(isset($_POST['submit'])){ $to = "email@example.com"; // this is your Email address $from = $_POST['email']; // this is the sender Email address $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $subject = "Form submission"; $subject2 = "Copy of your form submission"; $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message']; $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message']; $headers = "From:" . $from; $headers2 = "From:" . $to; mail($to,$subject,$message,$headers); mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly."; // You can also use header('Location: thank_you.php'); to redirect to another page. } ?> <!DOCTYPE html> <head> <title>Form submission</title> </head> <body> <form action="" method="post"> First Name: <input type="text" name="first_name"><br> Last Name: <input type="text" name="last_name"><br> Email: <input type="text" name="email"><br> Message:<br><textarea rows="5" name="message" cols="30"></textarea><br> <input type="submit" name="submit" value="Submit"> </form> </body> </html> 



Original answer




I was not sure what this question was, but it seems that a copy of the message should be sent to the person who filled out the form.

Here is a test / working copy of the HTML form and PHP handler. This uses the PHP mail() function.

The PHP handler will also send a copy of the message to the person filling out the form.

You can use two slashes // before the line of code if you are not going to use it.

For example: // $subject2 = "Copy of your form submission"; will not be executed.

HTML FORM:

 <!DOCTYPE html> <head> <title>Form submission</title> </head> <body> <form action="mail_handler.php" method="post"> First Name: <input type="text" name="first_name"><br> Last Name: <input type="text" name="last_name"><br> Email: <input type="text" name="email"><br> Message:<br><textarea rows="5" name="message" cols="30"></textarea><br> <input type="submit" name="submit" value="Submit"> </form> </body> </html> 



PHP handler (mail_handler.php)

(Uses information from an HTML form and sends an email)

 <?php if(isset($_POST['submit'])){ $to = "email@example.com"; // this is your Email address $from = $_POST['email']; // this is the sender Email address $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $subject = "Form submission"; $subject2 = "Copy of your form submission"; $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message']; $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message']; $headers = "From:" . $from; $headers2 = "From:" . $to; mail($to,$subject,$message,$headers); mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly."; // You can also use header('Location: thank_you.php'); to redirect to another page. // You cannot use header and echo together. It one or the other. } ?> 



Send in HTML format:

If you want to send mail as HTML for both instances, you will need to create two different sets of HTML headers with different variable names.

Read the mail() guide to learn how to send emails as HTML:




Footnote:

  • Regarding HTML5

You must specify the URL of the service that will process the submitted data using the action attribute.

As stated in https://www.w3.org/TR/html5/forms.html in section 4.10.1.3 Configuring the form for communication with the server. See the page for full details.

Therefore, action="" will not work in HTML5.

The correct syntax is:

  • action="handler.xxx" or
  • action="http://www.example.com/handler.xxx" .

Note that xxx will be the extension of the file type used to process the process. This may be an extension of .php , .cgi , .pl , .jsp , etc.




Consult the following Q & A on the stack if sending mail has failed:

  • PHP Email Form Does Not Complete Email
+99
Aug 22 '13 at 13:36 on
source share

PHP script to connect to an SMTP server and send email in Windows 7

Sending email with PHP on Windows is a bit of a minefield with gotchas and head scratches. I will try to get you through one instance where I got it to work with Windows 7 and PHP 5.2.3 under (IIS) the Internet Information Services web server.

I assume that you do not want to use any pre-created frameworks, such as CodeIgniter or Symfony, that contain email sending capabilities. We will send an email from a standalone PHP file. I got this code from under codeigniter hood (under system / libraries) and modified it so that you can just look into this Email.php file and it should just work.

This should work with newer versions of PHP. But you never know.

Step 1, you need username / password with SMTP server:

I am using the smtp server from smtp.ihostexchange.net , which has already been created and configured for me. If you do not have this, you cannot proceed. You should be able to use an email client such as thunderbird, evolution, Microsoft Outlook to specify your smtp server, and then you can send emails there,

Step 2. Create your Hello World email file:

I assume you are using IIS. Therefore, create a file under index.php under C:\inetpub\wwwroot and place this code there:

 <?php include("Email.php"); $c = new CI_Email(); $c->from("FromUserName@foobar.com"); $c->to("user_to_receive_email@gmail.com"); $c->subject("Celestial Temple"); $c->message("Dominion reinforcements on the way."); $c->send(); echo "done"; ?> 

You should be able to visit this index.php by going to localhost / index.php in the browser, this will lead to errors because Email.php is missing. But make sure you can at least run it from a browser.

Step 3, create a file called Email.php :

Create a new file called Email.php in the C:\inetpub\wwwroot .

Copy / paste this PHP code into Email.php:

https://github.com/sentientmachine/standalone_php_script_send_email/blob/master/Email.php

Since there are many types of smtp servers, you will have to manually configure the settings at the top of Email.php . I configured it so that it automatically works with smtp.ihostexchange.net , but your smtp server may be different.

For example:

  • Set the smtp_port parameter to the port of your SMTP server.
  • Set the smtp_crypto parameter for your smtp server.
  • Install $ newline and $ crlf so that it is compatible with what your SMTP server uses. If you are mistaken, the smtp server can ignore your request without errors. I use \ r \ n, maybe \n is required for you.

The linked code is too long to be inserted as a stackoverflow response. If you want to edit it, leave a comment here or via github, and I will change it.

Step 4, make sure your php.ini has the ssl extension:

Find the PHP.ini file and uncomment

 ;extension=php_openssl.dll 

It looks like this:

 extension=php_openssl.dll 

Step 5: run the index.php file that you just created in the browser:

You should get the following output:

 220 smtp.ihostexchange.net Microsoft ESMTP MAIL Service ready at Wed, 16 Apr 2014 15:43:58 -0400 250 2.6.0 <534edd7c92761@summitbroadband.com> Queued mail for delivery lang:email_sent done 

Step 6, check your email and spam folder:

Visit your email account for user_to_receive_email@gmail.com and you should receive an email. It should arrive within 5 or 10 seconds. If you do not, check the errors returned on the page. If this does not work, try stretching your face on the keyboard on Google, repeating: "Working at the grocery store is not so bad."

+4
Apr 16 '14 at 20:35
source share

If you have not already done so, look at your php.ini and make sure that the parameters under the [mail function] set correctly to activate the email service. After using PHPMailer and follow the instructions.

+3
Aug 22 '13 at 13:03
source share

You can also use mandrill app to send mail in php. You will get the API from https://mandrillapp.com/api/docs/index.php.html , where you can find complete information about emails sent and other details.

+3
Sep 25 '14 at 5:36
source share

Here are the PHP mail settings I use:

 //Mail sending function $subject = $_POST['name']; $to = $_POST['email']; $from = "zenphoto@example.com"; //data $msg = "Your MSG <br>\n"; //Headers $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=UTF-8\r\n"; $headers .= "From: <".$from. ">" ; mail($to,$subject,$msg,$headers); echo "Mail Sent."; 
+1
Aug 22 '13 at 11:47 on
source share

You need to add action to the form:

  <form name='form1' method='post' action='<?php echo($_SERVER['PHP_SELF']);'> <!-- All your input for the form here --> </form> 

Then place your snippet at the top of the document to send mail. What echo($_SERVER['PHP_SELF']); does echo($_SERVER['PHP_SELF']); is that it sends your information to the top of your script so you can use it.

+1
Aug 22 '13 at 11:53 on
source share

You need an SMPT server to

 ... mail($to,$subject,$message,$headers); 

for work.

You can try light weight SMTP servers like xmailer

0
Aug 22 '13 at 11:46 on
source share

I think one error in the source code could be this:

 $message = echo getRequestURI(); 

instead:

 $message = getRequestURI(); 

(Since then this code has been edited.)

-2
Dec 09 '15 at 8:50
source share



All Articles