Emailing Using PHP

Emailing Using PHP

Page 1 of 1

6 Replies - 11700 Views - Last Post: 11 March 2007 - 03:10 AM Rate Topic: -----

#1 maccer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-June 05

Emailing Using PHP

Posted 08 June 2005 - 09:59 AM

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:

<?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.

Is This A Good Question/Topic? 0
  • +

Replies To: Emailing Using PHP

#2 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1865
  • View blog
  • Posts: 20,278
  • Joined: 17-March 01

Re: Emailing Using PHP

Posted 08 June 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?
Was This Post Helpful? 0
  • +
  • -

#3 maccer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-June 05

Re: Emailing Using PHP

Posted 08 June 2005 - 10:16 AM

skyhawk133, on Jun 8 2005, 10:12 AM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#4 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1865
  • View blog
  • Posts: 20,278
  • Joined: 17-March 01

Re: Emailing Using PHP

Posted 08 June 2005 - 10:21 AM

Try doing this:

<?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.
Was This Post Helpful? 0
  • +
  • -

#5 maccer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-June 05

Re: Emailing Using PHP

Posted 09 June 2005 - 01:21 AM

skyhawk133, on Jun 8 2005, 10:21 AM, said:

Try doing this:

<?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.
Was This Post Helpful? 0
  • +
  • -

#6 RyanRyan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-March 07

Re: Emailing Using PHP

Posted 11 March 2007 - 02:17 AM

I midfied the first php script up there and I made it to
<?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
<!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 March 2007 - 02:19 AM

Was This Post Helpful? 0
  • +
  • -

#7 Styx  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 192
  • Joined: 04-March 07

Re: Emailing Using PHP

Posted 11 March 2007 - 03:10 AM

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1