Send form data email php

This tutorial explains how to send form data email php. The function accepts post data from the form and send the email on form submissions.

Please check the tutorial which explains how to send form data and upload file as email attachment and also send all files in a folder as attachment.

function prepareEmail( $formData ) {
    
    // email fields: to, from, subject, and so on
    $to = "test@gmail.com";
    $from = "admin@admin.com"; 
    $subject =""; 
    $message = "Uploaded File\n";
    $message .= "Name :". $formData['name']."\n";
    $message .= "Email Address :". $formData['email']."\n";
    $headers = "From: $from";

    // boundary 
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

    // headers for attachment 
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

    // multipart boundary 
    $message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
    $message .= "--{$mime_boundary}\n";
    
    $emailData = array (
        'to' => $to,
        'from' => $from,
        'subject' => $subject,
        'headers' => $headers,
        'message' => $message
    );
    
    return $emailData;
    
}

Share this Tutorial