PHP Script does not send e-mail

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
Tech_Face
Forum Newbie
Posts: 3
Joined: Sun Aug 29, 2010 10:58 pm

PHP Script does not send e-mail

Post by Tech_Face »

Hey guys,

I'm a novice to the PHP world. I'm working on a web form on my site and I can get the code to post to my database but, i can't get the form e-mailed to me when a client fills it out and submits it. Here's the code

Code: Select all

<?php

ini_set ('display_errors', 1); //Display all errors.
error_reporting (E_ALL & ~ E_NOTICED); //Don't show notices.

// This page receives data from contact_info.htm.
// It will receive: title, name, email, response, comments, and submit.

//Adjust for registered_globals being off.

/* Subject and E-mail Variables */

$email_subject = 'Performance Training Registration';
$email_addresses = 'danieljolivas@gmail.com';


/* Gathers Data Variables */

// General Information

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$country = $_POST['country'];
$phone = $_POST['phone'];
$alt_phone = $_POST['alt_phone'];
$email = $_POST['email'];

// Fitness Information

$gender = $_POST['gender'];
$weight = $_POST['weight'];
$age = $_POST['age'];
$height_ft = $_POST['height_ft'];
$height_in = $_POST['height_in'];
$waist = $_POST['waist'];
$goal = $_POST['goal'];

// Do you belong to a gym?

$membership = $_POST['membership'];
$what_gym = $_POST['what_gym'];
$member_length = $_POST['member_length'];
$days_attend = $_POST['days_attend'];
$join_gym = $_POST['join_gym'];

// Are you an athlete?

$athlete = $_POST['athlete'];
$what_sport = $_POST['what_sport'];
$play_level = $_POST['play_level'];
$improve_what = $_POST['improve_what'];
$improve_other = $_POST['improve_other'];

// Why are you interested in Performance training?

$weight_loss = $_POST['weight_loss'];
$goal_weight = $_POST['goal_weight'];
$strength = $_POST['strength'];
$tone = $_POST['tone'];
$flexibility = $_POST['flexibility'];
$health_reasons = $_POST['health_reasons'];

// What Equipment will you have available (if any)?

$gym_membership = $_POST['gym_membership'];
$medicine_ball = $_POST['medicine_ball'];
$home_gym = $_POST['home_gym'];
$stability_ball = $_POST['stability_ball'];
$agility_ladder = $_POST['agility_ladder'];
$dumbbells = $_POST['dumbbells'];
$equip_other = $_POST['equip_other'];
$other_equipment = $_POST['other_equipment'];

// What is your cardio preference (if any)?

$c_walk = $_POST['c_walk'];
$c_run = $_POST['c_run'];
$c_hike = $_POST['c_hike'];
$c_skate = $_POST['c_skate'];
$c_bike = $_POST['c_bike'];
$c_swim = $_POST['c_swim'];
$c_stairs = $_POST['c_stairs'];
$exercise_history = $_POST['exercise_history'];
$devote_days = $_POST['devote_days'];
$devote_min = $_POST['devote_min'];

//Personal

$race = $_POST['race'];
$sleep = $_POST['sleep'];

// Medical Information

$med_exam = $_POST['med_exam'];
$heart = $_POST['heart'];
$lung = $_POST['lung'];
$digestive = $_POST['digestive'];
$diabetes = $_POST['diabetes'];
$prescriptions = $_POST['prescriptions'];
$drugs = $_POST['drugs'];
$asthma = $_POST['asthma'];
$inhaler_amount = $_POST['inhaler_amount'];

// Dietary Information

$breakfast = $_POST['breakfast'];
$meals = $_POST['meals'];
$fruit_veg = $_POST['fruit_veg'];
$fats = $_POST['fats'];
$processed_food = $_POST['processed_food'];
$alcohol = $_POST['alcohol'];

// Psychological Information

$happiness = $_POST['happiness'];
$depression = $_POST['depression'];
$anxiety = $_POST['anxiety'];
$relaxation = $_POST['relaxation'];
$marriage = $_POST['marriage'];
$job = $_POST['job'];
$comments = $_POST['comments'];

