Welcome, guest | Sign In | My Account | Store | Cart

One way to work around the lack of a full implementation of sendmail in PHP is to use the Microsoft Collaboration Data Objects (CDO) Library.

Here is a short example of its usage:

PHP, 9 lines
1
2
3
4
5
6
7
8
9
<?php
$message = new COM('CDO.Message');
$message->To = 'receiver@somplace.com';
$message->From = 'Sender@MyCompany.com';
$message->Subject = 'This is a subject line';
$message->HTMLBody = '<html><body>This is <b>the</b> body!</body></html>';
$message->AddAttachment('http://www.ActiveState.com');
$message->Send();
?>

One issue on windows is the use of the Mail() function. On Linux systems, you can configure this function to use Sendmail or Qmail to send messages. As of version 4.0.5, this is not available under Windows (feature was added to 4.0.6). The Windows version of PHP includes a built-in implementation for sending emails that is limited to simple messaging.

See http://www.php.net/manual/en/function.mail.php for more information. If you need more capabilities you may need to script your own sendmail implementation, though you should be able to find several by search any of the PHP script repositories.

See http://msdn.microsoft.com/library/default.asp?URL=/library/psdk/cdo/_olemsg_introduction.htm for more information on using the CDO Library.

3 comments

Neal Schafer 18 years, 10 months ago  # | flag

Specifing Mail Server Parameters with CDO. If you are sending via an SMTP server that is not the same box as the web server you will need to configure the CDO object with the correct information. The following code shows this extra step.

$message = new COM('CDO.Message');
$messageCon= new COM('CDO.Configuration') ;
$messageCon->Fields['http://schemas.microsoft.com/cdo/configuration/smtpserver'] = YourServerAddress;
$messageCon->Fields['http://schemas.microsoft.com/cdo/configuration/sendusername'] = YourSMTPUserName;
$messageCon->Fields['http://schemas.microsoft.com/cdo/configuration/sendpassword'] = YourSMTPPassword;
$messageCon->Fields['http://schemas.microsoft.com/cdo/configuration/smtpserverport'] = 25 ;
$messageCon->Fields['http://schemas.microsoft.com/cdo/configuration/sendusing'] = 2 ;
$messageCon->Fields['http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout'] = 60 ;
$messageCon->Fields['http://schemas.microsoft.com/cdo/configuration/CdoProtocolsAuthentication'] = 1 ;
$messageCon->Fields['http://schemas.microsoft.com/cdo/configuration/smtpauthenticate'] = 1;
$messageCon->Fields->Update();
$message->Configuration = $messageCon;
$message->To = 'To Address';
$message->From = 'From Address';
$message->Subject = 'Message Subject';
$message->HTMLBody = ' Message Body ';
$message->Send() ;
Brain Lam 18 years, 4 months ago  # | flag

i can not use COM('CDO.Message') this code. i can not use COM('CDO.Message') this code. error: Fatal error: Uncaught exception 'com_exception' with message 'Source: CDO.Message.1 Description: The "SendUsing" configuration value is invalid. ' in D:\banh\cong viec\website\mailDual\b.php:7 Stack trace: #0 D:\banh\cong viec\website\mailDual\b.php(7): com->Send() #1 {main} thrown in D:\banh\cong viec\website\mailDual\b.php on line 7

Richie Cahipe 17 years, 11 months ago  # | flag

Created by Shane Caraveo on Fri, 7 Dec 2001 (MIT)
PHP recipes (51)
Shane Caraveo's recipes (5)

Required Modules

  • (none specified)

Other Information and Tasks