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

send mails using mail() ?

P: 5
hello,


can any tell me how to send mails from site using mail() in php

do we need to change in the php.ini
these are in php.ini


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

; For Win32 only.
sendmail_from = me@example.com


and i tried with this example it is giving me the message saying "message successfully sent" but there is no mail in my inbox

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Untitled Document</title>
  6. </head>
  7.  
  8. <body>
  9. <?php
  10. $to = "swetha123@example.com";
  11. $subject = "Hi!";
  12. $body = "Hi,\n\nHow are you?";
  13.  
  14. if (!is_null($to))
  15. {
  16.   mail($to, $subject, $body); 
  17.   echo("<p>Message successfully sent!</p>");
  18. } else {
  19.   echo("<p>Message delivery failed...</p>");
  20. }  
  21. ?>
  22. </body>
  23. </html>
  24.  
  25.  
Dec 24 '08 #1
Share this Question
Share on Google+
1 Reply


Atli
Expert 5K+
P: 5,058
Hi.

You aren't really checking whether the mail was actually sent. You are completely ignoring the return value of the mail() function, printing a success message regardless of it's success or failure.

You need to do something more like:
Expand|Select|Wrap|Line Numbers
  1. if(!empty($to) 
  2. && !empty($subject) 
  3. && !empty($message))
  4. {
  5.   if(mail($to, $subject, $message)) {
  6.     echo "Success";
  7.   } else {
  8.     echo "Failed!";
  9.   }
  10. }
  11. else {
  12.   echo "Required data missing.";
  13. }
Also, the PHP config directives you posted must contain real values for an active SMTP server. PHP must have access to a real SMTP server to be able to send mail.

If there is an active SMTP server available on the localhost via port 25, what you posted will work fine.
Dec 24 '08 #2

Post your reply

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