Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP Email Script not Sending Emails

Posted on 2011-02-24
4
Medium Priority
?
291 Views
Last Modified: 2012-08-13
Hi,

Just trying to get some very basic PHP code to work to send an email.

This is copied from the W3C schools website.
It doesn't work - no emails arrive - I don't get any failure and no error codes but no email either.
$to = "name@mydomain.co.uk";
$subject = "Enquiry from Website";
$from    = "info@mydomain.co.uk";
$message = "Hello you! This is a simple email message";

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: $from" . "\r\n";

mail($to,$subject,$message,$headers);

echo "Test eMail Sent";

Open in new window

0
Comment
Question by:Sean Clarke
  • 2
4 Comments
 
LVL 39

Expert Comment

by:Aaron Tomosky
ID: 34971308
Yourailserver has to be setup in php for this to work. Try phpmailer
0
 
LVL 17

Expert Comment

by:psimation
ID: 34971574
Examples of using PHPMailer class here: phpmailer.worxware.com/
Download it here: sourceforge.net/projects/phpmailer/
More examples/tutorials here: www.askapache.com/.../phpfreaks-eric-rosebrocks-phpmailer-tutorial.html

The built in PHP mail() function is dependent on a working smtp setup on your server, usually sendmail. Even if the smtp server is configured, there are hundreds of reasons why a mail using mail() may not reach it's intended target.

PHPMailer class allows you to utilize an existing smtp server ( like the one you get from your ISP or Hosting Provider).

You also have much much more control over your mail.
0
 
LVL 4

Expert Comment

by:racastillojr
ID: 34974595
$to = $_POST["to"];
$from = $_POST["from"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/html;charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding:7bit\r\n";
$header .= "From:". $from . "\r\n";
$mailsent = mail("user@email.com",$subject,$from,$message,$headers);

this code is pulling fields from a email form.
0
 
LVL 4

Accepted Solution

by:
racastillojr earned 2000 total points
ID: 34974636
This has to be sent from a server not local host to work
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

879 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