By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,728 Members | 1,143 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,728 IT Pros & Developers. It's quick & easy.

How to use the mail() function in php?

Nert
P: 64
hello every one, i need help..

how can i use the mail() funtion in php? when ever i execute the mail function i continuesly receive this error message:

Warning: mail() [function.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...
what do i need to do in order for me to connect with my localhost mailserver?

can anyone help me with this?
thanks in advance..(^_^)

--nert
Nov 21 '06 #1
Share this Question
Share on Google+
13 Replies


ronverdonk
Expert 2.5K+
P: 4,258
hello every one, i need help..

how can i use the mail() funtion in php? when ever i execute the mail function i continuesly receive this error message:



what do i need to do in order for me to connect with my localhost mailserver?

can anyone help me with this?
thanks in advance..(^_^)

--nert
Just do what the message says. Look in your PHP.INI for the SMTP, port and (If Windows) for the settings:
Expand|Select|Wrap|Line Numbers
  1. ; For Win32 only.
  2. SMTP = ??????
  3. smtp_port = 25
  4.  
  5. ; For Win32 only.
  6. sendmail_from = ???
  7.  
  8. ; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
  9. ;sendmail_path =
Ronald :cool:
Nov 21 '06 #2

P: 2
well i have a similar problem
i downloaded this code from a tutorial website, and save it on

c:\apache\htdocs\testsendemail\index.php


this is the code that i downloaded:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. //Check whether the submission is made
  4. if(isset($hidSubmit)){
  5.  
  6. //Declarate the necessary variables
  7. $mail_to=$txtEmailto;
  8. $mail_from=$txtEmailfrm;
  9. $mail_sub=$txtSub;
  10. $mail_mesg=$txtMsg;
  11.  
  12. //Check for success/failure of delivery
  13. if(mail($mail_to,$mail_sub,$mail_mesg,"From:$mail_from/r/nReply-to:$mail_from"))
  14. echo "<span class='textred'>E-mail has been sent successfully from $mail_sub to $mail_to</span>";
  15. else
  16. echo "<span class='textred'>Failed to send the E-mail from $mail_sub to $mail_to</span>";
  17. }
  18. ?>
  19.  
||||
||||
||||
||||
||||

this is the warning message that i received:

Warning: Failed to Connect in c:\apache\htdocs\testsendemail\index.php on line 13
Failed to send the E-mail from to


what should i do?
sorry i'm a total newbie here, just started to learn how to use php.
Nov 22 '06 #3

Nert
P: 64
hi,

i tried this stuff, but i still recieve the same error message.., am i missing something Sir?

Expand|Select|Wrap|Line Numbers
  1. ; For Win32 only.
  2. SMTP = localhost
  3. smtp_port = 25
  4.  
  5. ; For Win32 only.
  6. sendmail_from = localhost
  7.  
  8. ; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
  9. ;sendmail_path =
--nert
Nov 22 '06 #4

ronverdonk
Expert 2.5K+
P: 4,258
hi,

i tried this stuff, but i still recieve the same error message.., am i missing something Sir?

--nert
Does that mean that you have actually changed the PHP.INI settings to your own situation, saved it in C:\WINDOWS\ and it still does not run?
Then show us these lines you have made in PHP.INI.

Ronald :cool:
Nov 22 '06 #5

Nert
P: 64
Does that mean that you have actually changed the PHP.INI settings to your own situation, saved it in C:\WINDOWS\ and it still does not run?
Then show us these lines you have made in PHP.INI.

Ronald :cool:
hi,

i'm sorry sir this is just the first time i use the php mail function, and i don't have any idea about what configuration this function need in order to work.

is this right sir?(^_^)

[Syslog]
; Whether or not to define the various syslog variables (e.g. $LOG_PID,
; $LOG_CRON, etc.). Turning it off is a good idea performance-wise. In
; runtime, you can define these variables by calling define_syslog_variables().
define_syslog_variables = Off

[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25

; For Win32 only.
sendmail_from = localhost

; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path =

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =
can i add another question sir?
i don't even know how to configure smtp i tried to follow the instruction i found from an article in the internet, but don't know if my smtp really working know. Sir how do i know whether its really working?
Nov 22 '06 #6

ronverdonk
Expert 2.5K+
P: 4,258
In order to use the mail() function you must have setup the php.ini parameters correctly.
This means that you have to define in your php.ini:
Expand|Select|Wrap|Line Numbers
  1. SMTP = 
the name of the SMTP server. If you do not have your own SMTP server or your provider does not provide an SMTP server to you, you could specify the mail server that is attached to your standard email address, like smtp.mysite.com or alike.
Expand|Select|Wrap|Line Numbers
  1. sendmail_from=
you can use an address at your SMTP server address, in a situation as abobe, uou could use your own email address here.[/code]

Try it out and see if this setup works for you,

Ronald :cool:
Nov 23 '06 #7

ronverdonk
Expert 2.5K+
P: 4,258
Seeing your reply, which is the exact copy of your earlier post, and therefore removed, you do not quite understand what must be done.

In order to use the mail function, you must configure some parameters in the php.ini file, that should be located in your C:\WINDOWS directory.
The specific configuration directives for Windows are:
  • SMTP=
    host name or IP address of the SMTP server PHP should use for mail sent with the mail() function.
    I use my email provider's server for that. You can do same and specify something like SMTP=mail.myprovider.com (just an example!)
  • smtp_port=
    Usually defaults to 25, leave it 25.
  • sendmail_from=
    "From:" mail address should be used in mail sent from PHP under Windows.
    So you could define your own email adddress here.

If you want more and a better explanation of these directives, refer to the php manual on mail() at PHP Mail functions

And last: I am not Sir, my name is Ronald.

Ronald :cool:
Nov 23 '06 #8

P: 4
What about if the port is different than 25?
Still the function will work?

Thanks
Jan 19 '07 #9

ronverdonk
Expert 2.5K+
P: 4,258
What about if the port is different than 25?
Still the function will work?

Thanks
amorgos: you asked that already in a separate thread! Don't pollute this thread.

Ronald :cool:
Jan 19 '07 #10

P: 2
Ronald,

I am also looking for a solution for sending mail from forms using PHP in Windows. I researched your replies to the previous person and checked my PHP.ini and code to see if it complied with your instructions. It does comply and I and getting the error: "Warning: mail() {function.function]: smpt DERVER RESPONSE:530 AUTHENTICATION REQUIRED - FOR HELP GO TO HTTP://HELP.YAHOO.COM/HELP/US/SBC/CL...OP/POP-11.HTML IN d:\iNTERNET\aPACHED2.2\HTDOCS\MYSITE\messageSent.p hp on line 8.

The error points to my code on line 8 of the messageSent.php code:
Code goes here:
[code]
<?php
$to = "yourname@yourserver.com";
$subject = "This is the message subject";
$body = "this is the message body";
$headers = "From: yourname@yourserver.com\n";
mail($to,$subject,$body,$headers); ; this is line 8 of the code
?>

when the code attempts to send the message to my ISP's smtp server, the smtp server refuses the message because , I believe that there is no authentication set for it to use my isp's smtp server.

I use Yahoo and when I went to HTTP://HELP.YAHOO.COM/HELP/US/SBC/CLSL/MAIL/POP/POP-11.HTML,
I found that Yahoo only accepts certain email clients and Yahoo lists them.

There is also some discussion about paying for access to a smpt server, if you wish to send mail from your own site. I may be reading something wrong and I am looking for second opinions and some direction. Can anyone else provide some insight regarding how Yahoo or other ISPs handle this issue.

This may be why so many people are failing to be able to use smtp from their local host servers.

This is code from my PHP.ini file:
Expand|Select|Wrap|Line Numbers
  1. [mail function]
  2. ; For Win32 only.
  3. SMTP = smtp.pacbell.yahoo.com
  4. smtp_port = 25
  5.  
  6. ; For Win32 only.
  7. ;sendmail_from = me@pacbell.net
  8.  
  9. ; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
  10. ;sendmail_path =
  11.  
  12. ; Force the addition of the specified parameters to be passed as extra parameters
  13. ; to the sendmail binary. These parameters will always replace the value of
  14. ; the 5th parameter to mail(), even in safe mode.
  15. ;mail.force_extra_parameters =
  16.  

This is the code from my form:
Expand|Select|Wrap|Line Numbers
  1.     <td><form id="form2" name="form2" method="post" action="">
  2.       <textarea name="body" cols="55" id="body"></textarea>
  3.     </form>    </td>
  4.   </tr>
  5.   <tr>
  6.     <td>&nbsp;</td>
  7.     <td><form id="form3" name="form3" method="post" action="">
  8.       <input type="submit" name="Submit" value="Submit" />
  9.     </form>    </td>
  10.   </tr>
  11.  
The form and this code work well. My ISP seems to refuse to take messages from outside servers and or email clients other than those listed in the help page.

Is their a work around for this?
Jul 2 '07 #11

kovik
Expert 100+
P: 1,044
Why is everyone hijacking this thread?

Anyway, the simplest way to send mail using SMTP is to use SwiftMailer. It is very easy to use, extremely object-oriented, and has it's own support board at DevNetwork.
Jul 2 '07 #12

P: 2
Why is everyone hijacking this thread?

Anyway, the simplest way to send mail using SMTP is to use SwiftMailer. It is very easy to use, extremely object-oriented, and has it's own support board at DevNetwork.
Thanks for the information. I will try Swiftmailer.
Jul 3 '07 #13

eragon
100+
P: 431
sorry to use this thread, but its relavant to my problems, you see, therea a forum on my webite that is SUPPOSED to send me an email verification whenever someone posts a new thread, but it dosnt work.

heres the error:

Expand|Select|Wrap|Line Numbers
  1. Warning: mail() [function.mail]: Safety Restriction in effect. The mail() command is not allowed, contact the admin. in /forums/mboard.php on line 470
this file can be found at here
Jul 6 '07 #14

Post your reply

Sign in to post your reply or Sign up for a free account.