Backup_FME_personal/force-app/main/default/classes/OrgWideEmailHandler.cls

159 lines
5.8 KiB
OpenEdge ABL

//done
public with sharing class OrgWideEmailHandler {
// @AuraEnabled
// public static List<Map<String, String>> getAllFromEmailOptions() {
// List<Map<String, String>> emails = new List<Map<String, String>>();
// System.debug('emails>>>>>>>'+emails);
// try{
// // Current user email
// User currentUser = [ SELECT Id, name ,Email FROM User WHERE Id = :UserInfo.getUserId() LIMIT 1];
// emails.add(new Map<String, String>{
// 'label' => currentUser.Name + '(' + currentUser.Email + ')',
// 'value' => currentUser.Email
// });
// // Org-Wide emails
// for (OrgWideEmailAddress owa : [ SELECT Id, Address, displayname ,IsAllowAllProfiles FROM OrgWideEmailAddress WHERE Address != null ]) {
// emails.add(new Map<String, String>{
// 'label' => owa.displayname + '(' + owa.Address + ')',
// 'value' => owa.Address
// });
// }
// // return emails;
// }catch(Exception e){
// System.debug('Error in getAllFromEmailOptions: ' + e.getMessage());
// FME_Logger.error(e.getMessage(),' OrgWideEmailHandler ' + e.getLineNumber(),'getAllFromEmailOptions');
// emails = new List<Map<String, String>>();
// }finally{
// FME_Logger.saveLog();
// }
// return emails;
// }
@AuraEnabled
public static List<Map<String, String>> getAllFromEmailOptions() {
List<Map<String, String>> options = new List<Map<String, String>>();
// Org-Wide Email Addresses
for (OrgWideEmailAddress owe : [
SELECT Id, DisplayName, Address
FROM OrgWideEmailAddress
]) {
options.add(new Map<String, String>{
'label' => owe.DisplayName + ' <' + owe.Address + '>',
'value' => owe.Id // ✅ IMPORTANT
});
}
// Current User (optional)
User u = [
SELECT Id, Name, Email
FROM User
WHERE Id = :UserInfo.getUserId()
];
if(options.isEmpty() ){
options.add(new Map<String, String>{
'label' => u.Name + ' <' + u.Email + '>',
'value' => u.Id });
} else {
options.add(0, new Map<String, String>{
'label' => u.Name + ' <' + u.Email + '>',
'value' => u.Id
});
}
return options;
}
@AuraEnabled
public static void sendEmailFromLwc(String fromEmail, String toEmail, String ccEmail, String bccEmail,
String subject,
String body,
String fileId,
String fileName
) {
System.debug('fromEmail>>>>>>>'+fromEmail);
System.debug('toEmail>>>>>>>'+toEmail);
System.debug('ccEmail>>>>>>>'+ccEmail);
System.debug('bccEmail>>>>>>>'+bccEmail);
System.debug('subject>>>>>>>'+subject);
System.debug('body>>>>>>>'+body);
System.debug('fileId>>>>>>>'+fileId);
System.debug('fileName>>>>>>>'+fileName);
try{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new List<String>{ toEmail });
if (!String.isBlank(ccEmail)) {
mail.setCcAddresses(new List<String>{ ccEmail });
}
if (!String.isBlank(bccEmail)) {
mail.setBccAddresses(new List<String>{ bccEmail });
}
mail.setSubject(subject);
mail.setHtmlBody(body);
// If fromEmail is not current user's email, try setting OrgWideEmailAddressId
String currentUserEmail = [SELECT Email FROM User WHERE Id = :UserInfo.getUserId()].Email;
System.debug('currentUserEmail>>>>>>>'+currentUserEmail);
System.debug('fromEmail>>>>>>>'+fromEmail);
if (fromEmail != currentUserEmail) {
List<OrgWideEmailAddress> matches = [
SELECT Id FROM OrgWideEmailAddress WHERE Address = :fromEmail LIMIT 1
];
if (!matches.isEmpty()) {
mail.setOrgWideEmailAddressId(matches[0].Id);
}
}
if (!String.isBlank(fileId)) {
// Get the latest ContentVersion for the given ContentDocument Id
ContentVersion cv = [
SELECT Title, VersionData, FileExtension
FROM ContentVersion
WHERE ContentDocumentId = :fileId
ORDER BY CreatedDate DESC
LIMIT 1
];
System.debug('cv>>>>>>>'+cv);
System.debug('cv.Title>>>>>>>'+cv.Title);
System.debug('cv.VersionData>>>>>>>'+cv.VersionData);
System.debug('cv.FileExtension>>>>>>>'+cv.FileExtension);
// Attach the file to the email
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName(cv.Title + '.' + cv.FileExtension);
efa.setBody(cv.VersionData);
efa.setContentType('application/octet-stream');
mail.setFileAttachments(new Messaging.EmailFileAttachment[] { efa });
}
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
} catch (Exception e) {
FME_Logger.error(e.getMessage(), 'OrgWideEmailHandler', 'sendEmailFromLwc');
} finally {
FME_Logger.saveLog();
}
}
}