前提・実現したいこと
自分、あまりPHPには詳しくないですが、iPhoneから遠隔サーバを通してメールを送るためのSendmailのコードを書いています。メールには複数の添付書類をつけることになっているので、multipartのContent-Typeを指定しています。以下のコードがそのPHPのコードです。
該当のソースコード
PHP
1<?php 2 error_reporting(E_ALL); 3 ini_set('display_errors', 1); 4 mb_internal_encoding ("UTF-8"); 5 6 if(isset($_FILES) && (bool) $_FILES) { 7 $files = array(); 8 foreach($_FILES as $name=>$file) { 9 $file_name = $file['name']; 10 $temp_name = $file['tmp_name']; 11 $file_type = $file['type']; 12 $path_parts = pathinfo($file_name); 13 $ext = $path_parts['extension']; 14 array_push($files, $file); 15 } 16 17 // email fields: to, from, subject, and so on 18 $to = $_POST['mail_recipient']; 19 $cc = $_POST['mail_copy']; 20 $bcc = $_POST['mail_bcc']; 21 $from = $_POST['mail_sender']; 22 $myReturn = $_POST['mail_return']; 23 $subject = $_POST['mail_subject']; 24 $message = $_POST['mail_message']; 25 $returnpath = "-f" . $myReturn; 26 $headers = "From: $from\r\n"; 27 $headers .= "Cc: $cc\r\n"; 28 $headers .= "Bcc: $bcc"; 29 30 // boundary 31 $semi_rand = md5(time()); 32 $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 33 34 // headers for attachment 35 $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 36 37 // multipart boundary 38 $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain;\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 39 $message .= "--{$mime_boundary}\n"; 40 41 // preparing attachments 42 for($x=0; $x<count($files); $x++){ 43 $file = fopen($files[$x]['tmp_name'], "rb"); 44 $data = fread($file, filesize($files[$x]['tmp_name'])); 45 fclose($file); 46 $data = chunk_split(base64_encode($data)); 47 $name = $files[$x]['name']; 48 $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" . 49 "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" . 50 "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 51 $message .= "--{$mime_boundary}\n"; 52 } 53 54 // send 55 $ok = mail($to, $subject, $message, $headers, $returnpath); 56 if ($ok) { 57 echo "<p>mail sent to $to!</p>"; 58 } else { 59 echo "<p>mail could not be sent!</p>"; 60 } 61 } else { 62 // email fields: to, from, subject, and so on 63 $to = $_POST['mail_recipient']; 64 $cc = $_POST['mail_copy']; 65 $bcc = $_POST['mail_bcc']; 66 $from = $_POST['mail_sender']; 67 $myReturn = $_POST['mail_return']; 68 $subject = $_POST['mail_subject']; 69 $message = $_POST['mail_message']; 70 $returnpath = "-f" . $myReturn; 71 $headers = "From: $from\r\n"; 72 $headers .= "Cc: $cc\r\n"; 73 $headers .= "Bcc: $bcc"; 74 75 // send 76 $ok = mail($to, $subject, $message, $headers, $returnpath); 77 if ($ok) { 78 echo "<p>mail sent to $to!</p>"; 79 } else { 80 echo "<p>mail could not be sent!</p>"; 81 } 82 } 83?>
それで上のコードではメッセージの本文はPlain Textなんですが、それをHTML形式のテキストを送りたいのですが、どうしたらいいでしょうか?このURLにある説明を見ると、
PHP
1$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
とContent-typeを「text/html」にしろ、とあります。ただ自分の上のコードではすでにContent-typeを「multipart/mixed」と指定しています。すると両方を指定することはできるのでしょうか?
よろしくお願いします。
更新1
どうも自分が誤解していたようです。自分のPHPのコードで
php
1$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain;\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
とあるので、ここを「text/html」に変えればいいんでしょうね。ただこのURLにあるサイトによるとPlain TextとHTML Textの両方を送る、とあります。
更新2
その後以下のように「Content-Type」を「text/html」に変更してみました。でもメールの内容はPlain Textで送られます。何がいけないのでしょうか?
PHP
1<?php 2 error_reporting(E_ALL); 3 ini_set('display_errors', 1); 4 mb_internal_encoding ("UTF-8"); 5 6 if(isset($_FILES) && (bool) $_FILES) { 7 $files = array(); 8 foreach($_FILES as $name=>$file) { 9 $file_name = $file['name']; 10 $temp_name = $file['tmp_name']; 11 $file_type = $file['type']; 12 $path_parts = pathinfo($file_name); 13 $ext = $path_parts['extension']; 14 array_push($files, $file); 15 } 16 17 // email fields: to, from, subject, and so on 18 $to = $_POST['mail_recipient']; 19 $cc = $_POST['mail_copy']; 20 $bcc = $_POST['mail_bcc']; 21 $from = $_POST['mail_sender']; 22 $myReturn = $_POST['mail_return']; 23 $subject = $_POST['mail_subject']; 24 $message = $_POST['mail_message']; 25 $htmlMessage = $_POST['mail_html']; 26 $returnpath = "-f" . $myReturn; 27 $headers = "From: $from\r\n"; 28 $headers .= "Cc: $cc\r\n"; 29 $headers .= "Bcc: $bcc"; 30 31 // boundary 32 $semi_rand = md5(time()); 33 $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 34 35 // headers for attachment 36 $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 37 38 // multipart boundary 39 $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html;\n" . "Content-Transfer-Encoding: 7bit\n\n" . $htmlMessage . "\n\n"; 40 $message .= "--{$mime_boundary}\n"; 41 42 // preparing attachments 43 for($x=0; $x<count($files); $x++){ 44 $file = fopen($files[$x]['tmp_name'], "rb"); 45 $data = fread($file, filesize($files[$x]['tmp_name'])); 46 fclose($file); 47 $data = chunk_split(base64_encode($data)); 48 $name = $files[$x]['name']; 49 $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" . 50 "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" . 51 "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 52 $message .= "--{$mime_boundary}\n"; 53 } 54 55 // send 56 $ok = mail($to, $subject, $message, $headers, $returnpath); 57 if ($ok) { 58 echo "<p>mail sent to $to!</p>"; 59 } else { 60 echo "<p>mail could not be sent!</p>"; 61 } 62 } else { 63 // email fields: to, from, subject, and so on 64 $to = $_POST['mail_recipient']; 65 $cc = $_POST['mail_copy']; 66 $bcc = $_POST['mail_bcc']; 67 $from = $_POST['mail_sender']; 68 $myReturn = $_POST['mail_return']; 69 $subject = $_POST['mail_subject']; 70 $message = $_POST['mail_message']; 71 $returnpath = "-f" . $myReturn; 72 $headers = "From: $from\r\n"; 73 $headers .= "Cc: $cc\r\n"; 74 $headers .= "Bcc: $bcc"; 75 76 // send 77 $ok = mail($to, $subject, $message, $headers, $returnpath); 78 if ($ok) { 79 echo "<p>mail sent to $to!</p>"; 80 } else { 81 echo "<p>mail could not be sent!</p>"; 82 } 83 } 84?>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。