Solved

CAn't get this really simple script to work

Posted on 2008-06-17
2
178 Views
Last Modified: 2013-12-12
I have this php code that im using.  The goal is to create an input box where people can send their email address.  

once the user clicks on the submit button, the email that was put into the input box would be sent to an email address of my choosing

<?PHP
 
if ( isset ( $_POST['submit'] ) )
{
 
        $user_email = $_POST['email'];
        
        $to = 'michael@djmichaelray.com'; // your email address here
        $subject = 'New sign up'; // change this as desired
        $message = 'A New user has signed up, email address: ' . $user_email; // change this as desired
        
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= 'From: no-reply@yourdomain.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
                                
        mail ( $to, $subject, $message, $headers );
        
        $to = $user_email;
        $subject = 'no-reply@yourdomain.com'; // yourdomain here
        $message = 'Thank you for signing up to yourdomain.com'; // change this as desired
        
        mail ( $to, $subject, $message, $headers );
 
}
 
?>
 
<form action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post">
        <input type="text" name="email" /><input type="submit" value="sign up" />
</form>

Open in new window

0
Comment
Question by:djmichaelray
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 18

Accepted Solution

by:
Matthew Kelly earned 500 total points
ID: 21809245
If you do not have a email server running on the machine running PHP, mail will not work.

Is the problem with the email being sent?

If so, the best approach is to use a 3rd party mail program. In the past I have used PHPMailer

http://sourceforge.net/projects/phpmailer

Below is the PHP code to use it. Place class.phpmailer.php into your php includes directory.

Replace text in << >> with your server information: http://www.emailaddressmanager.com/tips/mail-settings.html
<?php
 
require("class.phpmailer.php");
 
$mail = new PHPMailer();
 
$mail->IsSMTP(); // send via SMTP
$mail->Host     = "<<smtpserver>>:<<stmpport>>"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "<<uname>>"; // SMTP username
$mail->Password = "<<pword>>"; // SMTP password
$mail->From = "<<email address from>>";
$mail->FromName = "<<email name from>>";
$mail->AddAddress("<<email address to>>"); 
$mail->AddReplyTo("<<email address from>>","<<email name from>>");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject  =  "Subject";
$mail->Body     =  "Body text";
$mail->AltBody  =  $mail->Body;
        
$mail->Send();
 
?>

Open in new window

0
 
LVL 14

Expert Comment

by:Vel Eous
ID: 21809273
My thought is it's either a server setup issue or being caught by a spam filter:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q__23493275.html
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

732 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