Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

Designating who a Form Email is From

the mail() function

Moderators: macek, egami, gesf

Designating who a Form Email is From

Postby rhender » Mon Feb 06, 2012 12:10 pm

Hello to all,

I have a contact form on my personal website.

When I receive emails from the contact form on my site... the Email From area shows my full hosting name.

Is there a way to change this with PHP?

Thank you,

EDIT: I just found this a couple of posts down...

$headers = 'From: ' . $_POST [ "from" ] . PHP_EOL ;
mail ( $to , $subject , $message , $headers );

I guess $headers is the part that I am missing... not sure what the PHP_EOL is... ??? i'm new, forgive me
rhender
New php-forum User
New php-forum User
 
Posts: 13
Joined: Thu Dec 01, 2011 11:36 am

Re: Designating who a Form Email is From

Postby egami » Mon Feb 06, 2012 1:38 pm

You don't need the EOL thing.. that's useless.

you can simply do..

$headers = "From: $_POST[email]\r\n";
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Designating who a Form Email is From

Postby rhender » Mon Feb 06, 2012 1:48 pm

Thank you egami!

That is just what I needed and it works great.

Thanks so much. I'm very happy :)
rhender
New php-forum User
New php-forum User
 
Posts: 13
Joined: Thu Dec 01, 2011 11:36 am

Re: Designating who a Form Email is From

Postby egami » Mon Feb 06, 2012 2:03 pm

I would however, encourage you to do some filtering and not use the direct input from the user

ie..
do some kind of regex to make sure it's a legit email address..


$email = trim(strip_tags($_POST['email'])); // a very basic way to make sure there's no injection
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Designating who a Form Email is From

Postby rhender » Mon Feb 06, 2012 2:58 pm

Here is the php code that I have in the <head> of my contact page.

Code: Select all
<?php

$hostUrl="myhostname";
$userName="my-db-username";
$password="my-password";

$connectID = mysql_connect($hostUrl, $userName, $password)
  or die ("Sorry, can't connect to database");

mysql_select_db("my-db-name", $connectID)
  or die ("Unable to select database");
   
mysql_close($connectID);

include ("_include-Functions/form_validation.php");

if (@$_POST['submitted']) {
   $first_name = @$_POST['first_name'];
  $last_name = @$_POST['last_name'];
  $email = @$_POST['email'];
  $msg = @$_POST['message'];
   
      // if magic quotes on, remove Magic Quotes effect
   if ( get_magic_quotes_gpc() ) { // no arg needed - returns 1 if magic quotes are on, else 0
      
    
     $first_name = stripslashes($first_name);
     $last_name = stripslashes($last_name);
     $email = stripslashes($email);
     $msg = stripslashes($msg);
   }
   

$error_msg=array(); 

$valid = verifyAlphaNum ($first_name);
if (!$valid){
$error_msg[]="First Name can only contain letters.";
}

$valid = verifyAlphaNum ($last_name);
if (!$valid){
$error_msg[]="Last Name can only contain letters.";
}

$valid = verifyEmail ($email);
if (!$valid){
$error_msg[]="Email must be a valid format (i.e. jimihendrix@gmail.com).  Check it and try again.";
}

$valid = verifyText ($msg);
if (!$valid){
$error_msg[]="Message can only contain letters, numbers and basic punctuation \" ' - ? ! ";
}
   
   
   $destination_email="my-email-address";
   $email_subject="Contact from my-website.com";
   $email_body = "$first_name\n$last_name\n$msg\n$email";
   $headers = "From: $_POST[email]\r\n";

 
    if  (!$error_msg) {
   mail ($destination_email, $email_subject, $email_body, $headers);
   header ('Location: thankyou.php');
   exit();
}   

}       

?>


...AND the form's code... within the HTML

Code: Select all
<div id="form">
       
           <form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
             
            <?php
          if ($error_msg) {
          echo "<ul>\n";
          foreach ($error_msg as $err) {
          echo "<li>".$err."</li>\n";
          }
          echo "</ul>\n";
          }
          ?>
           
            <label for="first_name"   >First Name*</label><br />
           
            <input name="first_name" type="text" size="20" id="first_name" value="<?php echo $first_name ?>" /><br />
             
            <label for="last_name">Last Name*</label><br />
             
            <input name="last_name" type="text" size="20" id="last_name" value="<?php echo $last_name ?>" /><br />
             
            <label for="email" >Email:</label><br />
             
            <input name="email" type="text" value="<?php echo $email ?>" /><br />
 
           <label for="message">Message:</label><br />
 
           <textarea name="message" rows="8" cols="30" id="form-input"><?php echo $msg ?></textarea><br />
 
          <input class="input" name="submitted" type="submit" value="Submit" />
            
          </form>
               
          <!--end form--></div>


The only problem is that it is not storing my contacts from the form in my database, as I hoped that it would.

Wondering if you, or someone here, wouldn't mind critiquing what I have going here for my contact form.

And if someone can tell from this why it is not being stored in the database, that would be incredible, but I don't know if you guys prefer to not have database questions here on the forum.
rhender
New php-forum User
New php-forum User
 
Posts: 13
Joined: Thu Dec 01, 2011 11:36 am


Return to PHP coding => Mail

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron