Sending email with php's mail function

Status
Not open for further replies.

reedjasonf

Solid State Member
Messages
9
Got a problem here and haven't found the solution anywhere!

Here's the situation: I have a single server computer running windows server 2003. The host is connected to the internet using a star topography, the gateway being a router connected directly to the internet and I have http port 80, smtp port 25 and pop3 port 110 all forwarded. On this host I am developing a website - works fine. I would like to set up this website to send emails using php's mail() function.

Of course, I am using windows IIS and pop3 services. I have set up my php.ini file to use localhost as smtp server on port 25 and set connection and relay settings under smtp virtual server.

I have 2 pop3 accounts setup: one for personnal use and another called "test". I am able to send and recieve messages on these accounts when using a pop3 mail reader, such as outlook, successfully. However, when I run my php test page to send a test email I don't recieve it on an external email address. I am trying to send an email to my old gmail account. My php code is here:

<?php
$to = 'my-gmail-account@gmail.com;
$subject = 'subject';
$headers = 'From: test@asubdomain.no-ip.org' . "\r\n" .'Reply-To: test@asubdomain.no-ip.org' . "\r\n" .'X-Mailer: PHP/' . phpversion();

$message = 'This is just a test message.';
$message = str_replace("\n.", "\n..", $message);


if(mail($to, $subject, $message, $headers))
echo 'Mail sent';
?>

When I run this script I don't get any error messages. I looked in my smtp log file and it reads:

#Software: Microsoft Internet Information Services 6.0
#Version: 1.0
#Date: 2009-10-08 05:19:50
#Fields: time c-ip cs-method cs-uri-stem sc-status
05:19:50 127.0.0.1 HELO - 250
05:19:50 127.0.0.1 MAIL - 250
05:19:50 127.0.0.1 RCPT - 250
05:19:50 127.0.0.1 DATA - 250
05:19:50 127.0.0.1 QUIT - 240

I notice that these messages sit in the queue folder until they expire and then a message is sent to my "test" account saying that the message was not sent to the recipient (myself).

I hope I have provided enough information about my problem for a diagnosis. I need a savior here...
 
Well, for starters, you missed a single apostrophe at the end of line 2.

If that doesn't fix you, then, try this simpler code:

PHP:
<?php

$to = 'my-gmail-account@gmail.com';
$subject = 'subject';
$headers = 'From: test@asubdomain.no-ip.org';
$message - 'Test message';

if (mail ($to,$subject,$message,$headers)) {
	echo 'Mail sent successfully!';
} else {
	echo 'Mail was not sent!';
}

?>

If that code works, then PHP is configured properly and your SMTP server is not.
 
Do you get your 'Mail sent' message? Try just using '\n' rather than '\r\n'.

Yes, I get my 'Mail sent' message and I've tried using just "\n" per the note on php.net. Either way, the mail just sits in the queue folder.
If I change the $to variable to an account on my domain the mail is sent and recieved successfully. The problem is when sending mail to an external domain, such as yahoo, gmail, hotmail, etc and ONLY when using php's mail function. If I use outlook it works fine.

@office politics: The DSC error is simply, 'Delivery to the following recipients failed.' then lists the intended recipient. I'll take a look at that website.

@crazeD: Pardon me. The apostrophe is supposed to be there but it got deleted when I was editing my variables to properly post on the forum. This is not the problem. I have taken into consideration that it may be a problem with the headers but I don't think it is because i've tried the script without the headers (only From:) as you've suggested.

Update: I just tested the SMTP server using telnet as suggested and when I attempt to set the rcpt to: x.gmail.com I get the 'Unable to Relay for x.gmail.com' error. I think this has identified the problem. I'm able to set rcpt to: to accounts on my domain.

Still having problems sending mail from mail() function in php to external mail server. I think the problem has something to do with not being able to resolve the external mail server's mx record or IP address. This should be pretty straight forward but somewhere my emails are just sitting on my smtp server in the queue.
 
i would setup a smart host in iis smtp using gmail's smtp server. be sure to enable authenication for the smart host.

HOW TO: Configure the SMTP Virtual Server for Message Delivery

I think I understand what you're saying. Like make an account with gmail to route all of my php generated mail through? That might work, even if it's not as elegant as what I'd like to have done.. Let me try that and i'll post if it works. Thanks.

I'm guessing I just set up the smart host in my smtp server as smtp.gmail.com?
 
I think the smart host should be set to smtp.gmail.com. You could confirm this by looking up how to configure gmail using Outlook.

the sending address may need to be the email address assigned to the gmail account.

there should be a button for authenication somewhere in the smtp server properties. you'll need to add the username and password for the gmail account.
 
Why does this happen ONLY when I send mail with PHP? I'm still not getting this to work. I don't understand how mail sent through outlook or thunderbird gets to the external server but mail sent with php just sits in the queue folder.
 
Status
Not open for further replies.
Back
Top Bottom