// PT Package Types

$high_school_training = $_POST['high_school_training'];
$target_training = $_POST['target_training'];
$pt1_training = $_POST['pt1_training'];
$professional_training = $_POST['professional_training'];
$elite_training = $_POST['elite_training'];

	$body = <<<EOD
<br><hr><br>
<strong> General Information </strong>
First Name: $first_name <br>
Last Name: $last_name <br>
Address: $address <br>
City: $city <br>
State: $state <br>
Zip: $zip <br>
Country: $country <br>
Phone: $phone <br>
Alt. Phone: $alt_phone <br>
Email: $email <br>
<br>
<strong>Fitness Information</strong>
<br>
Gender: $gender <br>
Weight: $weight <br>
Age: $age <br>
Height ft: $height_ft <br>
Height in: $height_in <br>
Waist: $waist <br>
Goal: $goal <br>
<br>
<strong>Do you belong to a gym?</strong>
<br>
What Gym: $what_gym <br>
Member Length: $member_length <br>
Days Attend: $days_attend <br>
Are You Willing to Join a Gym?: $join_gym <br>
<br>
<strong>Are you an athlete?</strong>
<br>
What Sport: $what_sport <br>
Play Level: $play_level <br>
Improve What: $improve_what <br>
Improve Other: $improve_other <br>
<br>
<strong>Why are you interested in Performance Training?</strong>
Weight Loss: $weight_loss <br>
Goal Weight: $goal_weight <br>
Strength: $strength <br>
Tone: $tone <br>
Flexibility: $flexibility <br>
Health Reasons: $health_reasons <br>
<br>
<strong>What equipment will you have available (If any)?</strong>
<br>
Gym Membership: $gym_membership <br>
Medicine Ball: $medicine_ball <br>
Home Gym: $home_gym <br>
Stability Ball: $stability_ball <br>
Agility Ladder: $agility_ladder <br>
Weights/dumbbells: $dumbbells <br>
Other: $equip_other <br>
Other Equipment: $other_equipment <br>
<br>
<strong>What is your cardio preference (if any)?</strong>
<br>
Walk: $c_walk<br>
Run: $c_run <br>
Hike: $c_hike <br>
Skate: $c_skate <br>
Bike: $c_bike <br>
Swim: $c_swim <br>
Stairs: $c_stairs <br>
<br>
<strong>Performance Training package</strong>
<br>
High School Package: $high_school_training <br>
Target Training Package: $target_training <br>
PT1 Training Package: $pt1_training <br>
Professional Package: $professional_training <br>
Elite Training Package: $elite_training <br>
<br>
<strong>Fitness Schedule</strong>
<br>
How Active Are you?: $exercise_history <br>
Devote Days: $devote_days <br>
Devote Min: $devote_min <br>
<br>
<strong>Personal Information</strong>
<br>
Race: $race <br>
Sleep: $sleep <br>
<br>
<strong>Medical Information</strong>
<br>
Medical Exams: $med_exam <br>
Heart: $heart <br>
Lung: $lung <br>
Digestive: $digestive <br>
Diabetes: $diabetes <br>
Drugs: $drugs <br>
Prescriptions: $prescriptions <br>
Asthma: $asthma <br>
Inhaler Amount: $inhaler_amount <br>
<br>
<strong>Dietary Information</strong>
<br>
Breakfast: $breakfast <br>
Meals: $meals <br>
Fruit and Vegetables: $fruit_veg <br>
Fats: $fats <br>
Processed Food: $processed_food <br>
Alcohol: $alcohol <br>
<br>
<strong>Psychological Information</strong>
<br>
Happiness: $happiness <br>
Depression: $depression <br>
Anxiety: $anxiety <br>
Relaxation: $relaxation <br>
Marriage: $marriage <br>
Job: $job <br>
Comments: $comments <br>
EOD;

	$headers = "From: $email\r\n";
	$headers .= "Content-type: text/html\r\n";
	$success = mail($email_addresses, $email_subject, $body, $headers);
