email subject

Status
Not open for further replies.

pauldf

New Member
Hi all

I was wondering if someone could help me with this. I have a php form script that handles the contact form on a website i have. The problem i have is that in my script the email subject is like this

$emailSubject = 'Contact Form';

Which means ( as i found out the annoying way ) that no matter who sends an email or how many is sent to me they all have the same thing in the subject field "Contact Form" , Everything else is fine Name etc.
On the form, i have a drop down list that people can choose what the reason for their email is ( info, help, about, other etc. ) , so what i was wondering, instead of having "Contact Form" appear in the subject field that arrives in my emails ( gmail ) can i get the whatever topic they choose from the list to appear in the subject field of my emails ( it would make life so much easier ) , and if i can could anyone tell me the code i'd need to change. I presume it's not as simple as changing

$emailSubject = 'Contact Form';
to
$emailSubject = 'list';

I would be really thankful for any help with this.

Paul
 

louie

New Member
you can grab the value of the select menu in your form and attache it to the subject line:
PHP:
$emailSubject = 'Contact Form'.(!empty($_POST['select_name']) ? " ". $_POST['select_name'] : "");
so that will show now as "Contact Form [Help, Info, or About]
 

php.allstar

New Member
Just to add to louie's solution, you should also take care to prevent possible email header injection which can lead to your form being used as a mailer for spam bots etc...

PHP:
function sanitize( $subject ) {
   return( str_ireplace(array(  "Content-Type:","to:","cc:", "bcc:", "\n", "\r", "%0d", "%0a"), "", $subject) );
}

$subject = sanitize($_POST['select_name']);

$emailSubject = 'Contact Form'.(!empty($subject) ? " ". $subject : "");
if you are using php less than php5, you'll have to use something like preg_replace as str_ireplace is a php5 only function, but lets hope you have php5 and I don't have to explain preg_repalce to you!
 

pauldf

New Member
Louie and php.allstar

You guys are legends, thanks a million for your help, it works like a charm now.

All the best

Paul
 
Status
Not open for further replies.
Top