/    Sign up×
Community /Pin to ProfileBookmark

PHP contact form not sending email

Hi, all!

I have tried to get this contact form to work for a few days now and can not seem to find what is wrong with it. I am currently running out of hair. I am new at php and have only the experience of what I am reading online. Can anyone help me? I am trying to get a persons contact info and have them select as many checkboxes as they want and to know which checkboxes they chose. The form is redirecting to the thankyou.html page I created, but no email is being sent to me as the form owner, nor to the person entering the data. This form did work perfectly prior to putting in my checkboxes, but I can’t seem to figure out what is wrong with the scenario. This is the php code:

<?php
$mailObj = mail($mailTo,$mailSubject,$mailContent,$mailHeader , “-f<[email protected]>”);
$to = “[email protected]“;
$from = $_REQUEST[’email’] ;
$name = $_
REQUEST[‘firstname’] ;
$headers = “From: $email”;
$subject = “Web Contact Data”;

$fields = array();
$fields{“firstname”} = “First Name”;
$fields{“lastname”} = “Last Name”;
$fields{“email”} = “Email Address”;
$fields{“phone”} = “Phone Number”;
$fields{“address”} = “Address”;
$fields{“address2”} = “Address 2”;
$fields{“city”} = “City”;
$fields{“state”} = “State”;
$fields{“zip”} = “Zip Code”;

if(isset($_POST[‘raisefunds’])) echo ‘Help raise funds checked’;
if(isset($_
POST[‘hostfundraiser’])) echo ‘Host a fund raiser checked’;
if(isset($_POST[‘hoandouts’])) echo ‘Help with literature checked’;
if(isset($_
POST[‘mailings’])) echo ‘Help with mailings checked’;
if(isset($_POST[‘doorknock’])) echo ‘Neighborhood walker checked’;
if(isset($_
POST[‘yardsign’])) echo ‘Display a yardsign checked’;
if(isset($_POST[‘headquarters’])) echo ‘Volunteer at headquarters checked’;
if(isset($_
POST[‘hostgettoknow’])) echo ‘Host a get to know checked’;
if(isset($_POST[‘eventsetup’])) echo ‘Help st up and clean up at event checked’;
if(isset($_
POST[‘eventparticipant’])) echo ‘Participate at an event checked’;
if(isset($_POST[‘internet’])) echo ‘Place a banner on my website checked’;
if(isset($_
POST[‘contribution’])) echo ‘Make a donation checked’;
if(isset($_POST[‘other’])) echo ‘Volunteer in some other way checked’;

$body = “We have received the following information:nn”; foreach($fields as $a => $b){ $body .= sprintf(“%20s: %sn”,$b,$_REQUEST[$a]); }

$headers2 = “From: [email][email protected][/email]“;
$subject2 = “Thank you for contacting us”;
$autoreply = “Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.logodesigngroup.com”;

