Send Image as an attatchment in email using php mail() method | Techbirds

Posted on: January 2, 2014 /

Categories: PHP / Author Name: Shivek Parmar

Well If you are reading this post then you must be finding a solution to send an image in an email using php mail() function.
Well before that you must understand how an email body works. For shortcut you can go to your email accout, open any of the email there “View messaage source” or sometimes “View original”. There you will see many times differnt types of headers

Content-Type:

. For more details you can read “This”. We use different headers in email just like in HTML page. When we send an email with only text in the body the we can use

Content-type:text/html; charset=iso-8859-1\r\n”

Content-type:text/html; charset=iso-8859-1\r\n”

in header of php mail function. But when we want to send an Image in email then we have to use

Content-type:Image/png;

but at that time we will not be able to see the text in the same email. Therefore, we have to use multiple headers in mail() function to send an image.

$output_hex_string_img = $image; $output_bin_string_img = base64_decode($output_hex_string_img); $XXX = base64_encode( $output_bin_string_img ); $from_name = “Senders Name”; $from_mail = “[email protected]”; $replyto = “[email protected]”; $subject = “Test Email”; $message = “This is a test Email. Best Of Luck for your future!!!\r\r “; $mailto = ‘[email protected]’; $file = $XXX; $filename = “Print_shot.png”; $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/html; 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 .= $file.”\r\n\r\n”; $header .= “–“.$uid.”–“; mail($mailto, $subject, “”, $header);

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

$output_hex_string_img = $image;

                    $output_bin_string_img = base64_decode($output_hex_string_img);

                    $XXX = base64_encode( $output_bin_string_img );

                    $from_name = “Senders Name”;

                    $from_mail = “[email protected]”;

                    $replyto = “[email protected]”;

                    $subject = “Test Email”;

                $message = “This is a test Email. Best Of Luck for your future!!!\r\r “;

                    $mailto = ‘[email protected]’;

                        $file = $XXX;

                        $filename = “Print_shot.png”;

                        $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/html; 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 .= $file.”\r\n\r\n”;

                        $header .= “–“.$uid.”–“;

                        mail($mailto, $subject, “”, $header);

In the above code ‘$image’ contains “LONGTEXT” type of Image which is stored in Database.

609 total views, 1 views today

Share this Onfacebook-1947656twitter-1454593linkedin-6497884google-8052524