Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKelvin Atawura
Front End Web Development Techdegree Student 19,022 Pointsphp form not sending
l have recorded my own php to send my emails on my site but there seem to be a bug in it and its not sending. Can you have a look at it for me guys thanks
<?php
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$firstName = clean_string($_POST['firstName']);
$secondName = clean_string($_POST['secondName']);
$firstLine = clean_string($_POST['first_line']);
$secondLine = clean_string($_POST['second_line']);
$postCode = clean_string($_POST['postCode']);
$postTown = clean_string($_POST['post_town']);
$email = clean_string($_POST['email']);
$tel = clean_string($_POST['telephone']);
$propertyType = clean_string($_POST['propertyType']);
$reasonForSale = clean_string($_POST['reasonForSale']);
$value = clean_string($_POST['value']);
$mortgage = clean_string($_POST['mortgage']);
$email_title = $name . " has filled in a form on Home House Buyers Website";
$email_message .= "First name: ".$firstName."\n";
$email_message .= "Second name: ".$secondName."\n";
$email_message .= "Address 1: ".$firstLine."\n";
$email_message .= "Address 2: ".$secondLine."\n";
$email_message .= "Post Code: ".$postCode."\n";
$email_message .= "Town:".$postTown."\n";
$email_message .= "Email:".$email."\n";
$email_message .= "Property type:".$propertyType."\n";
$email_message .= "Reason For Sale:".$reasonForSale."\n";
$email_message .= "value of current property:".$value."\n";
$email_message .= "Total of mortgages & secured loans: ".$mortgage."\n\n";
require("_postmark.php");
$postmark = new Postmark( "7ba8b2a4-d182-4021-9daa-316b6c336128", "kelvin@bluestone98.com", $email );
$result = $postmark->to('support@wpblogcare.com')
->subject($email_title)
->plain_message($email_message)
->send();
if($result):
echo json_encode( array('success' => true) );
else:
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail("kelvin@bluestone98.com", "Problem Sending Email With Postmark", $email_message, $headers);
endif;
?>
and the html for the code is as below
<form name="contactform" method="post" id="form" action="<?php echo get_template_directory_uri(); ?>/_form_to_email.php" class="cmxform" >
<div class="article">
<div class="" id="lookup_field" class="form-group">
</div>
<ul>
<li>
<div class="left-col">
<input name="firstName" id="cname" type="text" class="input" placeholder="First Name" title="Please enter your firstname" required/>
</div>
<div class="right-col">
<input name="lastName" id="cname" type="text" class="input" placeholder="Last Name" title="Please enter your lastname" required/>
</div>
</li>
<!-- postcode finder -->
<li>
<div class="left-col">
<input name="first_line" id="first_line" type="text" class="input" placeholder="Address line 1" title="Please enter the first line of your address" required />
</div>
<div class="right-col">
<input name="second_line" type="text" id="second_line" class="input" placeholder="Address line 2" title="Please enter the second line of your address" required/>
</div>
</li>
<li>
<div class="left-col">
<input name="postCode" id="postcode" type="text" class="input" placeholder="Postcode" title="Please enter your postcode" required />
</div>
<div class="right-col">
<input name="post_town" id="post_town" type="text" class="input" placeholder="Town" title="Please enter the name of your town" required/>
</div>
</li>
<!-- postcode finder -->
<li>
<div class="left-col">
<input name="email" id="email" type="email" class="input" placeholder="Email" required />
</div>
<div class="right-col">
<input name="telephone" type="text" class="input" placeholder="Telephone" title="Please provide a phone number in the following format: 0000-000-0000" required />
</div>
</li>
<li>
<div class="left-col">
<select name="propertyType" class="select">
<option value="Drop nav" selected>Property Type</option>
<option value="Detached">Detached</option>
<option value="Semi Detached">Semi Detached</option>
<option value="Flat">Flat</option>
<option value="Terrace">Terrace</option>
<option value="Land">Land</option>
<option value="Commercial">Commercial</option>
<option value="End of Terrace">End of Terrace</option>
<option value="Terrace">Terrace</option>
</select>
</div>
<div class="right-col">
<select name="reasonForSale" class="select">
<option value="Drop nav" selected>Reason for Sale</option>
<option value="Relocation">Relocation</option>
<option value="Repossession">Repossession</option>
<option value="Debt">Debt</option>
<option value="Divorce">Divorce</option>
<option value="Moving">Moving</option>
<option value="Abroad">Abroad</option>
<option value="Inheritance">Inheritance</option>
<option value="Empty property">Empty Property</option>
<option value="Abroad">Abroad</option>
</select>
</div>
</li>
<li>
<div class="left-col">
<input name="value" type="text" class="input" placeholder="Current Property Value" required/>
</div>
<div class="right-col">
<input name="mortgage" type="text" placeholder="Total of mortgages & secured loans"class="input" required/>
</div>
</li>
<div class="right-col">
<input class="submit" type="submit" id="submit" value="Submit">
</div>
</li>
</ul>
<a href="#codepopup" class="btn hidden fancybox" id="hidden" type="submit" name="sendMessage" value="submit"></a>
</div>
</form>
Thanks for your help in advance. Hampton Paulk
Emma Willmann
Treehouse Project ReviewerJust checking...is this site on a live web server, or on your localhost server? If it's on the localhost, the email probably won't work.
1 Answer
thomascawthorn
22,986 PointsIf you're working locally, you might not receive emails, but this is not always the case.
Are you picking up any errors or are the emails just not sending?
p.s. If that is a secret api key, you may want to consider changing it now!
Stephen Little
8,312 PointsStephen Little
8,312 PointsI have never used postmark before and did some poking around is this what you are using?
https://github.com/Znarkus/postmark-php
If so maybe you need to look at your results variable again. you have ..
$postmark = new Postmark( "7ba8b2a4-d182-4021-9daa-316b6c336128", "kelvin@bluestone98.com", $email );
but on this site it looks like you should be doing this
$email = new Postmark\Mail($postmarkApiKey);
$email->from('address@example.com', 'Name')
Again just throwing something out there maybe was off base... hope it helps in some small way
stephen