/    Sign up×
Community /Pin to ProfileBookmark

PHP Contact process file

Hi guys,
I have been using the same contact form for all my sites but only recently noticed during tests (and from comments from one of my “clients”) that half of the time, emails dont go through.
I have just put this code in to my new site to test and the contact form processed fine, got me to my success page which then counted down and redirected me to the home page.
I did this twice and all seemed fine but I still havent had my test emails in my inbox.
This is the code I use:
HTML

[CODE]<form action=”processform.php” method=”post”>
<label for=”name”>Name</label><input type=”text” id=”name” name=”name” />
<label for=”email”>Email address</label><input type=”text” id=”email” name=”email” />
<label for=”topic”>Subject</label><input type=”text” id=”topic” name=”topic” />
<textarea id=”comments” name=”comments” rows=”5″ cols=”30″></textarea>

<button type=”submit” id=”sendmessage”>Send</button>
</form>[/CODE]

PHP file

[CODE]<?php
// Pick up the form data and assign it to variables
$name = $_POST[‘name’];
$email = $_POST[’email’];
$topic = $_POST[‘topic’];
$comments = $_POST[‘comments’];

// Build the email (replace the address in the $to section with your own)
$to = ‘my e-mail address’;
$subject = “New message: $topic”;
$message = “$name said: $comments”;
$headers = “From: $email”;

// Send the mail using PHPs mail() function
mail($to, $subject, $message, $headers);

// Redirect
header(“Location: success.html”);[/CODE]

And before you say it, I did have my e-mail address in the “to” bit, i’ve changed it for the purpose of this post. ?

Cheers, Adey

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 18.2011 — Have you checked your spam folder? (I'm not claiming that's necessarily the most likely problem, but it's the easiest one to check first. ? )
Copy linkTweet thisAlerts:
@djadejonesauthorJun 18.2011 — Yep, first I put my NHS email address in and sent a test message, nothing in inbox of junk mail, and secondly put my hotmail address in. Again, nothing in inbox or junk mail. Really puzzled!

Adey
Copy linkTweet thisAlerts:
@NogDogJun 18.2011 — One common problem that could be the issue here is that many servers only allow the From email address to be a valid email on the host. You may need to set it to your email on that server, then put the address supplied by the user in a "Reply-To:" header.
Copy linkTweet thisAlerts:
@djadejonesauthorJun 18.2011 — so amend

[CODE]// Build the email (replace the address in the $to section with your own)
$to = 'my e-mail address';
$subject = "New message: $topic";
$message = "$name said: $comments";
$headers = "From: $email";[/CODE]


to

[CODE]// Build the email (replace the address in the $to section with your own)
$to = 'my e-mail address';
$subject = "New message: $topic";
$message = "$name said: $comments";
$headers = "From: [email protected]";
$headers = "Reply-To: $email";[/CODE]



Edit: Just tried this, still nothing :-(
Copy linkTweet thisAlerts:
@NogDogJun 18.2011 — [code=php]
// Build the email (replace the address in the $to section with your own)
$to = 'my e-mail address';
$subject = "New message: $topic";
$message = "$name said: $comments";
$headers = "From: [email protected]";
$headers = "Reply-To: $email";
[/code]
[/quote]

You need to concatenate your headers (From and Reply-To), as right now the 2nd is overwriting the first:
[code=php]
// Build the email (replace the address in the $to section with your own)
$to = 'my e-mail address';
$subject = "New message: $topic";
$message = "$name said: $comments";
$headers = "From: [email protected]" . PHP_EOL . "Reply-To: $email";
[/code]

The PHP_EOL constant is generally the surest way to have them separated by either "rn" or "n" as appropriate depending on what platform you're on.
Copy linkTweet thisAlerts:
@djadejonesauthorJun 20.2011 — Have added

[CODE]$headers = "From: [email protected]" . PHP_EOL . "Reply-To: $email" n;[/CODE]

and all seems to be fine for now, thanks for your help NogDog :-)
Copy linkTweet thisAlerts:
@german805Aug 03.2012 — Hi guys,

I have been using the same contact form for all my sites but only recently noticed during tests (and from comments from one of my "clients") that half of the time, emails dont go through.

I have just put this code in to my new site to test and the contact form processed fine, got me to my success page which then counted down and redirected me to the home page.

I did this twice and all seemed fine but I still havent had my test emails in my inbox.

This is the code I use:

HTML
[CODE]<form action="processform.php" method="post">
<label for="name">Name</label><input type="text" id="name" name="name" />
<label for="email">Email address</label><input type="text" id="email" name="email" />
<label for="topic">Subject</label><input type="text" id="topic" name="topic" />
<textarea id="comments" name="comments" rows="5" cols="30"></textarea>

<button type="submit" id="sendmessage">Send</button>
</form>[/CODE]

PHP file
[CODE]<?php
// Pick up the form data and assign it to variables
$name = $_POST['name'];
$email = $_POST['email'];
$topic = $_POST['topic'];
$comments = $_POST['comments'];

// Build the email (replace the address in the $to section with your own)
$to = 'my e-mail address';
$subject = "New message: $topic";
$message = "$name said: $comments";
$headers = "From: $email";

// Send the mail using PHPs mail() function
mail($to, $subject, $message, $headers);

// Redirect
header("Location: success.html");[/CODE]


And before you say it, I did have my e-mail address in the "to" bit, i've changed it for the purpose of this post. ?

Cheers, Adey[/QUOTE]

Please help

How can i add a Thank You bar to this i try my way but it didn't work
×

Success!

Help @djadejones 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.20,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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