Solved

Unable to send email using mail() function

Posted on 2008-06-13
7
1,454 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
Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

 
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

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
if (is_singular not working 5 38
Wordpress and Wufoo 1 37
How Close unsubmited attempts 10 43
Converting PHP triple des decrypt to c# 9 62
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

735 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