+ Start a Discussion
Supriya Adlinge 1Supriya Adlinge 1 

Wants to insert only pdf attachment from email to attachment object

I have configured email service ,I want to insert only pdf attachment to attachment object , Is there any way to get extension of email attached file.


Thanks
Best Answer chosen by Supriya Adlinge 1
Waqar Hussain SFWaqar Hussain SF
public class EmailProcess implements Messaging.InboundEmailHandler 
{
  // email contains subject, CC, body and attachment, envolope contants To & From address
  public Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.Inboundenvelope envelope) 
  {
    List<Attachment> attList = new List<Attachment>();
    // This will extract all blob attachments from email
    // For text type attachment Messaging.Inboundemail.TextAttachment can be used
    for (Messaging.Inboundemail.BinaryAttachment file : email.binaryAttachments) 
    {
      Attachment attachment = new Attachment();
      attachment.Name = file .fileName;
      attachment.Body = file .body;
      // some hardcoded or id extracted from email reference
      attachment.ParentId = parentId;
      attList .add(attachment);
   }
   if(attList .size()>0)
   {
     insert attList;
   }
  }
}

 

All Answers

Varun SinghVarun Singh
Hi @Supriya
You have to use js validation for extension ".pdf"
Visualforce component to upload a file 
<apex:inputFile required="true" value="{!document.body}" filename="{!document.name}" onchange="check(this)" id="filePath"/>


In the above line 'check(this)' is the javascript function. 
<script type = "text/javascript">
function check(obj) { 
var path = obj.value;
var ext = path.substring(path.lastIndexOf('.') + 1);

if(ext !="pdf")
{
obj.value = null;
window.alert("Please select only pdf");
return false;
}
}
</script>

I hope it is helpful for you please select my answer as best answer.
 
Waqar Hussain SFWaqar Hussain SF
public class EmailProcess implements Messaging.InboundEmailHandler 
{
  // email contains subject, CC, body and attachment, envolope contants To & From address
  public Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.Inboundenvelope envelope) 
  {
    List<Attachment> attList = new List<Attachment>();
    // This will extract all blob attachments from email
    // For text type attachment Messaging.Inboundemail.TextAttachment can be used
    for (Messaging.Inboundemail.BinaryAttachment file : email.binaryAttachments) 
    {
      Attachment attachment = new Attachment();
      attachment.Name = file .fileName;
      attachment.Body = file .body;
      // some hardcoded or id extracted from email reference
      attachment.ParentId = parentId;
      attList .add(attachment);
   }
   if(attList .size()>0)
   {
     insert attList;
   }
  }
}

 
This was selected as the best answer