Tuesday 1 November 2016

Published 03:22:00 by with 0 comment

How to Send PHP mail with Attachment

snippest

You probably know how to send email with PHP, but it gets little trickier when you want to send attachment with PHP email. Here’s another example you can use to send attachment with PHP mail.

Create a HTML form with file input field similar to below, enctype attribute should be “multipart/form-data“, so that form-data is encoded as “multipart/form-data” when sent to server.

html
html
html
1
2
3
4
5
6
7
8
<form name=“form1” enctype=“multipart/form-data” method=“post” action=“”>
<label>
<input type=“file” name=“my_file” />
</label>
<label>
<input type=“submit” name=“button” value=“Submit” />
</label>
</form>

PHP mail with Attachment

Here’s the complete PHP code that sends PHP email with attachment.
Just create a PHP file with code below and point your HTML form to the
file, and don’t forget to change recipient address and email address in
the code.

php
php
php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
if($_POST && isset($_FILES[‘my_file’]))
{

$sender_email = ‘sender_mail@example.com’; //sender email
$recipient_email = ‘recipient_mail@example.com’; //recipient email
$subject = ‘Test mail’; //subject of email
$message = ‘This is body of the message’; //message body

//get file details we need
$file_tmp_name    = $_FILES[‘my_file’][‘tmp_name’];
$file_name        = $_FILES[‘my_file’][‘name’];
$file_size        = $_FILES[‘my_file’][‘size’];
$file_type        = $_FILES[‘my_file’][‘type’];
$file_error       = $_FILES[‘my_file’][‘error’];

if($file_error>0)
{
die(‘upload error’);
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, “r”);
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));

$boundary = md5(“fluxntech”);
//header
$headers = “MIME-Version: 1.0\r\n;
$headers .= “From:”.$from_email.\r\n;
$headers .= “Reply-To: “.$user_email.“” . \r\n;
$headers .= “Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n;

//plain text
$body = “–$boundary\r\n;
$body .= “Content-Type: text/plain; charset=ISO-8859-1\r\n;
$body .= “Content-Transfer-Encoding: base64\r\n\r\n;
$body .= chunk_split(base64_encode($message));

//attachment
$body .= “–$boundary\r\n;
$body .=“Content-Type: $file_type; name=\”$file_name\”\r\n;
$body .=“Content-Disposition: attachment; filename=\”$file_name\”\r\n;
$body .=“Content-Transfer-Encoding: base64\r\n;
$body .=“X-Attachment-Id: “.rand(1000,99999).\r\n\r\n;
$body .= $encoded_content;

$sentMail = @mail($sender_email, $subject, $body, $headers);

if($sentMail) //output success or failure messages
{
die(‘Thank you for your email’);
}else{
die(‘Could not send mail! Please check your PHP mail configuration.’);
}

}

  • Windows 10
  • Hacker
  • Archive
  • Categories
  • 4gb memory card
  • 64mb
  • Ajax
  • Anniversary
  • Belong
  • Capture
  • Windows 10
  • Hacker

 

That’s it, you can capture subject and message body from HTML form
and use them in the code. I hope this code will be useful in your PHP
projects. Good luck!

These content links are provided by Content.ad. Both Content.ad and the web site upon which the links are displayed may receive compensation when readers click on these links. Some of the content you are redirected to may be sponsored content. View our privacy policy here.

Family-Friendly Content
Website owners select the type of content that appears in our units. However, if you would like to ensure that Content.ad always displays family-friendly content on this device, regardless of what site you are on, check the option below. Learn More

To learn how you can use Content.ad to drive visitors to your content or add this service to your site, please contact us at info@content.ad.

      edit

0 comments:

Post a Comment