?
Solved

Unable to send email using mail() function

Posted on 2008-06-13
7
Medium Priority
?
1,461 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

777 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