PHP n00b

the mail() function

Moderators: macek, egami, gesf

pacenvtechinc
New php-forum User
New php-forum User
Posts: 1
Joined: Thu May 24, 2012 10:12 am

PHP n00b

Postby pacenvtechinc » Thu May 24, 2012 10:56 am

First off, I'm not a web designer -- he's on vacation -- and I've been tasked with making a really simple modification to the mailer. Basically, we have a form on a web page with ~24ish objects (fields, checkboxes, etc). When the form is posted and mailed, a hard-coded subject is added to the email, and I want to modify it to include values from a couple of fields on the form. See pseudocode below for a better idea and current mailer code below that. There's a [computer] language barrier for me, dunno how to translate my pseudocode into PHP. Any help would be greatly appreciated.

Ex. Form:
field.name forename
field.name surname
field.name company
field.name address, city, state, zip
field.name phone
field.name email
submit

Pseudo:

Code: Select all

'decs
dim to as string
dim ip as string
dim subj as string
dim msg as string
dim br as string

'defs
to = "user@example.tld.cc"
ip = $_SERVER['REMOTE_ADDR'];
br = chr(13) 'carriage return/linefeed

'parser
for each field in form.objects
  if field <> "" then 'skip empty fields
    msg = msg & field.name & ": " & field.value & br
    if field.name = "forename" or field.name = "surname" or field.name = "company" then 'add to subj
      subj = subj & field.value & " "
    end if
  end if
next field

'add date/timestamp & ip
msg = "RFQ Below received at: " & date & " " & time & "from IP: " & ip & br & br & msg
subj = "PCA RFQ from " & subj

'mail
mail (to, subj, msg)
header ("Location: www.example.com/contact/thankyou.html")


Current:

Code: Select all

<?PHP
$to = "user@example.tld.cc";
$subj = "";

$date = date ("l, F jS, Y");
$time = date ("h:i A");

$ip = $_SERVER['REMOTE_ADDR'];
$msg = "";

foreach ($_POST as $key => $value) {if (!(empty($value))) {$msg .= $key . ": " . $value . "\n";}}
if (!(empty($msg))) {
   $msg = "RFQ Below received at: " . $date . " at " . $time . " from " . $ip . "\n\n" . $msg;
   mail ($to, $subj, $msg);
   header ("Location: http://www.example.com/contact/thankyou.html");}
?>

User avatar
ejsexton82
New php-forum User
New php-forum User
Posts: 86
Joined: Mon Jun 04, 2012 10:05 pm
Location: Ankara, Turkey
Contact:

Re: PHP n00b

Postby ejsexton82 » Tue Jun 05, 2012 10:33 am

The form field values are stored in the $_POST variable. You probably see the field names in the email which is sent out, which probably looks something like this:

Code: Select all

RFQ Below received at: Tuesday, June 5th at 2012 at 9:32 PM from 127.0.0.1

forename: field value
surname: field value
company: field value
...


So all you would have to do is pick out the names of the fields, pull the values from the $_POST variable, and shove them into the $subj variable:

Code: Select all

$subj = $_POST['forename'] . ' ' . $_POST['surname'] . ' at ' . $_POST['company'];


Return to “PHP coding => Mail”

Who is online

Users browsing this forum: No registered users and 2 guests

cron