Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Sending email with php's mail function
Reply
Old 10-08-2009, 02:32 AM   #1 (permalink)
 
Newb Techie

Join Date: Oct 2009

Posts: 9

reedjasonf is on a distinguished road

Exclamation Sending email with php's mail function

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...

Last edited by reedjasonf; 10-08-2009 at 02:36 AM. Reason: Providing new information about my problem.
reedjasonf is offline   Reply With Quote
Old 10-08-2009, 02:44 AM   #2 (permalink)
Hefemeister's Avatar
 

Join Date: Feb 2004

Location: Sweden

Posts: 6,677

Hefemeister is just really niceHefemeister is just really niceHefemeister is just really niceHefemeister is just really nice

Default Re: Sending email with php's mail function

moved to programming as it is more of a coding question than a question about hosting.
__________________
ASUS P6T Deluxe V2 :: INTEL i7 920 @3.4 :: XFX GTX260 :: 6gb Corsair 1600 :: Corsair 750TX :: TRUE 120 :: Samsung T240 24" :: Windows 7 X64

I do not accept support questions via PM

"The man in black fled across the desert, and the gunslinger followed."
Hefemeister is online now   Reply With Quote
Old 10-08-2009, 03:56 AM   #3 (permalink)
 

Join Date: Jul 2005

Location: England

Posts: 2,159

kmote has a spectacular aura aboutkmote has a spectacular aura about

Default Re: Sending email with php's mail function

Do you get your 'Mail sent' message? Try just using '\n' rather than '\r\n'.
__________________
MSI P43 Neo|Enermax Pro82+ 425W|E5200|silent 8500GT|250GB Samsung spinpoint F1|Samsung SATA DVD RW|4GB Corsair|Antec SOLO|openSUSE11


There are in order of increasing severity: lies, darn lies, statistics, and computer benchmarks. - diskinfo man page
kmote is offline   Reply With Quote
Old 10-08-2009, 12:50 PM   #4 (permalink)
office politics's Avatar
 
It's all just 1s and 0s

Join Date: Jan 2004

Location: in the lab

Posts: 4,410

office politics will become famous soon enough

Default Re: Sending email with php's mail function

can you send mail using this smtp using telnet?

How to test SMTP with telnet | eHow.com


what is the exact error message in the non delivery report?
office politics is offline   Reply With Quote
Old 10-08-2009, 06:02 PM   #5 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: Sending email with php's mail function

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 Code:
<?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.
__________________

Need website help? PM me!
CrazeD is offline   Reply With Quote
Old 10-08-2009, 06:53 PM   #6 (permalink)
 
Newb Techie

Join Date: Oct 2009

Posts: 9

reedjasonf is on a distinguished road

Default

Quote:
Originally Posted by kmote View Post
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.

Last edited by kmote; 10-09-2009 at 03:43 AM.
reedjasonf is offline   Reply With Quote
Old 10-09-2009, 11:26 PM   #7 (permalink)
office politics's Avatar
 
It's all just 1s and 0s

Join Date: Jan 2004

Location: in the lab

Posts: 4,410

office politics will become famous soon enough

Default Re: Sending email with php's mail function

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
office politics is offline   Reply With Quote
Old 10-10-2009, 09:16 PM   #8 (permalink)
 
Newb Techie

Join Date: Oct 2009

Posts: 9

reedjasonf is on a distinguished road

Default Re: Sending email with php's mail function

Quote:
Originally Posted by office politics View Post
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?
reedjasonf is offline   Reply With Quote
Old 10-11-2009, 10:44 PM   #9 (permalink)
office politics's Avatar
 
It's all just 1s and 0s

Join Date: Jan 2004

Location: in the lab

Posts: 4,410

office politics will become famous soon enough

Default Re: Sending email with php's mail function

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.
office politics is offline   Reply With Quote
Old 10-13-2009, 01:49 AM   #10 (permalink)
 
Newb Techie

Join Date: Oct 2009

Posts: 9

reedjasonf is on a distinguished road

Default Re: Sending email with php's mail function

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.
reedjasonf is offline   Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Encrypting email in Claws Mail Osiris Tips, Tricks & Tutorials 0 07-11-2009 11:32 PM
Claws Mail: The unsung powerhouse email client Osiris Tips, Tricks & Tutorials 0 07-04-2009 10:56 AM
Old School Linux email with Alpine Osiris Linux Tips and Tricks 0 03-05-2009 07:39 PM
The 25 Most Common Mistakes in Email Security Osiris Virus - Spyware Protection / Detection 8 02-20-2009 03:42 PM
Yahoo! Mail announces unlimited email storage maroon1 News & Polls 4 05-30-2007 11:03 PM