Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Unable to send email using mail() function

Posted on 2008-06-13
7
Medium Priority
?
1,468 Views
Last Modified: 2013-12-13
I am using php 5 with apache 2.2 under win xp pro.

I am using the following PHP script to send email,
but I get the error(shown in the error.log of apache)
[Sat Jun 14 13:41:37 2008] [error] [client 127.0.0.1] PHP Warning:  mail() [<a href='function.mail'>function.mail</a>]: SMTP server response: 550 5.7.1 Unable to relay for receiver@gmail.com in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\test2.php on line 8

(Note that in my case I have replaced receiver@gmail.com as the my own email).

May I know how to resolve this problem?

<?php
$to = "receiver@gmail.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "sender@company.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

Open in new window

0
Comment
Question by:william007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 9

Author Comment

by:william007
ID: 21784324
Some more info, I am sending email via my local machine.
Following is the relevant contents in php.ini.

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

; For Win32 only.
;sendmail_from = me@example.com
0
 
LVL 4

Expert Comment

by:afzz
ID: 21784401
uncomment ;sendmail_from = me@example.com and put your valid email address here
0
 
LVL 9

Author Comment

by:william007
ID: 21784618
What kind of valid email?

In the localhost, I don't have any SMTP setup there, thus do not have any email address as well.
(I am not sure with this I am able to send email)

Or you mean the email address of another SMTP server like abc@gmail.com...which is the gmail SMTP server?
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 35

Assisted Solution

by:torimar
torimar earned 600 total points
ID: 21784651
"If you have a test server in use running Windows and some kind of WAMP combo (XXAMP, WAMP Server, etc) then you'll notice that the PHP sendmail command (mail()) does not work. Windows simply does not provide the sendmail statement ..."

This is an excerpt from a User Contributed Note on php.net concerning the mail() function.
For a suggested work-around, check the full note:
http://de2.php.net/manual/de/ref.mail.php#82325
0
 
LVL 4

Assisted Solution

by:afzz
afzz earned 600 total points
ID: 21784995
In my setup. I have setup an smtp server software at localhost port:25.

In Xp professional you can try the following to setup smtp service

Go to control panel -> Add/remove programs -> select Add/remove windows components -> Select IIS and click details -> In details check SMTP Service and click on ok

You may need to go to -> Control Panel - > Administrative Tools -> Services  to start the SMTP Service
0
 
LVL 18

Accepted Solution

by:
Matthew Kelly earned 800 total points
ID: 21790624
If you do not have a email server running on the machine running PHP, mail will not work.

The best approach is to use a 3rd party mail program. In the past I have used PHPMailer

http://sourceforge.net/projects/phpmailer

Below is the PHP code to use it. Place class.phpmailer.php into your php includes directory.

Replace text in << >> with your server information: http://www.emailaddressmanager.com/tips/mail-settings.html
<?php
 
require("class.phpmailer.php");
 
$mail = new PHPMailer();
 
$mail->IsSMTP(); // send via SMTP
$mail->Host     = "<<smtpserver>>:<<stmpport>>"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "<<uname>>"; // SMTP username
$mail->Password = "<<pword>>"; // SMTP password
$mail->From = "<<email address from>>";
$mail->FromName = "<<email name from>>";
$mail->AddAddress("<<email address to>>"); 
$mail->AddReplyTo("<<email address from>>","<<email name from>>");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject  =  "Subject";
$mail->Body     =  "Body text";
$mail->AltBody  =  $mail->Body;
	
$mail->Send();
 
?>

Open in new window

0
 
LVL 9

Author Comment

by:william007
ID: 21836874
Thanks :)
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question