Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
tri

Sending Email - Script 8.10

Recommended Posts

Hi, I am using Xampp 3.2.2 and have written script 8.10 below (Page 228) but I just can´t get an e-mail. Everything looks fine but I get no e-mail at all. PIease help!

 

 

 

 

 

<?php // Script 8.10 - 19_register.php

/*This page lets people register for the site in theory*/

 

//Set the page title and include the header file:

define ('TITLE', 'Register');

include ('templates/19_header_v6.html');

 

//print some introductory text:

print '<div id = "leftcontent">

<h2>Registration Form</h2>

<p>Register so that you can take advantages of certain features like this, that and the other thing.</p>';

 

//Check whether the form has been submitted (handle form if):

if ($_SERVER['REQUEST_METHOD'] =='POST'){

 

$problem = false;

 

//Check for each value:

 

if (empty ($_POST['first_name'])){

$problem = TRUE;

print '<p class="text--error">Please enter your first name!</p>';

}

if (empty ($_POST['last_name'])){

$problem = TRUE;

print '<p class="text--error">Please enter your last name!</p>';

}

// Change the email validation(from 19_rigister_v11.php file) so that it also checks for a single "at" symbol:

 

if (empty ($_POST['email']) || (substr_count ($_POST['email'], '@')!=1)){

$problem = TRUE;

print '<p class="text--error">Please enter your email!</p>';

}

//Validate the password:

if (empty ($_POST['password1'])){

$problem = TRUE;

print '<p class="text--error">Please enter your password!</p>';

}

if ($_POST['password1'] != $_POST['password2']){

$problem = TRUE;

print '<p class="text--error">Your password did not match your confirmed password!</p>';

}

// Check whether a problem occured:

 

if (!$problem){

print '<p>You are now registered!<br />Okay, you are not really registered but ...</p>';

 

//Send the email:

 

$body = "Thank you, {$_POST['first_name']}, for registering with the J.D. Salinger fan club!";

mail($_POST['email'], 'Registration Confirmation', $body, 'From:me@example.com');

 

}else{ //Forgot a field

print '<p>Please try again!</>';

}

 

/*if (!problem){ // if there weren´t any problems ...

print '<p class ="text--success">You are now registered!<br />Okay, you are not really registerd but ...</p>';

//$_POST = [];

}else{//Forgot a field:

print '<p class = "text--error">Please try again!</p>';

}*/

 

} // End of handle form if: if ($_SERVER['REQUEST_METHOD'] =='POST'){

?>

 

<!--Display the form-->

 

<form action="19_register_v12.php" method="post" class="form--inline">

 

<!-- Create the sticky first name, last name and email inputs-->

 

<p><label for="first_name">First Name: </label><input type="text" name="first_name" size="20" value="<?php if (isset($_POST['first_name'])){

print htmlspecialchars($_POST['first_name']);

}?>"></p>

 

<p><label for="last_name">Last Name: </label><input type="text" name="last_name" size="20" value="<?php if (isset($_POST['last_name'])){

print htmlspecialchars($_POST['last_name']);

}?>"></p>

 

<p><label for="email">Email Address: </label><input type="email" name="email" size="20" value="<?php if (isset($_POST['email'])){

print htmlspecialchars($_POST['email']);

}?>"></p>

 

Password: <input type = "password" name = "password1" size = "20"/><br /><br />

Confirm Password: <input type = "password" name = "password2" size = "20" /><br /><br />

 

<p><input type="submit" name="submit" value="Register!" class="button--pill"></p>

 

</form>

 

<?php

print '</div>';

require ('templates/19_footer_v6.html');

?>

Share this post


Link to post
Share on other sites

What operating system are you using? And also make sure you use a real "from" email address as that can cause problems.

Share this post


Link to post
Share on other sites

Hi, Larry.

 

Sorry for my late feedback.

 

I am using Windows 7 and my e-mail from gmail  but I cann´t just the email. Please see below the last message I got:

 

Warning: mail(): SMTP server response: 553 We do not relay non-local mail, sorry. in C:\XAMPP\htdocs\19_register_v12.php on line 51

 

Thanks a lot,

 

AT.

Share this post


Link to post
Share on other sites

Ah, so this is relatively common on Windows. How critical is it for you to get this working? I don't normally bother when developing locally (because it is a big pain). If it's a must, I'd look at some of the solutions you'll find online:

 

https://www.google.com/search?q=php+windows+do+not+relay+non-local+mail&oq=php+windows+do+not+relay+non-local+mail&aqs=chrome..69i57j69i64.1831j0j7&sourceid=chrome&ie=UTF-8

Share this post


Link to post
Share on other sites

Larry,

I am also working on script 8.10 and received the following error when I tried to send the email: 

Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\CreatingWebAppsCh8\register.phpon line 44.

this is my line 44: mail($_POST['email'], 'Registration Confirmation', $body, 'From: admin@example.com');

I am running win10 on a 64bit system, xampp v3.2.2 and when I looked at the phpinfo.php file it was set for 

SERVER_PORT 80

but this is what the php.ini file has:

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP=localhost
; http://php.net/smtp-port
smtp_port=25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = me@example.com

I added the ini_set

 ini_set("smtp_port", 25);
mail($_POST['email'], 'Registration Confirmation', $body, 'From: admin@example.com');

and received the same error message.

I have looked at several different sources, including the php site and am still at a loss.

Share this post


Link to post
Share on other sites

I am not sure what I did but the previous issue is corrected, now I am getting the same error message at Tri, so I will consider this a completed task.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×