Jump to content

Beginner needs help - simple mail_to contact form in PHP


conrad1234

Recommended Posts

Hi all! ... I used TrendyFlash Site Builder to create a site, which generated a 'contact.php' file that I need to modify. Basically it calls for the user to send a comment with a name, email id, subject and message. I need it to send those details to an external email id that I specify. Here's the script generated 'as is' by the software in that 'contact.php' file :

 

<?

$contactfield1=$HTTP_GET_VARS["contactfield1"]
$contactfield2=$HTTP_GET_VARS["contactfield2"]
$contactfield3=$HTTP_GET_VARS["contactfield3"]
$contactfield4=$HTTP_GET_VARS["contactfield4"]

$email_from = "you@yourdomain.com";
$email_subject =  "" ;
//$email_txt = "";
$email_to = "info@yourdomain.com";

$email_message = "field1: $contactfield1\n
field2 :$contactfield2 \n
field3 :$contactfield3 \n
field4 : $contactfield4 \n

";

  mail($email_to, $email_subject, $email_message,$email_from);

?>

 

OK ... i went through some sample contact form scripts online and modified it like this :

 

<?php

// I should think the following 4 lines correspond to the 4 fields indicated in the screenshot attached at the 
// end of this message

$contactfield1=$_POST["name"]
$contactfield2=$_POST["email_from"]
$contactfield3=$_POST["subject"]
$contactfield4=$_POST["comment"]

$email_from = "";
$subject =  "" ;
//$email_txt = "";
$email_to = "[b]my email id here[/b]";

$message = "field1: $contactfield1\n
field2 :$contactfield2 \n
field3 :$contactfield3 \n
field4 : $contactfield4 \n

";

  mail($email_to, $subject, $message,$email_from);

?>

 

So is this right ? I uploaded it to test it, but it doesn't seem to work as I'm not getting anything in my email box. If I can only get this script right, I can apply it to all future sites that I create using this software (excellent software btw, the site I created only came to <500kb!!!). So I just need to get it right once. This software isn't designed for scripting- PHP or otherwise, so it needs the user to know PHP if he wishes to go by that route (else I'd have to only publish the site to CD). Also if the code requires any proper formatting (as some lines in the script seem to me to be out of place), then pls provide that too.

 

Image Link :

wjxn44.jpg

 

PS. In case anyone's interested, the site can be found at this link http://visitmyspace.sitegoz.com/ <br>Don't use the contact form tho .... just need that damn script to work first ! :)

Link to comment
Share on other sites

Here you go,

 

Make sure you remove the error reporting after testing, i have tested and it works for me.


<?php


// testing for errors make sure you remove this when site goes live
ini_set( 'error_reporting', E_ALL );

$name = $_POST["name"]; //need to have semicolon at end of line
$email_from = $_POST["email_from"]; //need to have semicolon at end of line
$subject = $_POST["subject"]; //need to have semicolon at end of line
$comment = $_POST["comment"]; //need to have semicolon at end of line

$email_from = "";
$subject = "";
//$email_txt = "";
$email_to = "tyrongower@gmail.com";

$message = "
Name: $name\n
From: $email_from \n
Subject: $subject \n
Comment: " . wordwrap( $comment, 70 );

mail( $email_to, $subject, $message, $email_from );


?>

 

 

Link to comment
Share on other sites

I'd actually go for this (including the previous comment on semi-colons at the end of lines):

 

<?php

// I should think the following 4 lines correspond to the 4 fields indicated in the screenshot attached at the 
// end of this message

$contactfield1=$_POST["contactfield1"]; // this is field 1
$contactfield2=$_POST["contactfield2"]; // this is field 2
$contactfield3=$_POST["contactfield3"]; // etc
$contactfield4=$_POST["contactfield4"];

$email_from = "";
$subject =  "" ;
//$email_txt = "";
$email_to = "[b]my email id here[/b]";

$message = "field1: $contactfield1\n
field2 :$contactfield2 \n
field3 :$contactfield3 \n
field4 : $contactfield4 \n

";

  mail($email_to, $subject, $message,$email_from);

?>

 

I think the $_POST key names have to be left the same, as I dont see anywhere on the sceenshot to modify them. I'd guess the program uses generic key names for the form vars.

 

Also, there's a chance the form uses GET instead of POST, so you might need to change the $_POST to $_GET for all those vars. Would check your site to find out, but I'm at work at the moment.

Link to comment
Share on other sites

In Contact Information can we send images...

How to send them to mail ..please explain

 

 

http://au.php.net/manual/en/function.mail.php

 

you need to change it to an html email format and add the http address of the image to the body. the above link will explain the html email side of things have a go then post your code if not working

 

 

