この記事は最終更新日から1年以上経過しています。
PHPのmb_send_mail関数でファイルを添付してメールを送信するスクリプト
1 | $to = "mailto@example.jp" ; |
2 | $from = "mailfrom@example.jp" ; |
8 | $boundary = md5(uniqid(rand())); |
11 | $header = "From: $to\n" |
13 | . "X-Mailer: PHP/" .phpversion(). "\n" |
14 | . "MIME-version: 1.0\n" |
15 | . "Content-Type: multipart/mixed;\n" |
16 | . "\tboundary=\"$boundary\"\n" ; |
18 | $msg = "This is a multi-part message in MIME format.\n\n" |
20 | . "Content-Type: text/plain; charset=iso-2022-jp\n" |
21 | . "Content-Transfer-Encoding: 7bit\n\n" |
29 | if ( $_FILES [ "upfile" ][ "error" ] == UPLOAD_ERR_OK) { |
31 | $tmp = $_FILES [ "upfile" ][ "tmp_name" ]; |
32 | $name = $_FILES [ "upfile" ][ "name" ]; |
33 | $size = $_FILES [ "upfile" ][ "size" ]; |
35 | $array = explode ( "." , $name ); |
37 | $ext = $array [ $nr - 1]; |
39 | $name = date ( "U" ). "." . $ext ; |
41 | $fp = fopen ( $tmp , "r" ); |
42 | $contents = fread ( $fp , filesize ( $tmp )); |
44 | $f_encoded = chunk_split ( base64_encode ( $contents )); |
46 | $msg .= "--$boundary\n" |
47 | . "Content-Type: application/octet-stream;\n" |
48 | . "\tname=\"$name\"\n" |
49 | . "Content-Transfer-Encoding: base64\n" |
50 | . "Content-Disposition: attachment;\n" |
51 | . "\tfilename=\"$name\"\n\n" |
62 | $orgEncoding = mb_internal_encoding(); |
65 | mb_internal_encoding( "utf-8" ) ; |
69 | if (!mb_send_mail( $mailto , $subject , $msg , $header )) { |
70 | echo "メールの送信に失敗しました。" ; |
74 | mb_internal_encoding( $orgEncoding ); |