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
Solved

Unable to send email using mail() function

Posted on 2008-06-13
7
1,452 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

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…
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
The viewer will learn how to dynamically set the form action using jQuery.
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…

856 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