Link to comment
Share on other sites

I'd actually go for this (including the previous comment on semi-colons at the end of lines):

 

I think the $_POST key names have to be left the same, as I dont see anywhere on the sceenshot to modify them. I'd guess the program uses generic key names for the form vars.

 

Also, there's a chance the form uses GET instead of POST, so you might need to change the $_POST to $_GET for all those vars. Would check your site to find out, but I'm at work at the moment.

 

You are right, i overlooked that. You can still change the $variables for ease of understanding the script and you can change field1: ect  in the email comments to what they are e.g. subject comment ect.

Link to comment
Share on other sites

here is a template i keep and you can try to use.

It sends an email to you from the user and also sends a confirmation to the sender.

 


<? 
error_reporting(7);
$subject		= $_POST['subject'];
$firstname 		= $_POST["firstname"];
$lastname 		= $_POST["lastname"];
$email		= $_POST['email'];
$country		= $_POST["country"];
$telephone		= $_POST["telephone"];
$message		= $_POST['message'];

        header("Location: pagetoredirecttoname.php");

$to = "info@website.com";
$subject = "WEBSITE NAME Inquiry";
$MsgHeader = "From: WEBSITE NAME<noreply@website.com>\n";
$MsgHeader .= "MIME-Version: 1.0\n";
$MsgHeader .= "Content-type: text/html; charset=iso-8859-1\n";
$MsgBody = "
<html>
<head>
<title>HTML message</title>
</head>
<body>
<table>
<tr><td align='left'><img src='http://www.website.com/pix/logo.jpg'></td></tr>
</table>
<table style='padding-left:20px'>
<tr><td> </td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>FirstName : 	$firstname</font></td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>LastName : $lastname</font></td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>Email: $email</font></td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>Country: $country</font></td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>Telephone: $telephone</font></td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>Message: $message</font></font></td></tr>
<tr><td> </td></tr>
</table>
<table>
<tr><td><font style='font-size: 10px' style='color: #336699' style='font-family: Tahoma, Arial'>This message, and any attachments, is intended only for the use of the individual or entity to which it is addressed, and may contain confidential, proprietary and/or privileged information that is exempt from disclosure under applicable law which is not waived or lost by any transmission failure. 
If the reader of this message is not the intended recipient or its employee, or agent responsible for delivering the message to the intended recipient, you are hereby notified that any use, dissemination, distribution or copying of this communication and any attachments hereto is strictly prohibited. 
If you have received this communication in error, please destroy this message. 
Any views expressed in this message are those of the individual sender, except where the message states otherwise and the sender is authorised to state them to be the views of another person or entity. 
</font></td></tr>
</table>
</body>
</html>";
mail($to, $subject, $MsgBody, $MsgHeader);
?>

<?

$to = "$email";
$subject = "$firstname Confirmation Of Your Inquiry At WEBSITE NAME";
$MsgHeader = "From: WEBSITE NAME<noreply@website.com>\n";
$MsgHeader .= "MIME-Version: 1.0\n";
$MsgHeader .= "Content-type: text/html; charset=iso-8859-1";
$MsgBody = "
<html>
<head>
<title>$firstname Your Inquiry</title>
</head>
<body>

<table style='padding-left:0px' style='padding-top:0px'>
<tr><td align='left'><img src='http://www.website.com/pix/apalogo.jpg'></td></tr>
</table>
<table style='margin-left:12px'>  
<tr><td> </td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>Dear $firstname $lastname,</font></td></tr>
<tr><td> </td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>Your inquiry has been received by WEBSITE NAME.</font></td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>A representative shall be in contact with you shortly to deal with your inquiry.</font></td></tr>
<tr><td> </td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>Best Regards</font></td></tr>
<tr><td> </td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>WEBSITE NAME Team</font></td></tr>
<tr><td> </td></tr>
<tr><td><font style='font-size: 11px' style='font-family: Tahoma, Arial'>This message has been automatically generated please do not reply.</font></td></tr>
<tr><td> </td></tr>
<tr><td> </td></tr>
</table>
<table style='margin-left:12px' style='margin-right:250px'>
<tr><td><font style='font-size: 10px' style='color: #336699' style='font-family: Tahoma, Arial'>This message, and any attachments, is intended only for the use of the individual or entity to which it is addressed, and may contain confidential, proprietary and/or privileged information that is exempt from disclosure under applicable law which is not waived or lost by any transmission failure. 
If the reader of this message is not the intended recipient or its employee, or agent responsible for delivering the message to the intended recipient, you are hereby notified that any use, dissemination, distribution or copying of this communication and any attachments hereto is strictly prohibited. 
If you have received this communication in error, please destroy this message. 
Any views expressed in this message are those of the individual sender, except where the message states otherwise and the sender is authorised to state them to be the views of another person or entity.</font></td></tr> 
</table>
</body>
</html>";
mail($to, $subject, $MsgBody, $MsgHeader);


