Thursday, June 5, 2008

Sending an email from PHP (on Windows too.)

Yey, finally got something that really works.
It's not my stuff, but I was able to found it after a long time search!
Here is the link to sourceForge.net, to the page that hosts php Mailer for PHP 5 for Windows, and looks like for Unix too. If the link does not work then search for the word phpMailer.

The easiest way to install the stuff is to do as follows (example on Windows, with assumption that you have your php installed in the c:\php directory.)

> mkdir c:\php\mailer
Unzip phpMailer_v2.1.zip into c:\php\mailer
> notepad.exe c:\php\php.ini
Add at the very end:
include_path=".;c:\php\mailer"

Youre done!

Now, example:
>
<?php
require_once( "class.phpmailer.php" ) ;

$mail = new PHPMailer() ;
$mail->IsSMTP() ;
$mail->Host = "smtp.myserver.com:587" ;
$mail->SMTPAuth = true ;
$mail->Username = "username" ;
$mail->Password = "password" ;
$mail->From = "me@myserver.com" ;
$mail->FromName = "It is me!" ;
$mail->AddAddress( "testaddress@theirserver.com", "Poor Receiver" ) ;
$mail->AddReplyTo( "me@myserver.com", "do-not-reply" ) ;
$mail->AddAttachment( "c:\\mydog.jpg", "Isn't she cute?" ) ;
$mail->Subject = "Sorry for this test email!" ;
$mail->IsHTML( true ) ;
$mail->Body = file_get_contents( "c:\\myemail.html" ) ;
$mail->AltBody = "non-HTML mail client? Where are you from?" ;

if( !$mail->Send())
printf( "Sending Error: " . $mail->ErrorInfo . "\n" ) ;
else
printf( "Message has been sent successfully.\n" ) ;
?>

No comments: