Mail Script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
take2hikes
Forum Newbie
Posts: 14
Joined: Sat Feb 14, 2009 1:45 pm

Mail Script

Post by take2hikes »

Hi everyone, I am new to PHP. I've never done anything and this will have been my first form script that I've 'modded'. I took bits and pieces, here and there, to make it work to my liking.

My one question is that the From name is put together in my inbox. So if a user enters "User Name" and email "email@email.com", I get the following:

UserName email@email.com <subject>

The following is the code I am using:

Code: Select all

         <?php
 
/* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
$recipient = 'me@gmail.com' ;
$subject = "FEEDBACK FROM T2H.COM" ;
$name = $HTTP_POST_VARS['name'];
$company = $HTTP_POST_VARS['company'];
$email = $HTTP_POST_VARS['email'];
$message = $HTTP_POST_VARS['message'];
 
// headers could be done in one line, but for clarity are separated
$mailheaders = "From: $name\r\n";
$mailheaders .= "Reply-To: $email\r\n";
$mailheaders .= "Return-Path: $email\r\n";
 
/* Makes message a bit neater */
$messageproper = "This message was sent from:\n" .
"Name: $name\n" .
"Email: $email\n" .
"Company: $company\n" .
"-----------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n" .
$message .
"\n\n-----------------------------------------------------------------------------------------------------------------------------------------------------------------\n" ;
 
 
/* PHP form validation: the script checks that the Email field contains a valid email address and the Subject field isn't empty. preg_match performs a regular expression match. It's a very powerful PHP function to validate form fields and other strings - see PHP manual for details. */
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
  echo "<p>Invalid email address! Please go back and try again.</p>";
  echo "<p><a href='javascript&#058;history.back(1);'>Back</a></p>";
  } elseif ($name == "") {
  echo "<p>No name! Please go back and try again.</p>";
  echo "<p><a href='javascript&#058;history.back(1);'>Back</a></p>";
  } elseif ($message == "") {
  echo "<p>You didn't enter a message! Please go back and try again.</p>";
  echo "<p><a href='javascript&#058;history.back(1);'>Back</a></p>";
}
 
/* If no erros, Sends the mail and outputs the "Thank you" string  */
else{
  mail($recipient, $subject, $messageproper, $mailheaders);
  echo "<p>Thanks for the feedback! If your message required a response, you can expect one within 24 hours.</p>";
}
?>
----

This is going to sound completely naive, but I have became rather fascinated by PHP. I'm wondering what other most common uses there are for it? From small personal pages to Corporations, I'd like to know it's implementations.

I'm fairly familiar with C and C++ although it's been a while, so feel free to be somewhat in-depth ( ha, dont need var explained ).

Thanks, guys!


.t2h
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Mail Script

Post by JAB Creations »

The mail function is exceptionally powerful though will of course only work on live servers (and not your localhost).

Since you're new to PHP the best advice I can give you is simply read php.net as it is exceptionally well documented. You should also see php.net's "see also..." bit at the end of explaining what the built-in functions do as there are various things that are similar though can effectively break your application if you attempt to use the wrong one.

There is an excellent form generator available there. Best to mess with already working code.

Also try this bit of code...trying is half the battle. :wink:

Code: Select all

<?php
$name = 'Example';
echo '<div><p>hi there Dr. $name</p></div>';
echo "<div><p>hi there Dr. $name</p></div>";
// single line comment
/*
multiple
line
comment
*/
?>
I also recommend commenting out stuff and seeing how to minimize the code that works until something breaks. By doing that you'll learn to figure out what's useful.

There are no doubt functions in C and C++? I presume you have messed with classes? Seriously I don't know what PHP can't do.

The best advice I can give you when posting on this forum...

1.) Use the [_php_] and [_/_php_] BB Code (without the underscores).

2.) Be as precise as you can about the question you have.

3.) Show you've made an effort at attempting to achieve and search for the issue.

Good luck and welcome to DevNetwork! :)
take2hikes
Forum Newbie
Posts: 14
Joined: Sat Feb 14, 2009 1:45 pm

Re: Mail Script

Post by take2hikes »

JAB,
Thank you very much for your reply. I will check out PHP.net for some more information.

The php script that I inserted into my previous post is fully functional, my only question was regarding the space in the "from" name in the actual email. For some reason it isn't separating the two values if the user enters a first and last name. I am however looking at PHP.net's documentation on the mail function, so we'll see if I can't figure it out.

Of the two echo's that you gave me, I have not tried them yet. I'll write back once I do, but right off the bat I wouldn't imagine the one wrapped in single ' would work. Unless PHP is different from the other languages I've used, "this text" denotes a string where '38493' denotes a value.

I have worked with classes with C and C++. It has been almost 4 years since I've done any programming. I'm getting out of the military in less than a year and once that goes through I plan on going to college for business and computer science. Just recently my passion for 1's and 0's has been re-fueled.

Anyways, Thanks for the tips! I look forward to using the forums!


.t2h
Post Reply