Sending email with uploaded attachment using raw php



$upload_path = dirname(__FILE__) . '/uploads/';
if (is_dir($upload_path) === false)
    mkdir($upload_path, 0777);
    $to = 'test@gmail.com';

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
    $file = $path . $filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: " . $from_name . " \r\n";
    $header .= "Reply-To: " . $replyto . "\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--" . $uid . "\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message . "\r\n\r\n";
    $header .= "--" . $uid . "\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"\r\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n";
    $header .= $content . "\r\n\r\n";
    $header .= "--" . $uid . "--";
    if (mail($mailto, $subject, "", $header)) {
        echo"success"; // header('location: /?success');
    } else {
        echo"No";
        //header('location: /?success');
    }
}

//end mail_attachment

function mail_plain($mailto, $from_mail, $from_name, $replyto, $subject, $message) {
    $header = "From: " . $from_name . " \r\n";
    $header .= "Reply-To: " . $replyto . "\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";

   if (mail($mailto, $subject, $message, $header)) {
        echo"success"; //header('location: /?success');
    } else {
        echo"No"; //header('location: /?success');
    }
}

//end function mail_plain

if (!empty($_FILES['resume']['name'])) {
    $filename = time() . '_' . $_FILES['resume']['name'];

    if (move_uploaded_file($_FILES['resume']['tmp_name'], $upload_path . $filename)) {
        $attachment = $filename;
    }
}

$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$phone = $_POST['phone']; // not required
$comments = $_POST['message']; // required

$subject = "Email Subject";
$from = $email_from; //$_POST['reply_email'];

$message  = "Form details below.\n\n";
$message .= "Name: " . $name . "\n";
$message .= "Email: " . $email_from. "\n";
$message .= "Phone: " . $phone . "\n";
$message .= "Message: " . $comments . "\n";
$from_name = $name;

if (isset($attachment)) {
    //echo $return="Attach";
    mail_attachment($attachment, $upload_path, $to, $from, $from_name, $from, $subject, $message);
} else {
    //echo $return="No Attach";
    mail_plain($to, $from, $from_name, $from, $subject, $message);
}

header('Location: test.html');

Advertisements

One response to “Sending email with uploaded attachment using raw php

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s