Solved

Unable to send email using mail() function

Posted on 2008-06-13
7
1,443 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
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 35

Assisted Solution

by:torimar
torimar earned 150 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 150 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 200 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now