Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,619 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,006 people online right now. Registration is fast and FREE... Join Now!




Emailing Using PHP

 
Reply to this topicStart new topic

Emailing Using PHP, Emailing Using PHP

maccer
post 8 Jun, 2005 - 08:59 AM
Post #1


New D.I.C Head

*
Joined: 8 Jun, 2005
Posts: 3

Hi, I am brand new to PHP having used JSP and JAVA for some time. I found a post on this site on how to send an email using PHP and it gave the following code:

CODE
<?php
$email_to = "guymcclintock@hotmail.com";
$email_subject = "Test E-Mail";
$email_body = "This is the body of the Email \nThis is a second line in the body!";
if(mail($email_to, $email_subject, $email_body))
{
   echo "The email($email_subject) was successfully sent.";
}
else
{
   echo "The email($email_subject) was NOT sent.";
}
?>

The problem is that it doesn't work for me. I am worried that I haven't set up my mail server anywhere. Does anyone know how to debug in PHP or if there is something obviously wrong with the above code?

Cheers.
User is offlineProfile CardPM

Go to the top of the page

skyhawk133
post 8 Jun, 2005 - 09:12 AM
Post #2


Head DIC Head

Group Icon
Joined: 17 Mar, 2001
Posts: 14,846



Thanked 45 times

Dream Kudos: 1650

Expert In: Web Development

My Contributions


If you have access to your php.ini file, you'll want to make sure the following is setup:

QUOTE
Runtime Configuration

The behaviour of these functions is affected by settings in php.ini.

Table 1. Mail configuration options
Name Default Changeable Changelog
SMTP "localhost" PHP_INI_ALL 
smtp_port "25" PHP_INI_ALL Available since PHP 4.3.0.
sendmail_from NULL PHP_INI_ALL 
sendmail_path NULL PHP_INI_SYSTEM 


Are you getting an error or just not getting the email?
User is offlineProfile CardPM

Go to the top of the page

maccer
post 8 Jun, 2005 - 09:16 AM
Post #3


New D.I.C Head

*
Joined: 8 Jun, 2005
Posts: 3

QUOTE(skyhawk133 @ Jun 8 2005, 10:12 AM)
If you have access to your php.ini file, you'll want to make sure the following is setup:

QUOTE
Runtime Configuration

The behaviour of these functions is affected by settings in php.ini.

Table 1. Mail configuration options
Name Default Changeable Changelog
SMTP "localhost" PHP_INI_ALL 
smtp_port "25" PHP_INI_ALL Available since PHP 4.3.0.
sendmail_from NULL PHP_INI_ALL 
sendmail_path NULL PHP_INI_SYSTEM 


Are you getting an error or just not getting the email?

I don't have a php.ini file so I assume that it resides on the web server that is running my php correct? If this is the case then it is with the free hosting company (t35.com) where my PHP is running. I can't see the web server so i don't know if there is an error, all i know is thet the mail(..) method must return false because the else part of the code I attached is output.
User is offlineProfile CardPM

Go to the top of the page

skyhawk133
post 8 Jun, 2005 - 09:21 AM
Post #4


Head DIC Head

Group Icon
Joined: 17 Mar, 2001
Posts: 14,846



Thanked 45 times

Dream Kudos: 1650

Expert In: Web Development

My Contributions


Try doing this:

CODE

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';

mail($to, $subject, $message);
?>


This is the simplest form of the mail tag, this should return an error if something is wrong. It is very possible that a free host would not provide SMTP information in the php.ini to prevent spamming and abuse.
User is offlineProfile CardPM

Go to the top of the page

maccer
post 9 Jun, 2005 - 12:21 AM
Post #5


New D.I.C Head

*
Joined: 8 Jun, 2005
Posts: 3

QUOTE(skyhawk133 @ Jun 8 2005, 10:21 AM)
Try doing this:

CODE

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';

mail($to, $subject, $message);
?>


This is the simplest form of the mail tag, this should return an error if something is wrong. It is very possible that a free host would not provide SMTP information in the php.ini to prevent spamming and abuse.

Thanks for that. When you say it should return an error, again I assume this is not returned by the call to the mail(..) function or is it? If so I don't think I'll be able to see the error. I think I'll get on the t35 forum and find out if other people are seeing the same thing. Thanks for all your help, I'll let you know if I find a solution.
User is offlineProfile CardPM

Go to the top of the page

RyanRyan
post 11 Mar, 2007 - 01:17 AM
Post #6


New D.I.C Head

*
Joined: 11 Mar, 2007
Posts: 1


My Contributions


I midfied the first php script up there and I made it to
CODE
<?php
$thiername = $_POST["thiername"];
$thieremail = $_POST["thieremail"];
$body = $_POST["body"];
$email_to = "ryan@****uin.com";
$email_subject = "A message from ***.com users";
$email_body = "$thiername <br/> $thieremail <br/> $body";
if(mail($email_to, $email_subject, $email_body))
{
   echo "The information was successfully sent.";
}
else
{
   echo "The information was NOT sent.";
}
?>
Then I hooked it up to an index.html page here
CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"xml:lang="en-US">
<head>
<title></title>
</head>
<body>
<form method="post" action="action.php">
<input type="text" name="thiername" value="Your name" /> <br/>
<input type="text" name="thieremail" value="Your email" /> <br/>
<textarea name="body" cols="30" rows="10">Your message</textarea /> <br/>
<input type="submit" value="Send">
</form>
</body>
</html>
It worked exellent for me. Thank you for the script and any help.

This post has been edited by RyanRyan: 11 Mar, 2007 - 01:19 AM
User is offlineProfile CardPM

Go to the top of the page

Styx
post 11 Mar, 2007 - 02:10 AM
Post #7


D.I.C Head

Group Icon
Joined: 4 Mar, 2007
Posts: 192



Dream Kudos: 225
My Contributions


That form is very vulnerable to spam bot attacks. The original script was more of an example of how to use it than something that should actually be deployed.

You should use some form of validation for the fields you are allowing them to enter.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 03:18AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month