?
Solved

Why doesn't this email script work?

Posted on 2011-03-09
4
Medium Priority
?
261 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 664 total points
ID: 35088666
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 668 total points
ID: 35088770
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 39

Assisted Solution

by:Aaron Tomosky
Aaron Tomosky earned 668 total points
ID: 35090825
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
ID: 35093841
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

840 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