// version history
// 2 - 6/24/03 added support for authorization settings and better errors.
//		also removed naked \n from body of email and replaced with \r\n.

{
	var safeToRunScript = true;
	
	safeToRunScript = app.project != null;
	if (! app.project) {
		alert ("A project must be open to run this script.");
	}
	if (safeToRunScript) {
		//check the render queue and make certain at least one item is queued
		safeToRunScript = false;
		for (i = 1; i <= app.project.renderQueue.numItems; ++i) {
			if (app.project.renderQueue.item(i).status == RQItemStatus.QUEUED) {
				safeToRunScript = true;
				break;
			}
		}
		if (! safeToRunScript) {
			alert ("You do not have any items set to render.");
		}
	}
	
	if (safeToRunScript) {
		// check and see if we can access the network to send email
		// test before the render so the person doesn't go home and wait for an email...
		
		// Load code from a file with handy emailing methods:
		var emailCodeFile = new File("email_methods.jsx");
		emailCodeFile.open("r");	
		eval( emailCodeFile.read() );
		emailCodeFile.close();
				
		// this address isn't actually used until send() is called, but specify the loopback for now
		// this is a test to see if network access is enabled in the preferences 
		// [24803] cprosser
		{ 
			var email_test = new EmailSocket("127.0.0.1");
		}

		// The script email_setup.jsx will prompt the user and establish the settings.
		// We'll only run it now if we don't have the settings already.  
		// If you want to change the settings, you can run email_setup.jsx as a 
		// separate script at any time.
		var settings = app.settings;
		if ( !settings.haveSetting("Email Settings", "Mail Server") ||
			 !settings.haveSetting("Email Settings", "Reply-to Address") ||
			 !settings.haveSetting("Email Settings", "Render Report Recipient")){
	
			// We don't have the settings yet, so run email_setup.jsx
			// to prompt for them 
			var email_setupfile = new File("email_setup.jsx");
			email_setupfile.open("r");	
			eval( email_setupfile.read() );
			email_setupfile.close();
		}
	
		var myQueue = app.project.renderQueue //creates a shortcut for RQ
		
		// Call render
		myQueue.render();
			
		// Now rendering is complete.
		// Create a string for the mail message that contains:
		// Start time (date)
		// Render time of each item in the queue
		// Total render time
		var projectName = "Unsaved Project";
		if (app.project.file) {
			projectName = app.project.file.name;
		}
		// can't have bare LF in email, always put \r before \n or some
		// servers will die.
		var myMessage = "Rendering of " + projectName + " is complete.\r\n\r\n";
		
		// Email the message.
		// We'll use three Settings to determine how to mail.
		// The section will be named "Email Settings"
		// The 3 settings will be named:
		//	"Mail Server" - the mail server to use when sending mail.
		//	"Reply-to Address" - the address from which the mail will be sent
		//	"Render Report Recipient" - the address to which mail will be sent.
	
		if ( !settings.haveSetting("Email Settings", "Mail Server") ||
			 !settings.haveSetting("Email Settings", "Reply-to Address") ||
			 !settings.haveSetting("Email Settings", "Render Report Recipient")){
			alert("Can't send an email, I don't have all the settings I need. Aborting.");
		} else {
			try {
			// send the email	
				var serverSetting = settings.getSetting("Email Settings", "Mail Server");
				var fromSetting = settings.getSetting("Email Settings", "Reply-to Address");
				var toSetting   = settings.getSetting("Email Settings", "Render Report Recipient");
				var authUser;
				var authPass;
				
				if (app.settings.haveSetting("Email Settings", "Auth User")) {
					authUser = app.settings.getSetting("Email Settings", "Auth User");
				}
				
				if (app.settings.haveSetting("Email Settings", "Auth Pass")) {
					authPass = app.settings.getSetting("Email Settings", "Auth Pass");
				}
				
				//ack, can't delete settings...
				if (authUser == "") {
					authUser = null;
					authPass = null;
				}
				
				var myMail = new EmailSocket(serverSetting);
				
				
				if (! myMail.send (fromSetting, toSetting, "AE Render Completed", myMessage, authUser, authPass) ) {
					alert("Sending mail failed");
				}
			} catch (e) {
				alert("Unable to send email.\n" + e.toString());
			}
		}
	}
}
	
	
	
	
	
	