Send an Email with Javascript and PHP

send an email with an attachment using PHP and JavaScript, you will need to use the PHP mail() function and the JavaScript FileReader API. Here is an example of how you could do this:

  1. First, create a form in your HTML file that allows the user to select a file to be attached to the email. The form should include a file input field, a subject input field, and a message input field.
  2. Add a JavaScript function to your HTML file that will be called when the form is submitted. This function should use the FileReader API to read the file selected by the user and convert it into a base64 encoded string.
  3. In your PHP file, use the mail() function to send the email. You will need to specify the recipient’s email address, the subject of the email, and the message body.
  4. In the message body, you will need to include the base64 encoded string of the attachment. To do this, you will need to use the PHP base64_encode() function to encode the string, and then include it in the message body using the appropriate MIME type.
  5. Finally, use the JavaScript window.location.href function to redirect the user to a thank you page or another page of your choice.

Here is an example of how this could look in your HTML file:

<form id="email-form" action="send-email.php" method="post">
  <label for="attachment">Attachment:</label><br>
  <input type="file" id="attachment" name="attachment"><br>
  <label for="subject">Subject:</label><br>
  <input type="text" id="subject" name="subject"><br>
  <label for="message">Message:</label><br>
  <textarea id="message" name="message"></textarea><br>
  <input type="submit" value="Send Email">
</form>

<script>
  document.getElementById('email-form').addEventListener('submit', function(e) {
    e.preventDefault();
    
    var file = document.getElementById('attachment').files[0];
    var reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function() {
      var attachment = reader.result;
      var subject = document.getElementById('subject').value;
      var message = document.getElementById('message').value;
      var formData = new FormData();
      formData.append('attachment', attachment);
      formData.append('subject', subject);
      formData.append('message', message);
      
      var xhr = new XMLHttpRequest();
      xhr.open('POST', 'send-email.php', true);
      xhr.send(formData);
      xhr.onload = function() {
        if (xhr.status === 200) {
          window.location.href = 'thank-you.html';
        }
      }
    }
  });
</script>
fetch('send-email.php', {
  method: 'POST',
  body: new FormData(form)
})
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.error(error));
<?php

$to = 'recipient@example.com';
$subject = 'Email with Attachment';
$message = 'This is the message body';
$headers = 'From: sender@example.com' . "\r\n";

// Set the boundary
$boundary = uniqid('np');

// Headers for attachment
$headers .= "\r\nMIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"_1_$boundary\"";

// Multipart boundary
$message = "This is a multipart message in MIME format.

--_1_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

$message

--_1_$boundary
Content-Type: application/octet-stream; name=\"attachment.txt\"
Content-Disposition: attachment
Content-Transfer-Encoding: base64

" . chunk_split(base64_encode(file_get_contents('path/to/attachment.txt'))) . "
--_1_$boundary--";

// Send the email
mail($to, $subject, $message, $headers);

?>