exit;



?>	

Link to comment
Share on other sites

so, this is the code I'll need to be using right?

 

<?php

$contactfield1=$_POST["contactfield1"]; //or $_GET, whichever works
$contactfield2=$_POST["contactfield2"]; // ---------"ditto"----------
$contactfield3=$_POST["contactfield3"]; // ---------"ditto"----------
$contactfield4=$_POST["contactfield4"]; // ---------"ditto"----------

$email_from = "";
$email_subject =  "" ;
//$email_txt = "";
$email_to = "drtech001@gmail.com";

$email_message = "field1: $contactfield1\n
field2 :$contactfield2 \n
field3 :$contactfield3 \n
field4 : $contactfield4 \n

";

  mail($email_to, $email_subject, $email_message, $email_from);


?>

 

If all's good ... then

 

@deeem

 

I was thinking the same thing, that the contactfields are probably hardcoded to the software, so those can't be changed... only the variables assigned to them can be changed. As I had said, I just followed some sample form scripts online and modified accordingly, but it does make sense to leave them as is, as there's no harm done.

 

I guess the code was right since you left most of it intact, except for the missing semi-colons ... missed that! I'll paste the code and test it. Thx deeem.

 

@shadiadiph

 

Woooah! That's one helluva long script .... altho I notice a lot of HTML tagging in there. But I'll keep it and go over it ... maybe use it when I'm not using this software or using a more scripted way of building future websites.

 

@farkewie

 

Thx 4 the input.

Link to comment
Share on other sites

Hi...

I have a similar problem.  I have a form created in html but can't seem to get the php script to work.

Please help... I'm new to this, so be gentle.  ;)

 

Here's the html form,

 

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

        <table style="margin-bottom: 0pt;" cellspacing="0">

          <tbody>

            <tr>

              <td width="150">Name<span class="required"> *</span></td>

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

            </tr>

            <tr>

              <td>Email<span class="required"> *</span></td>

              <td><input name="Email" type="text"></td>

            </tr>

            <tr>

              <td>City</td>

              <td><input name="City" type="text"></td>

            </tr>

            <tr>

              <td>State</td>

              <td><input name="State" type="text"></td>

            </tr>

            <tr>

              <td>Phone<span class="required"> *</span></td>

              <td><input name="Phone" size="12"

maxlength="12" type="text"></td>

            </tr>

            <tr>

              <td>Message<span class="required"> *<br>

              <br>

              <br>

              <br>

              <br>

              </span></td>

              <td><textarea name="Message" cols="40"

rows="6"></textarea></td>

            </tr>

            <tr>

              <td><input name="Submit" value="Submit"

type="submit"></td>

              <td> </td>

            </tr>

          </tbody>

        </table>

      </form>

 

...and the php

 

<?php

if(isset($_POST['submit'])) {

 

$to = "you@yourdomain.net";

$subject = "Contact from website";

$name_field = $_POST['name'];

$email_field = $_POST['email'];

$city_field = $_POST['city'];

$state_field = $_POST['state'];

$phone_field = $_POST['phone'];

$message = $_POST['message'];

 

$body = "From: $name_field \n E-Mail: $email_field \n City: $city_field \n State: $state_field \n Phone: $phone_field \n  Message:  Message: \n $message";

 

echo "Data has been submitted to $to!";

mail($to, $subject, $body);

 

} else {

 

echo "blarg!";

 

}

?>

Link to comment
Share on other sites

I made a couple of changes based on conrad's script...  I still get an error when trying to submit the form.

The error shows up as...

 

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home8/zomglawl/public_html/ericgruber.net/mailer.php on line 9

 

Parse error: syntax error, unexpected T_IF in /home8/zomglawl/public_html/ericgruber.net/mailer.php on line 10

 

 

Here's the script as of now.

 

<?php

if(isset($_POST["submit"])) {

 

$to = "you@yourdomane.net";

$subject = "Contact from website";

 

$name_field = $_POST[ "name"];

$email_field = $_POST[ "email"];

$city_field = $_POST[ "city"];

$state_field = $_POST[ "state"];

$phone_field = $_POST[ "phone"];

$message = $_POST[ "message"];

 

$body = "From: $name_field \n

E-Mail: $email_field \n

City: $city_field \n

State: $state_field \n

Phone: $phone_field \n 

Message:  Message: $message \n

 

";

 

echo "Data has been submitted to $to!";

mail($to, $subject, $body);

 

} else {

 

echo "blarg!";

 

}

?>

 

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.