开发者

email file via php form

开发者 https://www.devze.com 2022-12-09 09:00 出处:网络
I researched over the internet, but could not find what i need :/ I have a contact form, (php). I dont have any database. it is a simple php email form, but now I need to make it with file attachmen

I researched over the internet, but could not find what i need :/

I have a contact form, (php). I dont have any database. it is a simple php email form, but now I need to make it with file attachment :/ how can I email file via contact form? visitor will browse any file from his computer and send email via 开发者_如何转开发form.

appreciate!!


I've taken a look at the link in the comments and cleaned up a few things into a function. For one thing I've used an associative array of arguments and heredocs, since the use of php tags and output buffering in the example weren't exactly clean (or as clean as PHP will get).

http://aramk.com/php/php-sending-an-email-attachment/

emailFile(array(
    'to' => 'your@email.com',
    'from' => 'my@email.com',
    'subject' => 'Some Subject',
    'message' => '<b>Hello!</b>',
    'plain ' => 'Get a new email client!',
    'file' => '/path/to/file'
));

You can pass along the file path from $_FILES into the "file" argument.


Use a file upload script like you can find on the internet (e.g. http://www.w3schools.com/php/php_file_upload.asp). Then you have some temporary file, let's call it file_to_send. Then just use the code as mentioned in the comments by I.devries ( http://webcheatsheet.com/php/send_email_text_html_attachment.php ) for sending the attachment together with your mail.

Below you can find some copy pasted code from the sites mentioned above, but with the necessary adjustements.

HTML:

<form action="upload_file.php" method="post" enctype="multipart/form-data">
    <label for="file_to_send">Filename:</label>
    <input type="file" name="file_to_send" id="file_to_send"><br>
    <input type="submit" name="submit" value="Submit">
</form>

PHP:

<?php 
//define the receiver of the email 
$to = 'youraddress@example.com'; 
//define the subject of the email 
$subject = 'Test email with attachment'; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n 
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents($_FILES['file_to_send']['tmp_name']))); 
//define the body of the message. 
ob_start(); //Turn on output buffering 
?> 
--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2> 
<p>This is something with <b>HTML</b> formatting.</p> 

--PHP-alt-<?php echo $random_hash; ?>-- 

--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: application/zip; name="attachment.zip"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment  

<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 

<?php 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = @mail( $to, $subject, $message, $headers ); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消