質問編集履歴

4

削除された内容の復元を行いました

2020/12/01 04:37

投稿

tomato879241
tomato879241

スコア133

test CHANGED
File without changes
test CHANGED
@@ -1 +1,375 @@
1
+ ### 前提・実現したいこと
2
+
3
+ 自分、あまりPHPには詳しくないですが、iPhoneから遠隔サーバを通してメールを送るためのSendmailのコードを書いています。メールには複数の添付書類をつけることになっているので、**multipart**のContent-Typeを指定しています。以下のコードがそのPHPのコードです。
4
+
5
+ ### 該当のソースコード
6
+
7
+ ```PHP
8
+
9
+ <?php
10
+
11
+ error_reporting(E_ALL);
12
+
13
+ ini_set('display_errors', 1);
14
+
15
+ mb_internal_encoding ("UTF-8");
16
+
17
+
18
+
19
+ if(isset($_FILES) && (bool) $_FILES) {
20
+
21
+ $files = array();
22
+
23
+ foreach($_FILES as $name=>$file) {
24
+
25
+ $file_name = $file['name'];
26
+
27
+ $temp_name = $file['tmp_name'];
28
+
29
+ $file_type = $file['type'];
30
+
31
+ $path_parts = pathinfo($file_name);
32
+
33
+ $ext = $path_parts['extension'];
34
+
35
+ array_push($files, $file);
36
+
37
+ }
38
+
39
+
40
+
1
- xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx
41
+ // email fields: to, from, subject, and so on
42
+
43
+ $to = $_POST['mail_recipient'];
44
+
45
+ $cc = $_POST['mail_copy'];
46
+
47
+ $bcc = $_POST['mail_bcc'];
48
+
49
+ $from = $_POST['mail_sender'];
50
+
51
+ $myReturn = $_POST['mail_return'];
52
+
53
+ $subject = $_POST['mail_subject'];
54
+
55
+ $message = $_POST['mail_message'];
56
+
57
+ $returnpath = "-f" . $myReturn;
58
+
59
+ $headers = "From: $from\r\n";
60
+
61
+ $headers .= "Cc: $cc\r\n";
62
+
63
+ $headers .= "Bcc: $bcc";
64
+
65
+
66
+
67
+ // boundary
68
+
69
+ $semi_rand = md5(time());
70
+
71
+ $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
72
+
73
+
74
+
75
+ // headers for attachment
76
+
77
+ $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
78
+
79
+
80
+
81
+ // multipart boundary
82
+
83
+ $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";
84
+
85
+ $message .= "--{$mime_boundary}\n";
86
+
87
+
88
+
89
+ // preparing attachments
90
+
91
+ for($x=0; $x<count($files); $x++){
92
+
93
+ $file = fopen($files[$x]['tmp_name'], "rb");
94
+
95
+ $data = fread($file, filesize($files[$x]['tmp_name']));
96
+
97
+ fclose($file);
98
+
99
+ $data = chunk_split(base64_encode($data));
100
+
101
+ $name = $files[$x]['name'];
102
+
103
+ $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
104
+
105
+ "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
106
+
107
+ "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
108
+
109
+ $message .= "--{$mime_boundary}\n";
110
+
111
+ }
112
+
113
+
114
+
115
+ // send
116
+
117
+ $ok = mail($to, $subject, $message, $headers, $returnpath);
118
+
119
+ if ($ok) {
120
+
121
+ echo "<p>mail sent to $to!</p>";
122
+
123
+ } else {
124
+
125
+ echo "<p>mail could not be sent!</p>";
126
+
127
+ }
128
+
129
+ } else {
130
+
131
+ // email fields: to, from, subject, and so on
132
+
133
+ $to = $_POST['mail_recipient'];
134
+
135
+ $cc = $_POST['mail_copy'];
136
+
137
+ $bcc = $_POST['mail_bcc'];
138
+
139
+ $from = $_POST['mail_sender'];
140
+
141
+ $myReturn = $_POST['mail_return'];
142
+
143
+ $subject = $_POST['mail_subject'];
144
+
145
+ $message = $_POST['mail_message'];
146
+
147
+ $returnpath = "-f" . $myReturn;
148
+
149
+ $headers = "From: $from\r\n";
150
+
151
+ $headers .= "Cc: $cc\r\n";
152
+
153
+ $headers .= "Bcc: $bcc";
154
+
155
+
156
+
157
+ // send
158
+
159
+ $ok = mail($to, $subject, $message, $headers, $returnpath);
160
+
161
+ if ($ok) {
162
+
163
+ echo "<p>mail sent to $to!</p>";
164
+
165
+ } else {
166
+
167
+ echo "<p>mail could not be sent!</p>";
168
+
169
+ }
170
+
171
+ }
172
+
173
+ ?>
174
+
175
+ ```
176
+
177
+ それで上のコードではメッセージの本文はPlain Textなんですが、それをHTML形式のテキストを送りたいのですが、どうしたらいいでしょうか?[このURL](https://www.tutorialrepublic.com/php-tutorial/php-send-email.php)にある説明を見ると、
178
+
179
+ ```PHP
180
+
181
+ $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
182
+
183
+ ```
184
+
185
+ とContent-typeを「text/html」にしろ、とあります。ただ自分の上のコードではすでにContent-typeを「multipart/mixed」と指定しています。すると両方を指定することはできるのでしょうか?
186
+
187
+ よろしくお願いします。
188
+
189
+ ### 更新1
190
+
191
+ どうも自分が誤解していたようです。自分のPHPのコードで
192
+
193
+ ```php
194
+
195
+ $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";
196
+
197
+ ```
198
+
199
+ とあるので、ここを「text/html」に変えればいいんでしょうね。ただ[このURL](https://sendgrid.kke.co.jp/blog/?p=8262)にあるサイトによるとPlain TextとHTML Textの両方を送る、とあります。
200
+
201
+ ### 更新2
202
+
203
+ その後以下のように「Content-Type」を「text/html」に変更してみました。でもメールの内容はPlain Textで送られます。何がいけないのでしょうか?
204
+
205
+ ```PHP
206
+
207
+ <?php
208
+
209
+ error_reporting(E_ALL);
210
+
211
+ ini_set('display_errors', 1);
212
+
213
+ mb_internal_encoding ("UTF-8");
214
+
215
+
216
+
217
+ if(isset($_FILES) && (bool) $_FILES) {
218
+
219
+ $files = array();
220
+
221
+ foreach($_FILES as $name=>$file) {
222
+
223
+ $file_name = $file['name'];
224
+
225
+ $temp_name = $file['tmp_name'];
226
+
227
+ $file_type = $file['type'];
228
+
229
+ $path_parts = pathinfo($file_name);
230
+
231
+ $ext = $path_parts['extension'];
232
+
233
+ array_push($files, $file);
234
+
235
+ }
236
+
237
+
238
+
239
+ // email fields: to, from, subject, and so on
240
+
241
+ $to = $_POST['mail_recipient'];
242
+
243
+ $cc = $_POST['mail_copy'];
244
+
245
+ $bcc = $_POST['mail_bcc'];
246
+
247
+ $from = $_POST['mail_sender'];
248
+
249
+ $myReturn = $_POST['mail_return'];
250
+
251
+ $subject = $_POST['mail_subject'];
252
+
253
+ $message = $_POST['mail_message'];
254
+
255
+ $htmlMessage = $_POST['mail_html'];
256
+
257
+ $returnpath = "-f" . $myReturn;
258
+
259
+ $headers = "From: $from\r\n";
260
+
261
+ $headers .= "Cc: $cc\r\n";
262
+
263
+ $headers .= "Bcc: $bcc";
264
+
265
+
266
+
267
+ // boundary
268
+
269
+ $semi_rand = md5(time());
270
+
271
+ $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
272
+
273
+
274
+
275
+ // headers for attachment
276
+
277
+ $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
278
+
279
+
280
+
281
+ // multipart boundary
282
+
283
+ $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";
284
+
285
+ $message .= "--{$mime_boundary}\n";
286
+
287
+
288
+
289
+ // preparing attachments
290
+
291
+ for($x=0; $x<count($files); $x++){
292
+
293
+ $file = fopen($files[$x]['tmp_name'], "rb");
294
+
295
+ $data = fread($file, filesize($files[$x]['tmp_name']));
296
+
297
+ fclose($file);
298
+
299
+ $data = chunk_split(base64_encode($data));
300
+
301
+ $name = $files[$x]['name'];
302
+
303
+ $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
304
+
305
+ "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
306
+
307
+ "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
308
+
309
+ $message .= "--{$mime_boundary}\n";
310
+
311
+ }
312
+
313
+
314
+
315
+ // send
316
+
317
+ $ok = mail($to, $subject, $message, $headers, $returnpath);
318
+
319
+ if ($ok) {
320
+
321
+ echo "<p>mail sent to $to!</p>";
322
+
323
+ } else {
324
+
325
+ echo "<p>mail could not be sent!</p>";
326
+
327
+ }
328
+
329
+ } else {
330
+
331
+ // email fields: to, from, subject, and so on
332
+
333
+ $to = $_POST['mail_recipient'];
334
+
335
+ $cc = $_POST['mail_copy'];
336
+
337
+ $bcc = $_POST['mail_bcc'];
338
+
339
+ $from = $_POST['mail_sender'];
340
+
341
+ $myReturn = $_POST['mail_return'];
342
+
343
+ $subject = $_POST['mail_subject'];
344
+
345
+ $message = $_POST['mail_message'];
346
+
347
+ $returnpath = "-f" . $myReturn;
348
+
349
+ $headers = "From: $from\r\n";
350
+
351
+ $headers .= "Cc: $cc\r\n";
352
+
353
+ $headers .= "Bcc: $bcc";
354
+
355
+
356
+
357
+ // send
358
+
359
+ $ok = mail($to, $subject, $message, $headers, $returnpath);
360
+
361
+ if ($ok) {
362
+
363
+ echo "<p>mail sent to $to!</p>";
364
+
365
+ } else {
366
+
367
+ echo "<p>mail could not be sent!</p>";
368
+
369
+ }
370
+
371
+ }
372
+
373
+ ?>
374
+
375
+ ```

3

xxxxx

2020/12/01 04:37

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,395 +1 @@
1
- ### 前提・実現したいこと
2
-
3
-
4
-
5
- 自分、あまりPHPには詳しくないですが、iPhoneから遠隔サーバを通してメールを送るためのSendmailのコードを書いています。メールには複数の添付書類をつけることになっているので、**multipart**のContent-Typeを指定しています。以下のコードがそのPHPのコードです。
6
-
7
-
8
-
9
- ### 該当のソースコード
10
-
11
-
12
-
13
- ```PHP
14
-
15
- <?php
16
-
17
- error_reporting(E_ALL);
18
-
19
- ini_set('display_errors', 1);
20
-
21
- mb_internal_encoding ("UTF-8");
22
-
23
-
24
-
25
- if(isset($_FILES) && (bool) $_FILES) {
26
-
27
- $files = array();
28
-
29
- foreach($_FILES as $name=>$file) {
30
-
31
- $file_name = $file['name'];
32
-
33
- $temp_name = $file['tmp_name'];
34
-
35
- $file_type = $file['type'];
36
-
37
- $path_parts = pathinfo($file_name);
38
-
39
- $ext = $path_parts['extension'];
40
-
41
- array_push($files, $file);
42
-
43
- }
44
-
45
-
46
-
47
- // email fields: to, from, subject, and so on
1
+ xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx
48
-
49
- $to = $_POST['mail_recipient'];
50
-
51
- $cc = $_POST['mail_copy'];
52
-
53
- $bcc = $_POST['mail_bcc'];
54
-
55
- $from = $_POST['mail_sender'];
56
-
57
- $myReturn = $_POST['mail_return'];
58
-
59
- $subject = $_POST['mail_subject'];
60
-
61
- $message = $_POST['mail_message'];
62
-
63
- $returnpath = "-f" . $myReturn;
64
-
65
- $headers = "From: $from\r\n";
66
-
67
- $headers .= "Cc: $cc\r\n";
68
-
69
- $headers .= "Bcc: $bcc";
70
-
71
-
72
-
73
- // boundary
74
-
75
- $semi_rand = md5(time());
76
-
77
- $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
78
-
79
-
80
-
81
- // headers for attachment
82
-
83
- $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
84
-
85
-
86
-
87
- // multipart boundary
88
-
89
- $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";
90
-
91
- $message .= "--{$mime_boundary}\n";
92
-
93
-
94
-
95
- // preparing attachments
96
-
97
- for($x=0; $x<count($files); $x++){
98
-
99
- $file = fopen($files[$x]['tmp_name'], "rb");
100
-
101
- $data = fread($file, filesize($files[$x]['tmp_name']));
102
-
103
- fclose($file);
104
-
105
- $data = chunk_split(base64_encode($data));
106
-
107
- $name = $files[$x]['name'];
108
-
109
- $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
110
-
111
- "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
112
-
113
- "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
114
-
115
- $message .= "--{$mime_boundary}\n";
116
-
117
- }
118
-
119
-
120
-
121
- // send
122
-
123
- $ok = mail($to, $subject, $message, $headers, $returnpath);
124
-
125
- if ($ok) {
126
-
127
- echo "<p>mail sent to $to!</p>";
128
-
129
- } else {
130
-
131
- echo "<p>mail could not be sent!</p>";
132
-
133
- }
134
-
135
- } else {
136
-
137
- // email fields: to, from, subject, and so on
138
-
139
- $to = $_POST['mail_recipient'];
140
-
141
- $cc = $_POST['mail_copy'];
142
-
143
- $bcc = $_POST['mail_bcc'];
144
-
145
- $from = $_POST['mail_sender'];
146
-
147
- $myReturn = $_POST['mail_return'];
148
-
149
- $subject = $_POST['mail_subject'];
150
-
151
- $message = $_POST['mail_message'];
152
-
153
- $returnpath = "-f" . $myReturn;
154
-
155
- $headers = "From: $from\r\n";
156
-
157
- $headers .= "Cc: $cc\r\n";
158
-
159
- $headers .= "Bcc: $bcc";
160
-
161
-
162
-
163
- // send
164
-
165
- $ok = mail($to, $subject, $message, $headers, $returnpath);
166
-
167
- if ($ok) {
168
-
169
- echo "<p>mail sent to $to!</p>";
170
-
171
- } else {
172
-
173
- echo "<p>mail could not be sent!</p>";
174
-
175
- }
176
-
177
- }
178
-
179
- ?>
180
-
181
- ```
182
-
183
-
184
-
185
- それで上のコードではメッセージの本文はPlain Textなんですが、それをHTML形式のテキストを送りたいのですが、どうしたらいいでしょうか?[このURL](https://www.tutorialrepublic.com/php-tutorial/php-send-email.php)にある説明を見ると、
186
-
187
-
188
-
189
- ```PHP
190
-
191
- $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
192
-
193
- ```
194
-
195
- とContent-typeを「text/html」にしろ、とあります。ただ自分の上のコードではすでにContent-typeを「multipart/mixed」と指定しています。すると両方を指定することはできるのでしょうか?
196
-
197
-
198
-
199
- よろしくお願いします。
200
-
201
-
202
-
203
- ### 更新1
204
-
205
- どうも自分が誤解していたようです。自分のPHPのコードで
206
-
207
-
208
-
209
- ```php
210
-
211
- $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";
212
-
213
- ```
214
-
215
- とあるので、ここを「text/html」に変えればいいんでしょうね。ただ[このURL](https://sendgrid.kke.co.jp/blog/?p=8262)にあるサイトによるとPlain TextとHTML Textの両方を送る、とあります。
216
-
217
-
218
-
219
- ### 更新2
220
-
221
- その後以下のように「Content-Type」を「text/html」に変更してみました。でもメールの内容はPlain Textで送られます。何がいけないのでしょうか?
222
-
223
-
224
-
225
- ```PHP
226
-
227
- <?php
228
-
229
- error_reporting(E_ALL);
230
-
231
- ini_set('display_errors', 1);
232
-
233
- mb_internal_encoding ("UTF-8");
234
-
235
-
236
-
237
- if(isset($_FILES) && (bool) $_FILES) {
238
-
239
- $files = array();
240
-
241
- foreach($_FILES as $name=>$file) {
242
-
243
- $file_name = $file['name'];
244
-
245
- $temp_name = $file['tmp_name'];
246
-
247
- $file_type = $file['type'];
248
-
249
- $path_parts = pathinfo($file_name);
250
-
251
- $ext = $path_parts['extension'];
252
-
253
- array_push($files, $file);
254
-
255
- }
256
-
257
-
258
-
259
- // email fields: to, from, subject, and so on
260
-
261
- $to = $_POST['mail_recipient'];
262
-
263
- $cc = $_POST['mail_copy'];
264
-
265
- $bcc = $_POST['mail_bcc'];
266
-
267
- $from = $_POST['mail_sender'];
268
-
269
- $myReturn = $_POST['mail_return'];
270
-
271
- $subject = $_POST['mail_subject'];
272
-
273
- $message = $_POST['mail_message'];
274
-
275
- $htmlMessage = $_POST['mail_html'];
276
-
277
- $returnpath = "-f" . $myReturn;
278
-
279
- $headers = "From: $from\r\n";
280
-
281
- $headers .= "Cc: $cc\r\n";
282
-
283
- $headers .= "Bcc: $bcc";
284
-
285
-
286
-
287
- // boundary
288
-
289
- $semi_rand = md5(time());
290
-
291
- $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
292
-
293
-
294
-
295
- // headers for attachment
296
-
297
- $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
298
-
299
-
300
-
301
- // multipart boundary
302
-
303
- $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";
304
-
305
- $message .= "--{$mime_boundary}\n";
306
-
307
-
308
-
309
- // preparing attachments
310
-
311
- for($x=0; $x<count($files); $x++){
312
-
313
- $file = fopen($files[$x]['tmp_name'], "rb");
314
-
315
- $data = fread($file, filesize($files[$x]['tmp_name']));
316
-
317
- fclose($file);
318
-
319
- $data = chunk_split(base64_encode($data));
320
-
321
- $name = $files[$x]['name'];
322
-
323
- $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
324
-
325
- "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
326
-
327
- "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
328
-
329
- $message .= "--{$mime_boundary}\n";
330
-
331
- }
332
-
333
-
334
-
335
- // send
336
-
337
- $ok = mail($to, $subject, $message, $headers, $returnpath);
338
-
339
- if ($ok) {
340
-
341
- echo "<p>mail sent to $to!</p>";
342
-
343
- } else {
344
-
345
- echo "<p>mail could not be sent!</p>";
346
-
347
- }
348
-
349
- } else {
350
-
351
- // email fields: to, from, subject, and so on
352
-
353
- $to = $_POST['mail_recipient'];
354
-
355
- $cc = $_POST['mail_copy'];
356
-
357
- $bcc = $_POST['mail_bcc'];
358
-
359
- $from = $_POST['mail_sender'];
360
-
361
- $myReturn = $_POST['mail_return'];
362
-
363
- $subject = $_POST['mail_subject'];
364
-
365
- $message = $_POST['mail_message'];
366
-
367
- $returnpath = "-f" . $myReturn;
368
-
369
- $headers = "From: $from\r\n";
370
-
371
- $headers .= "Cc: $cc\r\n";
372
-
373
- $headers .= "Bcc: $bcc";
374
-
375
-
376
-
377
- // send
378
-
379
- $ok = mail($to, $subject, $message, $headers, $returnpath);
380
-
381
- if ($ok) {
382
-
383
- echo "<p>mail sent to $to!</p>";
384
-
385
- } else {
386
-
387
- echo "<p>mail could not be sent!</p>";
388
-
389
- }
390
-
391
- }
392
-
393
- ?>
394
-
395
- ```

2

コードの変更

2020/11/29 13:52

投稿

tomato879241
tomato879241

スコア133

test CHANGED
File without changes
test CHANGED
@@ -200,7 +200,7 @@
200
200
 
201
201
 
202
202
 
203
- ### 更新
203
+ ### 更新1
204
204
 
205
205
  どうも自分が誤解していたようです。自分のPHPのコードで
206
206
 
@@ -213,3 +213,183 @@
213
213
  ```
214
214
 
215
215
  とあるので、ここを「text/html」に変えればいいんでしょうね。ただ[このURL](https://sendgrid.kke.co.jp/blog/?p=8262)にあるサイトによるとPlain TextとHTML Textの両方を送る、とあります。
216
+
217
+
218
+
219
+ ### 更新2
220
+
221
+ その後以下のように「Content-Type」を「text/html」に変更してみました。でもメールの内容はPlain Textで送られます。何がいけないのでしょうか?
222
+
223
+
224
+
225
+ ```PHP
226
+
227
+ <?php
228
+
229
+ error_reporting(E_ALL);
230
+
231
+ ini_set('display_errors', 1);
232
+
233
+ mb_internal_encoding ("UTF-8");
234
+
235
+
236
+
237
+ if(isset($_FILES) && (bool) $_FILES) {
238
+
239
+ $files = array();
240
+
241
+ foreach($_FILES as $name=>$file) {
242
+
243
+ $file_name = $file['name'];
244
+
245
+ $temp_name = $file['tmp_name'];
246
+
247
+ $file_type = $file['type'];
248
+
249
+ $path_parts = pathinfo($file_name);
250
+
251
+ $ext = $path_parts['extension'];
252
+
253
+ array_push($files, $file);
254
+
255
+ }
256
+
257
+
258
+
259
+ // email fields: to, from, subject, and so on
260
+
261
+ $to = $_POST['mail_recipient'];
262
+
263
+ $cc = $_POST['mail_copy'];
264
+
265
+ $bcc = $_POST['mail_bcc'];
266
+
267
+ $from = $_POST['mail_sender'];
268
+
269
+ $myReturn = $_POST['mail_return'];
270
+
271
+ $subject = $_POST['mail_subject'];
272
+
273
+ $message = $_POST['mail_message'];
274
+
275
+ $htmlMessage = $_POST['mail_html'];
276
+
277
+ $returnpath = "-f" . $myReturn;
278
+
279
+ $headers = "From: $from\r\n";
280
+
281
+ $headers .= "Cc: $cc\r\n";
282
+
283
+ $headers .= "Bcc: $bcc";
284
+
285
+
286
+
287
+ // boundary
288
+
289
+ $semi_rand = md5(time());
290
+
291
+ $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
292
+
293
+
294
+
295
+ // headers for attachment
296
+
297
+ $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
298
+
299
+
300
+
301
+ // multipart boundary
302
+
303
+ $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";
304
+
305
+ $message .= "--{$mime_boundary}\n";
306
+
307
+
308
+
309
+ // preparing attachments
310
+
311
+ for($x=0; $x<count($files); $x++){
312
+
313
+ $file = fopen($files[$x]['tmp_name'], "rb");
314
+
315
+ $data = fread($file, filesize($files[$x]['tmp_name']));
316
+
317
+ fclose($file);
318
+
319
+ $data = chunk_split(base64_encode($data));
320
+
321
+ $name = $files[$x]['name'];
322
+
323
+ $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
324
+
325
+ "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
326
+
327
+ "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
328
+
329
+ $message .= "--{$mime_boundary}\n";
330
+
331
+ }
332
+
333
+
334
+
335
+ // send
336
+
337
+ $ok = mail($to, $subject, $message, $headers, $returnpath);
338
+
339
+ if ($ok) {
340
+
341
+ echo "<p>mail sent to $to!</p>";
342
+
343
+ } else {
344
+
345
+ echo "<p>mail could not be sent!</p>";
346
+
347
+ }
348
+
349
+ } else {
350
+
351
+ // email fields: to, from, subject, and so on
352
+
353
+ $to = $_POST['mail_recipient'];
354
+
355
+ $cc = $_POST['mail_copy'];
356
+
357
+ $bcc = $_POST['mail_bcc'];
358
+
359
+ $from = $_POST['mail_sender'];
360
+
361
+ $myReturn = $_POST['mail_return'];
362
+
363
+ $subject = $_POST['mail_subject'];
364
+
365
+ $message = $_POST['mail_message'];
366
+
367
+ $returnpath = "-f" . $myReturn;
368
+
369
+ $headers = "From: $from\r\n";
370
+
371
+ $headers .= "Cc: $cc\r\n";
372
+
373
+ $headers .= "Bcc: $bcc";
374
+
375
+
376
+
377
+ // send
378
+
379
+ $ok = mail($to, $subject, $message, $headers, $returnpath);
380
+
381
+ if ($ok) {
382
+
383
+ echo "<p>mail sent to $to!</p>";
384
+
385
+ } else {
386
+
387
+ echo "<p>mail could not be sent!</p>";
388
+
389
+ }
390
+
391
+ }
392
+
393
+ ?>
394
+
395
+ ```

1

その後調べてわかったこと

2020/11/28 13:26

投稿

tomato879241
tomato879241

スコア133

test CHANGED
File without changes
test CHANGED
@@ -197,3 +197,19 @@
197
197
 
198
198
 
199
199
  よろしくお願いします。
200
+
201
+
202
+
203
+ ### 更新
204
+
205
+ どうも自分が誤解していたようです。自分のPHPのコードで
206
+
207
+
208
+
209
+ ```php
210
+
211
+ $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";
212
+
213
+ ```
214
+
215
+ とあるので、ここを「text/html」に変えればいいんでしょうね。ただ[このURL](https://sendgrid.kke.co.jp/blog/?p=8262)にあるサイトによるとPlain TextとHTML Textの両方を送る、とあります。