Solved

Why doesn't this email script work?

Posted on 2011-03-09
4
233 Views
Last Modified: 2012-05-11
Here's my code:

$email_address = trim($_POST['email']);
$name = trim($_POST['name']);
$phone = trim($_POST['phone']);
$time_frame = trim($_POST['time_frame']);
$underground = "christopher@undergroundtreehouse.com";

$to  = $email . ', '; // note the comma
$to .= 'christopher@undergroundtreehouse.com';
$subject = "The Underground Treehouse Demo Service";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <christopher@undergroundtreehouse.com>' . "\r\n";
$subject = "The Underground Treehouse";
$message .= "<b>Thank you for contacting the \"Underground Treehouse!\"</b><P>You will be contacted shortly.<P>Thanks again!";
mail($to, $subject , $message, $headers);

I don't know how to test it without sending multiple emails (which I have been doing), but I'm hoping some ninja can look at this and identify a problem that I currently can't see.

Thoughts?
0
Comment
Question by:brucegust
4 Comments
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 166 total points
Comment Utility
You don't actually say what is wrong with the mail. "Doesn't work" is not very helpful in diagnosis.

Having said that....

1) You have "$subject = " on two lines, so one wipes the other

2) $headers .= 'From: <christopher@undergroundtreehouse.com>' . "\r\n"; should not have < and > in there

3) None of your data is cleaned to prevent abuse. I could use this script as a mail relay by simply setting $_POST['email'] appropriately.

4) Your HTML is badly formed. The <p> tags have no closures </p>

But none of those will stop it from working
0
 
LVL 7

Accepted Solution

by:
lexlythius earned 167 total points
Comment Utility
Instead of discarding mail function's return value, print it:

$res = mail($to, $subject , $message, $headers);
var_dump($res);

Open in new window


If it prints false, you have a problem with the mail syntax.

If it prints true, PHP forwarded your mail to the system's Mail Transport Agent and problem is elsewhere (firewall, PHP configuration, MTA configuration, OS configuration, etc).
0
 
LVL 38

Assisted Solution

by:Aaron Tomosky
Aaron Tomosky earned 167 total points
Comment Utility
At se point you may want to look at phpmailer. Free. Awesome. Attachments. And best of all.... Use any smut server you want. It comes with examples to pull email addresses from a db too.
0
 

Author Comment

by:brucegust
Comment Utility
Guys, as it turns out the script does work. Because I'm on a shared server, it just took longer than I expected to arrive.

However, every one of these insights is valuable so I'm going to give all three of you points.

Thanks for weighing in!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now