Sending email with attachment using amazon SES

前端 未结 4 802
星月不相逢
星月不相逢 2020-12-06 08:16

I am trying to send an email that contains an attachment (a pdf file) using the amazon SES API.

I am using Symfony2 so I included the AmazonWebServiceBundle in my pr

4条回答
  •  时光取名叫无心
    2020-12-06 08:25

    I managed to create a raw MIME Message in the below manner to send an email with attachment (.pdf file), using Amazon SES sendRawEmail. This is for plain javascript usage; one can surely refine it further add other Content-types too.

    1. Use a library like jsPDF & html2Canvas to create the PDF file & save the contents into a variable & get the base 64 data:

      var pdfOutput = pdf.output();
      var myBase64Data = btoa(pdfOutput);
      
    2. Use the below code to create the MIME Message. Note, the sequence is important else the email will end up as a text email exposing all the base 64 data :

      var fileName = "Attachment.pdf";
      var rawMailBody = "From: testmail@gmail.com\nTo: testmail@gmail.com\n";
      rawMailBody = rawMailBody + "Subject: Test Subject\n"; 
      rawMailBody = rawMailBody + "MIME-Version: 1.0\n"; 
      rawMailBody = rawMailBody + "Content-Type: multipart/mixed; boundary=\"NextPart\"\n\n"; 
      rawMailBody = rawMailBody + "--NextPart\n";  
      rawMailBody = rawMailBody + "Content-Type: application/octet-stream\n"; 
      rawMailBody = rawMailBody + "Content-Transfer-Encoding: base64\n"; 
      rawMailBody = rawMailBody + "Content-Disposition: attachment; filename=\"" + fileName + "\"\n\n"; 
      rawMailBody = rawMailBody + "Content-ID random2384928347923784letters\n"; 
      rawMailBody = rawMailBody + myBase64Data+"\n\n"; 
      rawMailBody = rawMailBody + "--NextPart\n";
      
    3. Invoke sendRawEmail:

       var params = {
            RawMessage: {
              Data: rawMailBody
            },
            Destinations: [],
            Source: 'testmail@gmail.com'
          };
      
       ses.sendRawEmail(params, function(err, data) {
         if (err) alert("Error: "+err); // an error occurred
         else     {
      
         alert("Success: "+data);           // successful response
         }
      
       }); 
      

提交回复
热议问题