Divide and conquer.
Connecting to a MSSQL database in PHP
http://webcheatsheet.com/php/connect_mssql_database.php
http://us2.php.net/manual/en/book.mssql.php
PHP file IO functions
http://www.php.net/manual/en/ref.filesystem.php
PHP send email
http://www.php.net/manual/en/function.mail.php
Once you've learned how to do each of these small tasks, you can integrate them into the larger system you've described.