title: AWS Lambda in Python to send email with attachment
body: <p>I have created static web site which I am planning to host in S3 bucket. This HTML has a form and attachment feature which will be sent to my email id when user will attach a file and hit 'Upload'. Below is my HTML code snippets - </p>
  
  <pre><code>&lt;form enctype="multipart/form-data"  name="fileinfo"&gt;
              &lt;label&gt;Your email address:&lt;/label&gt;
              &lt;input id = "email "type="email" autocomplete="on" autofocus name="userid" placeholder="email" required size="32" maxlength="64" /&gt;&lt;br /&gt;
              &lt;label&gt;Custom file label:&lt;/label&gt;
              &lt;input type="text" name="filelabel" size="12" maxlength="32" /&gt;&lt;br /&gt;
              &lt;label&gt;File to stash:&lt;/label&gt;
              &lt;input type="file" name="file" id = "fileUpload" required /&gt;
               &lt;input type="button" id = "upload" value="Upload!" /&gt;
          &lt;/form&gt;​
  </code></pre>
  
  <p>and then Jquery to trigger POST request to AWS API gateway</p>
  
  <pre><code>$('#upload').on("click", function (){
    var oOutput = document.querySelector("div"),
        oData = new FormData(form);
  
      var xhttp = new XMLHttpRequest();
  
      xhttp.onreadystatechange = function() {
            if (this.readyState == 4 &amp;&amp; this.status == 200) {
              // Typical action to be performed when the document is ready:
               console.log(xhttp.responseText);
              console.log(xhttp.statusText);
              console.log(xhttp.status);
              oOutput.innerHTML = xhttp.responseText;
              } else {
                  oOutput.innerHTML = "Error " + xhttp.status + " occurred when trying to upload your file.&lt;br \/&gt;";
              }
            };
  
      xhttp.open("POST", "https://myLambdaAPIURL", true);
      xhttp.setRequestHeader("Content-type", "application/json;charset=UTF-8");
      xhttp.send(JSON.stringify({Email:$('#email').val(),fileUpload:$('#fileUpload').val()}));
  </code></pre>
  
  <p>API gateway in AWS has all the default configurations to process the json format.</p>
  
  <p>My AWS lambda code which works fine to send just an email.</p>
  
  <pre><code>import smtplib , os
  from email.mime.image import MIMEImage
  from email.mime.multipart import MIMEMultipart
  from email.mime.application import MIMEApplication
  from email.mime.base import MIMEBase
  from email.message import Message
  from email.encoders import encode_base64
  from email.mime.text import MIMEText
  from mimetypes import guess_type
  
  
  def lambda_handler(event, context):
      msg = MIMEMultipart()
      msg['Subject'] = 'File Uploaded'
      msg['From'] = 'user@gmail.com'
      msg['To'] = 'user@gmail.com'
      emailFrom = "user@gmail.com"
      emailTo ="user@gmail.com"
  
      for file in event['fileUpload']:
          mimetype, encoding = guess_type(file)
          if mimetype == None:
              mimetype = "text/plain"
          mimetype = mimetype.split('/',1)    
          fp = open(file, "rb")       
          attachment.set_payload(fp.read())
          fp.close()
          encode_base64(attachment)
          attachment.add_header('Content-Disposition', 'attachment', file=os.path.basename(file))
          msg.attach(attachment)
      s = smtplib.SMTP('smtp.gmail.com:587')
      s.starttls()
      s.login('user@gmail.com','password')
      response = s.sendmail(emailFrom, emailTo, msg.as_string())
      s.quit()
  
  return response
  </code></pre>
  
  <p>But when I attach a file and try to process it, the fake path error comes </p>
  
  <blockquote>
    <p>{"stackTrace": [["/var/task/pyLambda.py", 24, "lambda_handler", "fo = open(filename, \"rb\")"]], "errorType": "IOError", "errorMessage": "[Errno 2] No such file or directory: u'C:\\fakepath\\testing.txt'"}</p>
  </blockquote>
  
  <p>so I am thinking may be I can use \tmp directory in AWS lambda but not sure how to do that.</p>
  
  <p>Any help or any guidance is highly appreciated.</p>