16c01-3.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 68 行

PHP
68
字号
<?php// Prepare by setting a timezone, mail() uses this.date_default_timezone_set('America/New_York');// Save some values to send an email, these might have come from any source:$to = 'example@eliw.com';$subject = 'A sample email - Dual Format plus attachment';// Create a boundary string.  It needs to be unique (not in the text) so ...// We are going to use the sha1 algorithm to generate a 40 character string:$sep = sha1(date('r', time()));// Define the headers we want passed.$headers = "From: php@example.com\r\nX-Mailer: Custom PHP Script";// Add in our primary content boundary, and mime type specification:$headers .=    "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-{$sep}\"";// Read in our file attachment$attachment = file_get_contents('attachment.zip');// Base64 encode it so that the potentially binary file can safely be send// over email, which is a text based protocol.$encoded = base64_encode($attachment);// Now use chunk_split to automatically break this into lines that are at//  most 76 characters wide.  This is needed to match the email protocol.$attached = chunk_split($encoded);// Now the body of the message.  Separate each original part (message from//  attachments) with the first separator.  Then within that giving //  alternatives with the second separator.$body =<<<EOBODY--PHP-mixed-{$sep}Content-Type: multipart/alternative; boundary="PHP-alt-{$sep}"--PHP-alt-{$sep}Content-Type: text/plainThis is our sample email messageHello World!That's it for now--PHP-alt-{$sep}Content-Type: text/html<p>This is our sample email message</p><h2>Hello World!</h2><p>That's it for now.</p>--PHP-alt-{$sep}----PHP-mixed-{$sep}Content-Type: application/zip; name="attachment.zip"Content-Transfer-Encoding: base64Content-Disposition: attachment{$attached}--PHP-mixed-{$sep}--EOBODY;// Finally, send the emailmail($to, $subject, $body, $headers);?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?