if($email == ”) {print “You have not entered an email, please go back and try again”;}
else {
if($firstname == ”) {print “You have not entered a name, please go back and try again”;}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{header( “Location: http://www.logodesigngroup.com/thankyou.html” );}
else
{print “We encountered an error sending your mail, please notify [email][email protected][/email]“; }
}
}
?>

This is the corresponding html page code:
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
<title>Untitled Document</title>
</head>

<body>
<table width=”800″ height=”743″ border=”0″ cellpadding=”5″>
<tr>
<td height=”154″ colspan=”2″><p>Volunteers are the driving force behind all good campaigns. </p>
<p>If you are interested in helping Frank become the next sheriff of Charlotte County, then please complete the following form and select one or more volunteer categories.</p>
<p><strong><em>We appreciate your support!</em></strong></p></td>
</tr>
<tr>
<td colspan=”2″ valign=”top”><form id=”form1″ name=”form1″ method=”post” action=”volunteer.php”>
<p>
<label for=”firstname”>First Name:*</label>
<input name=”firstname” type=”text” id=”firstname” size=”30″ maxlength=”15″ />
<label for=”lastname”>Last Name:*
</label>
<input name=”lastname” type=”text” id=”lastname” size=”30″ maxlength=”20″ />
</p>
<p>
<label for=”email”>Email:</label>*
<input name=”email” type=”text” id=”email” size=”35″ maxlength=”35″ />
<label for=”phone”>Phone:*
</label>
<input name=”phone” type=”text” id=”phone” size=”34″ maxlength=”15″ />
</p>
<p>
<label for=”address”>Address:</label>
<input name=”address” type=”text” id=”address” size=”60″ maxlength=”30″ />
</p>
<p>
<label for=”address2″>Address 2:</label>
<input name=”address2″ type=”text” id=”address2″ size=”58″ maxlength=”30″ />
</p>
<p>
<label for=”city”>City:</label>
<input name=”city” type=”text” id=”city” size=”25″ maxlength=”20″ />
<label for=”state”>State:</label>
<input name=”state” type=”text” id=”state” size=”10″ maxlength=”4″ />
<label for=”zip”>Zip Code:</label>
<input name=”zip” type=”text” id=”zip” size=”10″ maxlength=”11″ />
</p>
<p>
<input type=”checkbox” name=”raisefunds” id=”raisefunds” />
<label for=”raisefunds”>Campaign Fund Raising – Help raise funds</label>
<br />
<input type=”checkbox” name=”hostfundraiser” id=”hostfundraiser” />
<label for=”hostfundraiser”>Campaign Fund Raising – Host a fundraiser at my home or business</label>
<br />
<input type=”checkbox” name=”handouts” id=”handouts” />
<label for=”handouts”>Promotion – Hand out literature</label>
<br />
<input type=”checkbox” name=”mailings” id=”mailings” />
<label for=”mailings”>Promotion – Help with mailings</label>
<br />
<input type=”checkbox” name=”doorknock” id=”doorknock” />
<label for=”doorknock”>Promotion – Neighborhood walker</label>
<br />
<input type=”checkbox” name=”yardsign” id=”yardsign” />
<label for=”yardsign”>Promotion – Display a yard sign at my home or business</label>
<br />
<input type=”checkbox” name=”headquarters” id=”headquarters” />
<label for=”headquarters”>Promotion – Volunteer at headquarters</label>
<br />
<input type=”checkbox” name=”hostgettoknow” id=”hostgettoknow” />
<label for=”hostgettoknow”>Events – Host a &quot;Get to know candidate&quot; event at my home or business</label>
<br />
<input type=”checkbox” name=”eventsetup” id=”eventsetup” />
<label for=”eventsetup”>Events – Help set up and clean up at campaign events</label>
<br />
<input type=”checkbox” name=”eventparticipant” id=”eventparticipant” />
<label for=”eventparticipant”>Events – Participate at events</label>
<br />
<input type=”checkbox” name=”internet” id=”internet” />
<label for=”internet”>Internet – Place a banner on my website</label>
<br />
<input type=”checkbox” name=”contribution” id=”contribution” />
<label for=”contribution”>Contribution – I would like to make a donation to the campaign</label>
<br />
<input type=”checkbox” name=”other” id=”other” />
<label for=”other”>Other – I would like to volunteer in some other way</label>
</p>
<p>
<input type=”submit” name=”submit” id=”submit” value=”Volunteer” />
</p>
<p>&nbsp; </p>
<p>&nbsp;</p>
</form></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@ahelperJun 16.2012 — I'm not sure what the original code looked like but some problems immediately jump out at me:

[LIST]
  • [*]The first line attempts to send an e-mail without defining any of the variables. PHP is probably complaining about that.

  • [*]The 'if(isset($_POST['raisefunds'])) echo 'Help raise funds checked';' lines are outputting content before the header() line is executed. I'm pretty sure you don't intend to do that. You should be appending the information to the $body. The easiest way to do that is do 'if (isset($_POST['raisefunds'])) $fields{"raisefunds"} = "Help raise funds";'.

  • [*]I'm pretty sure you need a 'value' attribute for the checkboxes. It might default to a value of 'on' but I think I've also seen some browser not send the information in the first place even though it was checked because there was no 'value'. I'd error on the side of caution.

  • [*]Error messages are being output before the 'html' tag. This will cause all sorts of problems. Save the error message to a variable and display it wherever it makes the most sense.

  • [/LIST]


    Beyond that, the PHP mail() function is not very reliable in this day and age of spam filters. You should also consider saving the information to a MySQL database so, in the event you don't get an e-mail, it is at least able to be viewed later on.

    If you find your e-mails are getting blocked by spam filters, I recommend the excellent software over here:

    http://barebonescms.com/documentation/ultimate_email_toolkit/

    As you mentioned, mail() seems to work for you, so there's no reason to replace it right away. Fix the existing problems I listed earlier.
    ×

    Success!

    Help @x54x25 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.3,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...