// send email
SimpleRepositoryFileData data =
repo.getDataForRead( sourceFile.getId(), SimpleRepositoryFileData.class );
// if email is setup and we have tos, then do it
Emailer emailer = new Emailer();
if ( !emailer.setup() ) {
// email not configured
return;
}
String to = (String) actionParams.get( "_SCH_EMAIL_TO" );
String cc = (String) actionParams.get( "_SCH_EMAIL_CC" );
String bcc = (String) actionParams.get( "_SCH_EMAIL_BCC" );
if ( ( to == null || "".equals( to ) ) && ( cc == null || "".equals( cc ) )
&& ( bcc == null || "".equals( bcc ) ) ) {
// no destination
return;
}
emailer.setTo( to );
emailer.setCc( cc );
emailer.setBcc( bcc );
emailer.setAttachment( data.getInputStream() );
emailer.setAttachmentName( "attachment" );
String attachmentName = (String) actionParams.get( "_SCH_EMAIL_ATTACHMENT_NAME" );
if ( attachmentName != null && !"".equals( attachmentName ) ) {
String path = filePath;
if ( path.endsWith( ".*" ) ) {
path = path.replace( ".*", "" );
}
String extension = MimeHelper.getExtension( data.getMimeType() );
if ( extension == null ) {
extension = ".bin";
}
if ( !attachmentName.endsWith( extension ) ) {
emailer.setAttachmentName( attachmentName + extension );
} else {
emailer.setAttachmentName( attachmentName );
}
} else if ( data != null ) {
String path = filePath;
if ( path.endsWith( ".*" ) ) {
path = path.replace( ".*", "" );
}
String extension = MimeHelper.getExtension( data.getMimeType() );
if ( extension == null ) {
extension = ".bin";
}
path = path.substring( path.lastIndexOf( "/" ) + 1, path.length() );
if ( !path.endsWith( extension ) ) {
emailer.setAttachmentName( path + extension );
} else {
emailer.setAttachmentName( path );
}
}
if ( data == null || data.getMimeType() == null || "".equals( data.getMimeType() ) ) {
emailer.setAttachmentMimeType( "binary/octet-stream" );
} else {
emailer.setAttachmentMimeType( data.getMimeType() );
}
String subject = (String) actionParams.get( "_SCH_EMAIL_SUBJECT" );
if ( subject != null && !"".equals( subject ) ) {
emailer.setSubject( subject );
} else {
emailer.setSubject( "Pentaho Scheduler: " + emailer.getAttachmentName() );
}
String message = (String) actionParams.get( "_SCH_EMAIL_MESSAGE" );
if ( subject != null && !"".equals( subject ) ) {
emailer.setBody( message );
}
emailer.send();
} catch ( Exception e ) {
log.warn( e.getMessage(), e );
}
}