PHP

HOW TO SEND ATTACHMENT WITH PHP MAIL

RANDOM BOUNDARY $bound = md5(time());

EMAIL HEADER

01

EMAIL HEADER $head = implode("\r\n", [   "MIME-Version: 1.0",   "Content-Type: multipart/mixed;    boundary=\"$bound\'"' ]);

EMAIL MESSAGE $body = implode("\r\n", [   "--$bound",   "Content-type: text/html; charset=utf-8",   "", "<p>MAIL MESSAGE</p>"  ]);

EMAIL MESSAGE

02

ATTACH FILE $file = "pic.jpg"; $body .= implode("\r\n", [   "", "--$boundary",   "Content-Type: application/octet-stream;   name=\"'.basename($file)."\"",   "Content-Transfer-Encoding: base64",   "Content-Disposition: attachment", "",   chunk_split(base64_encode   (file_get_contents($file))),   "--$boundary--" ]);

EMAIL ATTACHMENT

03

SEND! echo mail("jon@doe.com", "Subject", $body, $head) ? "OK" : "ERROR" ;

SEND EMAIL

04