Created
July 30, 2018 17:08
-
-
Save sohalloran/ee174d86dd2ebe09c4d53f1e2229d2bb to your computer and use it in GitHub Desktop.
Email Handling for Opportunity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<aura:component controller="EmailSendController" implements="force:lightningQuickAction,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global"> | |
<!--Part 1 [for attribute declare]--> | |
<aura:attribute name="email" type="string"/> | |
<aura:attribute name="subject" type="string"/> | |
<aura:attribute name="body" type="string"/> | |
<aura:attribute name="mailStatus" type="boolean" default="false"/> | |
<!---Part 2 [header part] --> | |
<div class="slds-page-header" role="banner"> | |
<h1 class="slds-page-header__title slds-m-right--small slds-align-middle slds-truncate" title="this should match"> | |
Quick Email Send. | |
</h1> | |
<div class="slds-text-color--weak"></div> | |
</div> | |
<!---Part 3 [message display part] --> | |
<aura:if isTrue="{!v.mailStatus}"> | |
<div role="alertdialog" tabindex="-1" aria-labelledby="prompt-heading-id" aria-describedby="prompt-message-wrapper" class="slds-modal slds-fade-in-open slds-modal--prompt"> | |
<div class="slds-modal__container"> | |
<div class="slds-modal__header slds-theme--error slds-theme--alert-texture"> | |
<h2 class="slds-text-heading--medium" id="prompt-heading-id">Mail Status</h2> | |
</div> | |
<div class="slds-modal__content slds-p-around--medium"> | |
<div> | |
<p>Email Sent successfully to {!v.email}</p> | |
</div> | |
</div> | |
<div class="slds-modal__footer slds-theme--default"> | |
<button class="slds-button slds-button--brand" onclick="{!c.closeMessage}">Close</button> | |
</div> | |
</div> | |
</div> | |
<div class="slds-backdrop slds-backdrop--open"></div> | |
</aura:if> | |
<!---Part 4 [mail fourm part]--> | |
<div class="slds-m-around--medium"> | |
<div class="slds-container--medium"> | |
<div class="slds-form--stacked"> | |
<div class="slds-form-element"> | |
<label class="slds-form-element__label" for="CC">Email</label> | |
<div class="slds-form-element__control"> | |
<ui:inputEmail class="slds-input" aura:id="email" value="{!v.email}" required="true" placeholder="[email protected]"/> | |
</div> | |
</div> | |
<div class="slds-form-element"> | |
<label class="slds-form-element__label" for="CC">Subject</label> | |
<div class="slds-form-element__control"> | |
<ui:inputText class="slds-input" aura:id="subject" value="{!v.subject}" placeholder="Subject"/> | |
</div> | |
</div> | |
<div class="slds-form-element"> | |
<label class="slds-form-element__label" for="textareaSample2">Mail Body</label> | |
<div class="slds-form-element__control"> | |
<lightning:inputRichText aura:id="body" value="{!v.body}" /> | |
</div> | |
</div> | |
<div class="slds-form-element"> | |
<button class="slds-button slds-button--brand" onclick="{!c.sendMail}">Send</button> | |
</div> | |
</div> | |
</div> | |
</div> | |
</aura:component> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EmailSendController { | |
@AuraEnabled | |
public static void sendMailMethod(String mMail ,String mSubject ,String mbody, String mId){ | |
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>(); | |
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); | |
List<String> sendTo = new List<String>(); | |
sendTo.add(mMail); | |
mail.setToAddresses(sendTo); | |
// TEMP HARDCODED reply to address of email handler | |
mail.setReplyTo('emailservice@i-298wrpbndoj5m9zi8zfudvnch4nuu79ntb5g3mcicuzd22wegn.1t-e2jjeac.eu16.apex.salesforce.com'); // change it with your mail address. | |
mail.setSenderDisplayName('salesforce User'); | |
mail.setSubject('ref:' + mId + ' ' + mSubject); | |
mail.setHtmlBody(mbody); | |
mail.setWhatId (mId); | |
mails.add(mail); | |
Messaging.sendEmail(mails); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
sendMail: function(component, event, helper) { | |
var getEmail = component.get("v.email"); | |
var getSubject = component.get("v.subject"); | |
var getbody = component.get("v.body"); | |
if ($A.util.isEmpty(getEmail) || !getEmail.includes("@")) { | |
alert('Please Enter valid Email Address'); | |
} else { | |
helper.sendHelper(component, getEmail, getSubject, getbody, component.get("v.recordId")); | |
} | |
}, | |
closeMessage: function(component, event, helper) { | |
component.set("v.mailStatus", false); | |
component.set("v.email", null); | |
component.set("v.subject", null); | |
component.set("v.body", null); | |
}, | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
sendHelper: function(component, getEmail, getSubject, getbody, getTargetId) { | |
var action = component.get("c.sendMailMethod"); | |
action.setParams({ | |
'mMail': getEmail, | |
'mSubject': getSubject, | |
'mbody': getbody, | |
'mId' : getTargetId | |
}); | |
action.setCallback(this, function(response) { | |
var state = response.getState(); | |
if (state === "SUCCESS") { | |
var storeResponse = response.getReturnValue(); | |
component.set("v.mailStatus", true); | |
} | |
}); | |
$A.enqueueAction(action); | |
}, | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
global class EmailService implements Messaging.InboundEmailHandler { | |
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.Inboundenvelope envelope) { | |
Opportunity opp; | |
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult(); | |
try { | |
// Look for account whose name is the subject and create it if necessary | |
if ([select count() from Opportunity where Id =:email.subject.substringAfter('ref:').left(18)] == 0) { | |
opp = new Opportunity(); | |
opp.Name = email.subject; | |
opp.StageName = 'Draft'; | |
opp.CloseDate = system.today(); | |
insert opp; | |
} else { | |
opp = [select Id from Opportunity where Id =:email.subject.substringAfter('ref:').left(18)]; | |
} | |
Task tsk1=new Task(); | |
tsk1.Subject = email.Subject; | |
tsk1.OwnerId=UserInfo.getUserId(); | |
tsk1.Description = email.plainTextBody; | |
tsk1.WhatId = opp.Id; | |
tsk1.status = 'Completed'; | |
insert tsk1; | |
if(email.textAttachments != null) { | |
// Save attachments, if any | |
for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) { | |
Attachment attachment = new Attachment(); | |
attachment.Name = tAttachment.fileName; | |
attachment.Body = Blob.valueOf(tAttachment.body); | |
attachment.ParentId = opp.Id; | |
insert attachment; | |
} | |
} | |
if(email.binaryAttachments != null) { | |
for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) { | |
Attachment attachment = new Attachment(); | |
attachment.Name = bAttachment.fileName; | |
attachment.Body = bAttachment.body; | |
attachment.ParentId = opp.Id; | |
insert attachment; | |
} | |
} | |
result.success = true; | |
result.message = (email.subject + ' matched'); | |
} catch (Exception e) { | |
result.success = false; | |
result.message = 'Oops, I failed. Error : '+e.getMessage(); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment