Solved

IIS7 SMTP PHP Email Problem

Posted on 2011-09-06
10
1,765 Views
Last Modified: 2012-05-12
I'm running a dedicated Windows Web Server 2008 R2 Service Pack 1.

I can send email fine using an ASP script (attached) but can't send email via a PHP script (attached).

I have MailEnable installed but the SMTP service for this is disabled. If I disable the Windows SMTP service all mail gets saved to C:\inetpub\mailroot\Pickup so I feel confident the Windows SMTP service is being used. I've looked at the configuration options for SMTP in IIS Manager but any change I make here to Default Web Site or the test URL's site don't seem to affect the SMTP service.

php.ini
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = me@example.com


Does anyone have any suggestions? Is there somewhere else I can configure SMTP?


<%
Err.Clear
On Error Resume Next

Set mail 	 = Server.CreateObject ("CDONTS.NewMail")
mail.From    = "admin@mydomain.com" & "(Senders Name)"
mail.Subject = "TEST ASP"
mail.To      = "test@hotmail.co.uk"
'Text format
mail.BodyFormat = 0
'Mime format
mail.MailFormat = 0
mail.Body = body
mail.Send
set mail = nothing

If Err.Number <> 0 Then
  Response.Write (Err.Description& "<br><br>")  
  Response.End
ELse
   Response.Write("Mail Sent" & vbCrLf)
End If
On Error GoTo 0
%>


<?php
if(mail('test@hotmail.co.uk','test subject','test message')){
      echo('ok');
    }
else{
     echo('not ok');
    }
?>

Open in new window

0
Comment
Question by:benners70
  • 4
  • 3
  • 2
10 Comments
 
LVL 5

Accepted Solution

by:
zvytas earned 250 total points
ID: 36488720
Try to uncomment the following line in the php.ini:

sendmail_from = me@example.com

This line is required when running sending mail from php on Windows.
0
 

Author Comment

by:benners70
ID: 36488766
What should I be replacing me@example.com with?
0
 
LVL 5

Expert Comment

by:zvytas
ID: 36489206
Whatever you want to be a sender of the email, if you want it exactly the same as asp version you should use "admin@mydomain.com".
0
 

Author Comment

by:benners70
ID: 36489325
I think the solution is in the php.ini file. I currently have the following which isn't working. I've tried to set ip up to use an SMTP service on a shared server I use which requires authentication as I can't seem to use localhost.

I also don't get an helpfull errors from by PHP mail test script, is there something I can do to get something useful back?

[mail function]
; For Win32 only.
SMTP = mail.mydomain.co.uk
smtp_port = 25

; For Win32 only.
sendmail_from = website@mydomain.com
auth_username = website@mydomain.com
auth_password = mydomainpassword

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path =

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =

; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header = On

; The path to a log file that will log all mail() calls. Log entries include
; the full path of the script, line number, To address and headers.
;mail.log =
0
How does your email signature look on mobiles?

Do your employees use mobile devices to reply to emails? With mobile becoming increasingly important to the business world, it is in your best interest to make sure that your email signature looks great across all types of devices.

 
LVL 5

Expert Comment

by:zvytas
ID: 36489446
Try to add the following at the top of your php code:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

This might show some details about the error. If it doesn't I think the only way to debug is checking the log files.
0
 
LVL 4

Assisted Solution

by:vaibhavjaiman
vaibhavjaiman earned 250 total points
ID: 36496317
Hello,

You are using port 25 for relaying the mail from the IIS SMTP which would be conflicting with the Mailenable SMTP services,You can try telnet to the port 25 and you will know it,

What you require is that please change the SMTP port under the php.ini to 2525 and there after make sure that you have configured the IIS SMTP to relay the mail from the server using that port.
IIS7 uses the IIS6 SMTP virtual server to relay mail.

Further you can use the below simple script to relay mail through localhost
*******************************************************************************************
<?php
$to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
*******************************************************************************************
Alternatively,You can use the authenticated script to  send the mail from PHP,Below is the sample script,In order to make the below script work you need to install the pear package including mail,Auth,net_smtp pear package
*******************************************************************************************
<?php
require_once "Mail.php";

$from = "name Sender <noreply@technodg.com>";
$to = "name Recipient <logswatch@yahoo.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";

$host = "hostname";
$username = "test@domain.com";
$password = "N%Kx.p7vULSN";

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
 } else {
  echo("<p>Message successfully sent!</p>");
 }
?>
*******************************************************************************************
0
 

Assisted Solution

by:benners70
benners70 earned 0 total points
ID: 36496786
HI vaibhavjaiman

Thanks for the suggestions which are useful. I have however had to pay for support from my provider to resolve this quicker.

The solution was as follows:
1. Disable all components of MailEnable to ensure no comflict.
2. Open IIS6 to adminster the Relay setting under the Access tab. Add in 127.0.0.1 and also localhost. This last value was probably the root of the problem.
3.php.ini as follows. As far as I can tell sendmail_from can be any email.

[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25
sendmail_from = admin@yourdomain.com
4. Restart IIS.

Other useful things.
1. To send via external SMTP with authentication in PHP you need to install PEAR.
2. To help avoid spam folders set 'sendmail_from' to be the same as the Reply-To. e.g.
ini_set('sendmail_from','admin@yourdomain.com');
$headers .= 'Reply-To: admin@yourdomain.com' ;
mail($to, $subject , $body, $headers
0
 

Author Comment

by:benners70
ID: 36500921
I've requested that this question be closed as follows:

Accepted answer: 0 points for benners70's comment http:/Q_27292675.html#36496786

for the following reason:

I have accepted my own solution as it is the solution.
0
 
LVL 4

Expert Comment

by:vaibhavjaiman
ID: 36500922
Hello,

That's what I stated under my post.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Nearly six years ago I was hired by a company to be their senior server engineer. One of my first projects was to implement Exchange Server 2007 on a Windows Server 2008 Single Copy Cluster for high availability. That was the easy part; read on to l…
Easy CSR creation in Exchange 2007,2010 and 2013
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
To show how to create a transport rule in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Mail Flow >> Rules tab.:  To cr…

757 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