Jump to content

Send an email with attachment


eduardoribeiro

Recommended Posts

Hi,

 

I want to make a form to send by email, but I want the user upload a file and then send the email. I have the script to upload and the script to send a email with attachment, but how can I make first the upload of the file and then set that file to be sended with the data collected from the form?

 

Can anyone help me, I'm starting to learn PHP and it's a bit difficult to me, because I'm used to ASP.

 

Thank you in advance,

 

Eduardo Ribeiro

Link to comment
https://forums.phpfreaks.com/topic/54787-send-an-email-with-attachment/
Share on other sites

  • 2 weeks later...

here is the code

 

.......

 

 

 

<?php

 

/* Mailer with Attachments */

 

$action = $_REQUEST['action'];

global $action;

 

function showForm() {

?>

 

<form enctype="multipart/form-data" name="send" method="post" action="<?=$_SERVER['PHP_SELF']?>">

<input type="hidden" name="action" value="send" />

<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />

<table border="0" cellpadding="2" cellspacing="2">

<tr><td colspan="2" align="center"><strong><font color="#FF0000">Send E-mail</font></strong></td></tr>

<tr><td>Recipient Name:</td><td> <input name="to_name" size="50" /></td></tr>

<tr><td>Recipient Email:</td><td> <input name="to_email" size="50" /></td></tr>

<tr><td>From Name:</td><td><input name="from_name" size="50" /></td></tr>

<tr><td>From Email:</td><td> <input name="from_email" size="50" /></td></tr>

<tr><td>Subject:</td><td><input name="subject" size="50" /></td></tr>

<tr><td valign="top">Message:</td><td><textarea name="body" rows="10" cols="50"></textarea></td></tr>

<tr><td colspan="2"><strong>(Attach the files with extension *.doc,*.txt,*.pdf)</strong></td></tr>

<tr><td>Attachment1:</td><td><input type="file" name="attachment" size="50" /></td></tr>

<tr><td>Attachment2:</td><td><input type="file" name="attachment1" size="50" /></td></tr>

<tr><td colspan="2" align="center"><input type="submit" value="Send Email" /></td></tr>

</table>

 

<!--

<p>Recipient Name: <input name="to_name" size="50" /><br />

Recipient Email: <input name="to_email" size="50" /><br />

From Name: <input name="from_name" size="50" /><br />

From Email: <input name="from_email" size="50" /><br />

Subject: <input name="subject" size="50" /><br />

Message: <textarea name="body" rows="10" cols="50"></textarea><br />

Attachment: <input type="file" name="attachment" size="50" /><br />

Attachment: <input type="file" name="attachment1" size="50" /><br />

<input type="submit" value="Send Email" /></p>

-->

 

 

 

<?php

}

 

function sendMail() {

$to_name = $_POST['to_name'];

$from_name =$_POST['from_name'];

$subject = stripslashes($_POST['subject']);

$body = stripslashes($_POST['body']);

$to_email =$_POST['to_email'];

 

$headers = "From: $from_name <" . $_POST['from_email'] . ">\n";

$headers .= "Reply-To: <" . $_POST['from_email'] . ">\n";

$headers .= "MIME-Version: 1.0\n";

$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";

$headers .= "X-Sender: $from_name <" . $_POST['from_email'] . ">\n";

$headers .= "X-Mailer: PHP4\n";

$headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal

$headers .= "Return-Path: <" . $_POST['from_email'] . ">\n";

$headers .= "This is a multi-part message in MIME format.\n";

$headers .= "------=MIME_BOUNDRY_main_message \n";

$headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n";

$message = "------=MIME_BOUNDRY_message_parts\n";

$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";

$message .= "Content-Transfer-Encoding: quoted-printable\n";

$message .= "\n";

$message .= "$body\n";

$message .= "\n";

$message .= "------=MIME_BOUNDRY_message_parts--\n";

$message .= "\n";

 

foreach($_FILES as $file => $value) {

echo $_tmpname = $_FILES[$file]['tmp_name'];

echo $_filename = $_FILES[$file]['name'];

if (is_uploaded_file($_tmpname)) {

$pathinfo = pathinfo($_filename);

$ext = $pathinfo['extension'];

if ($ext=="doc" || $ext=="pdf" || $ext=="txt") {

$fp = fopen($_tmpname, "rb");

$data = fread($fp, filesize($_tmpname));

$data = chunk_split(base64_encode($data));

$message .= "------=MIME_BOUNDRY_main_message\n";

$message .= "Content-Type: application/octet-stream;\n\tname=\"" . $_filename . "\"\n";

$message .= "Content-Transfer-Encoding: base64\n";

$message .= "Content-Disposition: attachment;\n\tfilename=\"" . $_filename . "\"\n\n";

$message .= $data;

$message .= "\n\n";

fclose($fp);

}

else {

echo "(Attachment file type not supported. Attachment was not sent.)

";

}

}

}

 

$message .= "------=MIME_BOUNDRY_main_message--\n";

//$ok = mail($to_email, $subject, $message, $headers);

if ($ok == 1) print "We've received your application.

";

else print "Application could not be sent.

";

}

?>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

 

<style="css" type="text/css">

<!--

body {

margin: 0px;

font-family: Verdana, Arial, Helvetica, sans-serif;

font-size: 12px;

}

a {color: #0000ff}

-->

</style>

</head>

<body>

 

<?php

switch ($action) {

case "send":

sendMail();

showForm();

break;

default:

showForm();

}

?>

 

</body>

</html>

 

~suma237

You are doing so many things to get teh Answer of 2+2. Its Really Too simple

I use this Class for sending Attachment

Its So simple and easy

$ext["attach"] = "atch.txt,test.wav";
$mail = new mail("admin@localhost", "Mail Subject", "Mail Body", $ext)

You can also use in this way

$mail = new mail("admin@localhost", "Mail Subject","Mail Body");
$test->atch("atch.txt,test.wav");//Set Attachment

And Its So Easy

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.