Its weird because i have another table in the database that is somewhat similar to the previous code, except there is less of it and I get responses from the e-mails no problem. Its almost the same coding.

Code: Select all

<?php
ini_set ('display_errors', 1); //Display all errors.
error_reporting (E_ALL & ~ E_NOTICED); //Don't show notices.

// This page receives data from contact_info.htm.
// It will receive: name, phone, email, address, city, and submit.

//Adjust for registered_globals being off.

$email_subject = 'Performance Training Contact';
$email_addresses = 'danieljolivas@gmail.com';

//Tell Us More

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$phone = $_POST['phone'];
$phone_type_1 = $_POST['phone_type_1'];
$alt_phone = $_POST['alt_phone'];
$phone_type_2 = $_POST['phone_type_2'];
$occupation = $_POST['occupation'];
$email = $_POST['email'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$country = $_POST['country'];
$referral = $_POST['referral'];


//Who Am I

$fitness = $_POST['fitness'];
$sport = $_POST['sport'];
$level = $_POST['level'];
$contact = $_POST['contact'];
$comments = $_POST['comments'];


	$body = <<<EOD
<br><hr><br>
<strong>Tell Us More</strong> <br>
<br>
First Name: $first_name <br>
Last Name: $last_name <br>
Phone: $phone <br>
Phone Type: $phone_type_1 <br>
Alt. Phone: $alt_phone <br>
Phone Type: $phone_type_2 <br>
Occupation: $occupation <br>
Email: $email <br>
Address: $address <br>
City: $city <br>
Zip: $zip <br>
State: $state <br>
Country: $country <br>
Referral: $referral <br>
<br>
<strong>Who am I</strong> <br>
<br>
I Am A/An: $fitness <br>
My Sport Is: $sport<br>
Team/Level Preparing For: $level <br>
Contact Me By: $contact <br>
Comments: $comments <br>
EOD;

	$headers = "From: $email\r\n";
	$headers .= "Content-type: text/html\r\n";
	$success = mail($email_addresses, $email_subject, $body, $headers);
 
Any help would be appreciated
User avatar
phpcip28
Forum Newbie
Posts: 22
Joined: Sun Aug 29, 2010 1:38 pm
Location: NewYork
Contact:

Re: PHP Script does not send e-mail

Post by phpcip28 »

First of all don't use E_NOTICED, the correct, is E_NOTICE.

And regarding your email issue, try to use

Code: Select all

$headers = "From: You Name <{$email}>\r\n";

Code: Select all

$headers = "From: <{$email}> You Name\r\n";
Not sure exactly how it was if you had the name before the address or vice-versa...

Instead of the line you have for the FROM Headers.

Not sure this is the issue, but I know you should also include a Name in the from header.
Also there may be a lot other issues with sending out emails like this, whether your MTA is running or not, etc etc etc. But I assume all those are in place in your case.

Thanks and I hope this works.
Tech_Face
Forum Newbie
Posts: 3
Joined: Sun Aug 29, 2010 10:58 pm

Re: PHP Script does not send e-mail

Post by Tech_Face »

Thank you for your reply, although, i must of worded it wrong. This code is supposed to send me an e-mail of the web form they submitted on my site. The from headers is supposed to indicate the client's e-mail address, not mine. I've been constantly testing the code but, still no go.
User avatar
phpcip28
Forum Newbie
Posts: 22
Joined: Sun Aug 29, 2010 1:38 pm
Location: NewYork
Contact:

Re: PHP Script does not send e-mail

Post by phpcip28 »

Tech_Face wrote:Thank you for your reply, although, i must of worded it wrong. This code is supposed to send me an e-mail of the web form they submitted on my site. The from headers is supposed to indicate the client's e-mail address, not mine. I've been constantly testing the code but, still no go.
Well I guess it's time you check to see if you have any MTA present in your server. Otherwise you can't use the mail function.
jarofgreen
Forum Commoner
Posts: 71
Joined: Sun Jul 11, 2010 12:40 pm

Re: PHP Script does not send e-mail

Post by jarofgreen »

Who is your email provider, and do they have any spam controls on? 'Cos what you do, setting the From address as the user is going to trip them ("This email claims to come from gmail and really comes from a server at ineverheardofyou.com?") Use an email from your server as From and if you must, set the Reply-To header as their email.

Basically, Email sending is fraught with anti-spam traps you can so easily fall into.
Tech_Face
Forum Newbie
Posts: 3
Joined: Sun Aug 29, 2010 10:58 pm

Re: PHP Script does not send e-mail

Post by Tech_Face »

phpcip28 wrote:
Tech_Face wrote:Thank you for your reply, although, i must of worded it wrong. This code is supposed to send me an e-mail of the web form they submitted on my site. The from headers is supposed to indicate the client's e-mail address, not mine. I've been constantly testing the code but, still no go.
Well I guess it's time you check to see if you have any MTA present in your server. Otherwise you can't use the mail function.
I adjusted the code an alternate way and it worked for the e-mail address on my webserver but, not for the email provider email addresses, weird. Is there anyway I can adjust this in the code or do i have to set something in my email provider(gmail) settings?

Code: Select all

/* Gathers Data Variables */

// General Information

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$country = $_POST['country'];
$phone = $_POST['phone'];
$alt_phone = $_POST['alt_phone'];
$email = $_POST['email'];

// Fitness Information

$gender = $_POST['gender'];
$weight = $_POST['weight'];
$age = $_POST['age'];
$height_ft = $_POST['height_ft'];
$height_in = $_POST['height_in'];
$waist = $_POST['waist'];
$goal = $_POST['goal'];

// Do you belong to a gym?

$membership = $_POST['membership'];
$what_gym = $_POST['what_gym'];
$member_length = $_POST['member_length'];
$days_attend = $_POST['days_attend'];
$join_gym = $_POST['join_gym'];

// Are you an athlete?

$athlete = $_POST['athlete'];
$what_sport = $_POST['what_sport'];
$play_level = $_POST['play_level'];
$improve_what = $_POST['improve_what'];
$improve_other = $_POST['improve_other'];

// Why are you interested in Performance training?

$weight_loss = $_POST['weight_loss'];
$goal_weight = $_POST['goal_weight'];
$strength = $_POST['strength'];
$tone = $_POST['tone'];
$flexibility = $_POST['flexibility'];
$health_reasons = $_POST['health_reasons'];

// What Equipment will you have available (if any)?

$gym_membership = $_POST['gym_membership'];
$medicine_ball = $_POST['medicine_ball'];
$home_gym = $_POST['home_gym'];
$stability_ball = $_POST['stability_ball'];
$agility_ladder = $_POST['agility_ladder'];
$dumbbells = $_POST['dumbbells'];
$equip_other = $_POST['equip_other'];
$other_equipment = $_POST['other_equipment'];

// What is your cardio preference (if any)?

$c_walk = $_POST['c_walk'];
$c_run = $_POST['c_run'];
$c_hike = $_POST['c_hike'];
$c_skate = $_POST['c_skate'];
$c_bike = $_POST['c_bike'];
$c_swim = $_POST['c_swim'];
$c_stairs = $_POST['c_stairs'];
$exercise_history = $_POST['exercise_history'];
$devote_days = $_POST['devote_days'];
$devote_min = $_POST['devote_min'];

//Personal

$race = $_POST['race'];
$sleep = $_POST['sleep'];

// Medical Information

$med_exam = $_POST['med_exam'];
$heart = $_POST['heart'];
$lung = $_POST['lung'];
$digestive = $_POST['digestive'];
$diabetes = $_POST['diabetes'];
$prescriptions = $_POST['prescriptions'];
$drugs = $_POST['drugs'];
$asthma = $_POST['asthma'];
$inhaler_amount = $_POST['inhaler_amount'];

// Dietary Information

$breakfast = $_POST['breakfast'];
$meals = $_POST['meals'];
$fruit_veg = $_POST['fruit_veg'];
$fats = $_POST['fats'];
$processed_food = $_POST['processed_food'];
$alcohol = $_POST['alcohol'];

// Psychological Information

$happiness = $_POST['happiness'];
$depression = $_POST['depression'];
$anxiety = $_POST['anxiety'];
$relaxation = $_POST['relaxation'];
$marriage = $_POST['marriage'];
$job = $_POST['job'];
$comments = $_POST['comments'];

// PT Package Types

$high_school_training = $_POST['high_school_training'];
$target_training = $_POST['target_training'];
$pt1_training = $_POST['pt1_training'];
$professional_training = $_POST['professional_training'];
$elite_training = $_POST['elite_training'];

$message = "Fitness Registration

GENERAL INFORMATION
First Name: $first_name
Last Name: $last_name
Address: $address
City: $city
State: $state
Zip: $zip
Country: $country
Phone: $phone
Alt. Phone: $alt_phone
Email: $email

FITNESS INFORMATION
Gender: $gender
Weight: $weight
Age: $age
Height ft: $height_ft
Height in: $height_in
Waist: $waist
Goal: $goal

DO YOU BELONG TO A GYM?
What Gym: $what_gym
Member Length: $member_length
Days Attend: $days_attend
Are You Willing to Join a Gym?: $join_gym

ARE YOU AN ATHLETE?
What Sport: $what_sport
Play Level: $play_level
Improve What: $improve_what
Improve Other: $improve_other

WHY ARE YOU INTERESTED IN PERFORMANCE TRAINING?
Weight Loss: $weight_loss
Goal Weight: $goal_weight
Strength: $strength
Tone: $tone
Flexibility: $flexibility
Health Reasons: $health_reasons

WHAT EQUIPMENT WILL YOU HAVE AVAILABLE (IF ANY)?
Gym Membership: $gym_membership
Medicine Ball: $medicine_ball
Home Gym: $home_gym
Stability Ball: $stability_ball
Agility Ladder: $agility_ladder
Weights/dumbbells: $dumbbells
Other: $equip_other
Other Equipment: $other_equipment

WHAT IS YOUR CARDIO PREFERENCE (IF ANY)?
Walk: $c_walk
Run: $c_run
Hike: $c_hike
Skate: $c_skate
Bike: $c_bike
Swim: $c_swim
Stairs: $c_stairs

PERFORMANCE TRAINING PACKAGE
High School Package: $high_school_training
Target Training Package: $target_training
PT1 Training Package: $pt1_training
Professional Package: $professional_training
Elite Training Package: $elite_training

FITNESS SCHEDULE
How Active Are you?: $exercise_history
Devote Days: $devote_days
Devote Min: $devote_min

GENERAL HEALTH

PERSONAL
Race: $race
Sleep: $sleep

MEDICAL INFORMATION
Medical Exams: $med_exam
Heart: $heart
Lung: $lung
Digestive: $digestive
Diabetes: $diabetes
Drugs: $drugs
Prescriptions: $prescriptions
Asthma: $asthma
Inhaler Amount: $inhaler_amount

DIETARY INFORMATION
Breakfast: $breakfast
Meals: $meals
Fruit and Vegetables: $fruit_veg
Fats: $fats
Processed Food: $processed_food
Alcohol: $alcohol

PSYCHOLOGICAL INFORMATION
Happiness: $happiness
Depression: $depression
Anxiety: $anxiety
Relaxation: $relaxation
Marriage: $marriage
Job: $job
Comments: $comments
";

$from = "From: $email\r\n";

mail('danieljolivas@gmail.com , info@goperformance1.com , performancetraining24@gmail.com' , 'Performance Training Registration' , $message, $from);

/* Displays confirmation with message */
User avatar
phpcip28
Forum Newbie
Posts: 22
Joined: Sun Aug 29, 2010 1:38 pm
Location: NewYork
Contact:

Re: PHP Script does not send e-mail

Post by phpcip28 »

Might I suggest you use the phpmailer class ?
Here it is:
http://phpmailer.worxware.com/

You've got all you need in there, from examples, to documentation and all you need

Also I would recommend sending out SMTP Authenticated Mail with phpmailer.

If you read the docs, it's all there.

This really can't fail.
Post Reply