Jump to content

html form to email using php


sanji982

Recommended Posts

Hi there, I would need some help as I'm tryin to set up a email form with php.

it seems to work as it returns the echo as requested

    if (mail("[email protected]", $subject, $message, $from)) {

    echo "<h2>La ringraziamo per l'interesse, la sua email é stata inviata.</h2>";}

 

the html form:

<form method="post" action="contact.php">

            <table align="center" background="bkg.jpg" bgcolor=#FFFFFF>

<tr>

  <td colspan=2><div align="center"><strong>Contattaci</strong></div></td>

</tr>

 

<tr><td width="255"><div align="center"><font color=red>*</font> Nome:</div></td><td width="360"><input size=60 name="Nome"></td></tr>

<tr><td><div align="center"><font color=red>*</font> Email:</div></td><td><input size=60 name="visitoremail"></td></tr>

<tr>

  <td><div align="center">Oggetto:</div></td><td><input size=60 name="Oggetto"></td></tr>

<tr><td colspan=2><div align="center">Message:</div></td></tr>

<tr>

  <td colspan=2 align=center><textarea name="Messaggio" rows=8 cols=100></textarea></td>

</tr>

<tr><td colspan=2 align=center><input type=submit name="send" value="Submit"></td></tr>

<tr>

  <td colspan=2 align=center><small>NB. <font color=red>*</font> campo obbligatorio </small></td>

</tr>

</table>

</form>

 

nevertheless I don't get any email from it.... so I'm not sure anymore if that's a problem with the server or an error in my code...

 

can anyone help please??

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/132587-html-form-to-email-using-php/
Share on other sites

I believe sendmail must be configured on your server for the mail() function to work.  You should check with your hosting company, or, if it is your own server, look into configuring sendmail for PHP.

 

From the docs:

(http://us.php.net/manual/en/mail.setup.php)

For the Mail functions to be available, PHP must have access to the sendmail binary on your system during compile time. If you use another mail program, such as qmail or postfix, be sure to use the appropriate sendmail wrappers that come with them. PHP will first look for sendmail in your PATH, and then in the following: /usr/bin:/usr/sbin:/usr/etc:/etc:/usr/ucblib:/usr/lib. It's highly recommended to have sendmail available from your PATH. Also, the user that compiled PHP must have permission to access the sendmail binary.

Here's a form mail I stitched together from various tutorials. Hope you like!

 

form.html

<html>
<head></head>
<body>

<table>


<form action="process-form.php" ...
   method="post">

  <tr>
  <td>Name</td>
  <td><input type="text" id="name" name="name" /></td>
  </tr>

  <tr>
  <td>Email address:</td>
  <td><input type="text" id="email" name="email" /></td>
  </tr>

  <tr>
  <td>Recipient:</td> 
  <td> 
  <select id="recipient" name="recipient">
  <option value="a">someone</option>
  <option value="b">someoneelse</option>
  </select>
  </td>
  </tr>

  <tr>
  <td>Subject:</td>
  <td><input type="text" id="topic" name="topic" /></td>
  </tr>

  <tr>
  <td valign="top">Your comments:</td>
  <td><textarea id="comments" name="comments" rows="5" cols="30"></textarea></td>
  </tr>

  <tr>
  <td></td>
  <td><button type="submit">Send</button></td>
  </tr>

</form>
</table>


</body>
</html>

 

process-form.php

<?php
// Pick up the form data and assign it to variables
$name = $_POST['name'];
$email = $_POST['email'];
//$recipient = $_POST['recipient'];
// Conceal mail, switch and case
switch(@$_POST['recipient']) {
    case 'a': $recipient = '[email protected]'; break;
    case 'b': $recipient = '[email protected]'; break;
    default: $recipient = '[email protected]'; // if they don't send one of the valid options
}
$topic = $_POST['topic'];
$comments = $_POST['comments'];
$now_datetime = date('Y-m-d h:i:s');
$ipaddress = getenv('REMOTE_ADDR');

// Build the email
//$to = '[email protected]';
$to = "$recipient";
$subject = "New message: $topic";
$message = "$name wrote:\n\n$comments\n\nMessage sent: $now_datetime\n\nI.P. Address: $ipaddress";
$headers = "From: $email";


// Send the mail using PHPs mail() function
mail($to, $subject, $message, $headers);

//write the information back to database
include("dbinfo.inc.php");

$now_datetime = date('Y-m-d h:i:s');

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO yourtablename VALUES ('','$now_datetime','$ipaddress','$name','$email','$recipient','$topic','$comments')";
mysql_query($query);

mysql_close();

// Redirect
header("Location: success.html");

 

dbinfo.inc.php

<?
$username="yourusername";
$password="yourpassword";
$database="yourdatabase";
?>

 

success.html

<html>
<head></head>
<body>

<p>Your information has been sent!</p>

</body>
</html>

 

The drop down allows users to select from a recipient list. Recipient recieves Sender's Email, Name, Message, Date/Time and IP Address. A copy of the message is also saved to a database.

 

The DATABASE structure is as follows:

 

index | mediumint(9) | Null No | autoincrement

time| datetime| NULL Yes | Default NULL

ipaddress | mediumint(9) | Null No

name | varchar(100) | Null No

email| varchar(255) | Null No

recipient | varchar(255) | Null No

topic | varchar(255) | Null No

comments | text | Null No

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.