Email with PHP on windows server

Hi,

How I send an email with PHP on windows server (windows Server Web 2008 and IIS 7.0) with smtp external server.

Into file : php.ini


[mail function]
; For Win32 only.
; SMTP = localhost
SMTP = mail.toto.com
; SMTP = 111.111.1.1
smtp_port = 25

I tried with this code, first time:



//echo $var_emaill;
//exit();	
						
		If (!empty($var_emaill))
		{

$recipient = $var_emaill;
$subject = "confirmation …" ;
$msg = "hello \	$var_mr \	$var_name\
";
$msg .= "Email : \	$var_emaill\
";
$msg .= "thanks for your inscription\
\
";
$msg .= "Seconde ligne….." ;
$mailheaders = "From: toto.com<> \
";

//echo $msg;
//exit();

// send email
mail($recipient, $subject, $msg, $mailheaders);

}



Here is an error message :

Server Error
500 - Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed.

And after, I tried an other solution :

I installed “phpmailer » : C:\phpmailer\class.smtp.php

And I used an other code, this one :


<?php

//echo $var_emaill;
//exit();

		If (!empty($var_emaill))
		{

//PHP Mailer
include ("C:\\phpmailer\\class.smtp.php");


// Preparation du mail

$mail = new PHPmailer();
	$mail->IsSMTP();
	$mail->Host='mail.toto.com';
	$mail->From='toto.com';
	$mail->AddAddress($var_emaill);
	$mail->Subject='confirmation…';
	$mail->Body='hello \	$var_mr \	$var_name\
';
	if(!$mail->Send()){ //Test le return code de la fonction
	  echo $mail->ErrorInfo; //error message
	}
	else{	
	  echo 'Mail sent with succes';
	}
	$mail->SmtpClose();
	unset($mail);

	
	

}
?>


I have same error :

Server Error
500 - Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed.

Can you help me :
How must I configure php.ini ?
must I configure class.smtp.php ? If yes how is it ?

my code php (my codes, the two above,) are there correct ?

Best regards

Hello,

For an help, here is my code :

      function sendMail($tab_rcpt_to, $subject, $body, & $tab_errors, $tab_pjs = array (),$from = "noreply@xxxx.fr",$fromname = "xxxx",$host = "xxxx.yyyy.zzzz")
      {
        $mail = new PHPmailer();

        $tab_errors = array ();
        $mail->SMTPDebug = false;
        $mail->IsSMTP();
        $mail->IsHTML(false);
        $mail->Host = $host;
        $mail->From = $from;
        $mail->FromName = $fromname;
        foreach ($tab_rcpt_to as $adr)
        {
          $mail->AddAddress($adr);
        }
        $mail->Subject = $subject;
        $mail->Body = $body;
        foreach ($tab_pjs as $pj)
        {
          if (!$mail->AddAttachment($pj))
          {
            $tab_errors[] = "Attachement PJ ($pj) incorrect";
          }
        }

        $res = @ $mail->Send();
        @ $mail->SmtpClose();
        unset ($mail);
        switch ((int) $res)
        {
          case -3 :
            $tab_errors[] = "Destinataire(s) du message non fourni(s).";
            $res = false;
            break;
          case -4 :
            $tab_errors[] = "Erreur à la création du corps du message.";
            $res = false;
            break;
          case -5 :
            $tab_errors[] = "Problème(s) SMTP possible(s) (connexion refusée, adresse émetteur refusée, adresse destinataire(s) refusée(s), réception de données refusée).";
            $res = false;
            break;
          default :
        }
        return ($res);
      }

Hi LrtL,

Thanks for your code…

But I think I have a problem with my configuration…

I tried with this code, for manually send mail from the webserver computer :


<?php

mail('myemail@gmail.com','my test mail','my Message works');
?>

Here is an error message :

Server Error
500 - Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed.

Into file : php.ini


[mail function]
; For Win32 only.
; SMTP = localhost
SMTP = mail.toto.com
; SMTP = 111.111.1.1
smtp_port = 25

I have an other application with asp.net : this application sent also an email and it works…

So I checked his file web.config (like php.ini for asp.net)
Into this file (web.config) there are some information on login and password .

I don’t know how I can put this information in php.ini.
I don’t know if I must put it in php.ini

Do you have some idea ?:blush:

I’m :mad:

I tried also this code :


<?php
$username = "tototiti";
$password = "tatatete";
$POPserver = "111.111.1.1";
### php.ini's SMTP must correspond to this server
### and sendmail_from must be from this server (??)

$msg = POP_authenticate($username, $password, $POPserver);
if ($msg === FALSE) {
mail("toto@gmail.com", "PHP test", "Line 1\
Line 2");
$msg = "mail (probably) sent.\\r\
";
}
exit($msg);
?>

but the same error message
:eek::(:mad:

I suspect SMTP is not configured correctly in IIS.

Try checking the server’s event logs.

Also try this diagnostic tool: http://www.microsoft.com/downloads/details.aspx?FamilyID=bc1881c7-925d-4a29-bd42-71e8563c80a9&DisplayLang=en

Also, some setup guides:
http://service1.symantec.com/support/ent-gate.nsf/docid/2007241920754398
http://www.jppinto.com/2009/02/installing-iis-70-with-smtp-on-windows-server-2008/

Are sure it’s a problem with sending email and not a problem with PHP itself?

Yes I’m sure that this problem with mail function because other page web with PHP work…

It works after I installed phpmailer-1.71 on my web site

this is a my solution for windows web server 2008 :

Copy thoso files class.phpmailer.php and class.smtp.php to your web site

So you have installed phpmailer-1.71

You don’t need to change your php.ini file

Then, integrate the following code in your form:



// Preparation email

require("class.phpmailer.php"); // ("name_of_your_folder/class.phpmailer.php");)

$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP


$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Host = "111.111.1.1"; // or name of your smtp exemple 111.111.1.1 or smtp.toto.com
$mail->Username = "your_Username";
$mail->Password = "your_Password";

$mail->From = "your_email@toto.com";


$mail->AddAddress = "his_email@otot.fr"; //

$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \
\
 This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;


if(!$mail->Send())
{
   echo 'Message was not sent.';
   echo 'Raison : ' . $mail->ErrorInfo;
}
else
{
   echo "Message has been sent.";
}