teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

質問、コードの変更

2020/08/21 12:26

投稿

退会済みユーザー
title CHANGED
@@ -1,1 +1,1 @@
1
- localhostから電子メールをemail.htmlの<form>からemail.phpを使ってGmailに送って表示したい
1
+ localhostからGmailにメールを送って表示したい
body CHANGED
@@ -1,187 +1,93 @@
1
- localhostから電子メールをemail.htmlの<form>からemail.phpを使ってGmailに送って表示したい。
2
- 一応送信は出来る状態なのですが、表示はemail.phpに設定した$body="hellooooo";です。
3
- これを<form>から送られたメッセージを表示したいのですが、どうすればいいかわからないので、
4
- ヒント、もしくは修正の仕方を教えてください。
5
- email.phpコードはgmailに送信出来るようにurlのコード修正します。
1
+ swiftmailer使っ以下の設定で実行するとエラーが出ます。
6
- email.phpコードの5行目、6行目の
7
- //$mail = $_REQUEST["email"] ;
8
- //$message = $_REQUEST["message"] ;
9
- を使って<form>から送られたメッセージを表示したいのです。
10
- よろしくお願いいたします。
11
2
 
12
- ```html
3
+ $message
13
- //email.html
4
+ ->setCharset('iso-2022-jp')
5
+ ->setEncoder(Swift_Encoding::get7BitEncoding()) // 52行目、エラー
14
6
 
15
- <!-- https://living-sun.com/ja/php/668969-mailer-error-smtp-connect-failed-in-php-mailer-https-githubcom-phpmailer-phpmailer-wiki-troubleshooting-duplicate-php-email-phpmailer.html -->
7
+ ここの行がどうしても解決できないのでヒント、助言をお願いいたします。
16
- <form method="post" action="email.php">
8
+ いろいろ使っているプラグインもあると思いますが、XAMPPを使っています。
9
+ 書かなければならないファイルなどや。バージョンがあったら教えてください。
10
+ お願いいたします。
17
11
 
18
- Email: <input name="email" id="email" type="text" /><br />
19
-
20
- Message:<br />
21
- <textarea name="message" id="message" rows="15" cols="40"></textarea><br />
22
-
23
- <input type="submit" value="Submit" />
24
-
25
- </form>
26
12
  ```
27
-
28
- ```php
29
- //email.php
13
+ // swiftmailer5_4.php
30
-
31
14
  <?php
32
- // https://living-sun.com/ja/php/668969-mailer-error-smtp-connect-failed-in-php-mailer-・・// https-githubcom-phpmailer-phpmailer-wiki-troubleshooting-duplicate-php-email-pmailer.html
15
+ // https://php-archive.net/php/swift-mailer/ 参照
33
- namespace MyApp;
16
+ // https://stackoverrun.com/ja/q/12418106   参照
34
17
 
35
- //$mail = $_REQUEST["email"] ;
36
- //$message = $_REQUEST["message"] ;
37
- //var_dump ($mail);
38
- //var_dump ($message);
39
-
40
- use PHPMailer\PHPMailer\PHPMailer;
41
- use PHPMailer\PHPMailer\Exception;
42
-
43
- //Load Composer's autoloader
44
- require 'C:\xampp\htdocs\phpmailer\vendor/autoload.php';
18
+ require 'vendor/autoload.php';
45
-
19
+
46
- $mail = new PHPMailer(true); // Passing `true` enables exceptions
20
+ // アカウントの設定
47
-
48
- $mail->SMTPOptions = array(
21
+ $config = array(
22
+ 'host' => 'smtp.gmail.com',
49
- 'ssl' => array(
23
+ 'port' => 587,
24
+ 'user' => 'sanchunaka@gmail.com',
25
+ 'pass' => '6BeKxxx',
50
- 'verify_peer' => false,
26
+ 'encryption' => 'tls'
51
- 'verify_peer_name' => false,
52
- 'allow_self_signed' => true
53
- )
54
27
  );
28
+
55
-
29
+ // メールの内容
56
- echo "\n";
30
+ $subject = '件名です';
31
+ $body = '本文です';
32
+ $from = array('sanchunaka@gmail.com' => '送信者名');
33
+ $to = array('sanchunaka@gmail.com' => '受信者名');
34
+
35
+ // 日本語に関する初期設定
36
+ Swift::init(function () {
37
+ Swift_DependencyContainer::getInstance()
38
+ ->register('mime.qpheaderencoder')
39
+ ->asAliasOf('mime.base64headerencoder');
40
+ Swift_Preferences::getInstance()->setCharset('iso-2022-jp');
41
+ });
42
+
57
- //Server settings
43
+ // SMTP サーバーとの接続設定
58
- $mail->SMTPDebug = 2;
44
+ /** @var \Swift_SmtpTransport $transport */
59
-
60
- $mail->isSMTP(); // Set mailer to use SMTP
45
+ $transport = new Swift_SmtpTransport('smtp.gmail.com', 587, 'tls');
61
-
62
- $mail->Host = "smtp.gmail.com";
63
-
64
- $mail->SMTPAuth = true; // turn on SMTP authentication
65
46
 
66
- $mail->Username = "sanchunaka@gmail.com"; // SMTP username
47
+ $transport->setUsername($config['user'])->setPassword($config['pass']);
48
+
49
+ $mailer = new Swift_Mailer($transport);
50
+ //$mailer = Swift_Mailer::newInstance($transport);
51
+
52
+ $mailer->options = array (
53
+ 'ssl' => array(
54
+ 'verify_peer' => false,
55
+ 'verify_peer_name' => false,
56
+ 'allow_self_signed' => true));
67
57
 
58
+
59
+ // メールの作成
68
- $mail->Password = "xxxFi9x"; // SMTP password
60
+ $message = new Swift_Message($subject);
69
61
 
62
+ $message
70
- $mail->SMTPSecure = "tls";
63
+ ->setCharset('iso-2022-jp')
64
+ ->setEncoder(Swift_Encoding::get7BitEncoding())
65
+ ->setSubject($subject)
66
+ ->setFrom($from)
67
+ ->setTo($to)
68
+ ->setBody($body);
69
+
70
+ // 送信
71
+ $result = $mailer->send($message);
71
72
 
73
+ /* 実行結果
74
+ hiroko@HIROKO711 C:\xampp\htdocs\SwiftMailer_proj\mailer5
72
- $mail->Port = 587;
75
+ # php src/swiftmailer5_4.php
76
+ PHP Fatal error: Uncaught Error: Class 'Swift_Encoding' not found in C:\xampp\h
77
+ tdocs\SwiftMailer_proj\mailer5\src\swiftmailer5_4.php:52
78
+ Stack trace:
79
+ #0 {main}
80
+ thrown in C:\xampp\htdocs\SwiftMailer_proj\mailer5\src\swiftmailer5_4.php on l
81
+ ine 52
73
82
 
74
- $mail->AddAddress("sanchunaka@gmail.com","taturou nakamura");
83
+ Fatal error: Uncaught Error: Class 'Swift_Encoding' not found in C:\xampp\htdocs
84
+ \SwiftMailer_proj\mailer5\src\swiftmailer5_4.php:52
85
+ Stack trace:
86
+ #0 {main}
87
+ thrown in C:\xampp\htdocs\SwiftMailer_proj\mailer5\src\swiftmailer5_4.php on l
88
+ ine 52
75
89
 
76
- $mail->SetFrom("sanchunaka@gmail.com","taturou nakamura");
90
+ */
77
91
 
78
- $mail->WordWrap = 50;
79
-
80
- $mail->isHTML(true);
81
-
82
- $mail->Subject = "You have received feedback from your website!";
83
-
84
- $body="hellooooo";
85
-
86
- $mail->MsgHTML($body);
87
-
88
- if(!$mail->Send()){
89
-
90
- echo "Message could not be sent. <p>";
91
-
92
- echo "Mailer Error: " . $mail->ErrorInfo;
93
-
94
- exit;
95
-
96
- }
97
-
98
- echo "Message has been sent";
99
-
100
- /* 実行結果 正常動作
101
- hiroko@HIROKO711 C:\xampp\htdocs\php_mailer
102
- # php src/email.php
103
- 2020-07-31 12:41:57 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP h4sm1066901pg
104
- q.9 - gsmtp
105
- 2020-07-31 12:41:57 CLIENT -> SERVER: EHLO hiroko711
106
- 2020-07-31 12:41:57 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [1
107
- 26.169.48.171]
108
- 250-SIZE 35882577
109
- 250-8BITMIME
110
- 250-STARTTLS
111
- 250-ENHANCEDSTATUSCODES
112
- 250-PIPELINING
113
- 250 SMTPUTF8
114
- 2020-07-31 12:41:57 CLIENT -> SERVER: STARTTLS
115
- 2020-07-31 12:41:57 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
116
- 2020-07-31 12:41:58 CLIENT -> SERVER: EHLO hiroko711
117
- 2020-07-31 12:41:58 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [1
118
- 26.169.48.171]
119
- 250-SIZE 35882577
120
- 250-8BITMIME
121
- 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLI
122
- ENTTOKEN OAUTHBEARER XOAUTH
123
- 250-ENHANCEDSTATUSCODES
124
- 250-PIPELINING
125
- 250 SMTPUTF8
126
- 2020-07-31 12:41:58 CLIENT -> SERVER: AUTH LOGIN
127
- 2020-07-31 12:41:58 SERVER -> CLIENT: 334 VXNlcm5hbWU6
128
- 2020-07-31 12:41:58 CLIENT -> SERVER: [credentials hidden]
129
- 2020-07-31 12:41:58 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
130
- 2020-07-31 12:41:58 CLIENT -> SERVER: [credentials hidden]
131
- 2020-07-31 12:41:58 SERVER -> CLIENT: 235 2.7.0 Accepted
132
- 2020-07-31 12:41:58 CLIENT -> SERVER: MAIL FROM:<anchunaka@gmail.com>
133
- 2020-07-31 12:41:59 SERVER -> CLIENT: 250 2.1.0 OK h4sm1066901pgq.9 - gsmtp
134
- 2020-07-31 12:41:59 CLIENT -> SERVER: RCPT TO:<anchunaka@gmail.com>
135
- 2020-07-31 12:41:59 SERVER -> CLIENT: 250 2.1.5 OK h4sm1066901pgq.9 - gsmtp
136
- 2020-07-31 12:41:59 CLIENT -> SERVER: DATA
137
- 2020-07-31 12:41:59 SERVER -> CLIENT: 354 Go ahead h4sm1066901pgq.9 - gsmtp
138
-
139
- 2020-07-31 12:41:59 CLIENT -> SERVER: Date: Fri, 31 Jul 2020 14:41:57 +0200
140
- 2020-07-31 12:41:59 CLIENT -> SERVER: To: taturou nakamura <anchunaka@gmail.
141
- com>
142
- 2020-07-31 12:41:59 CLIENT -> SERVER: From: taturou nakamura <anchunaka@gmai
143
- l.com>
144
- 2020-07-31 12:41:59 CLIENT -> SERVER: Subject: You have received feedback fr
145
- om your website!
146
- 2020-07-31 12:41:59 CLIENT -> SERVER: Message-ID: <1q1hKQR6FprjUIdsqzX1aud7x
147
- s82ZRbFpVMCf7eAmw@hiroko711>
148
- 2020-07-31 12:41:59 CLIENT -> SERVER: X-Mailer: PHPMailer 6.1.7 (https://git
149
- hub.com/PHPMailer/PHPMailer)
150
- 2020-07-31 12:41:59 CLIENT -> SERVER: MIME-Version: 1.0
151
- 2020-07-31 12:41:59 CLIENT -> SERVER: Content-Type: multipart/alternative;
152
- 2020-07-31 12:41:59 CLIENT -> SERVER: boundary="b1_1q1hKQR6FprjUIdsqzX1aud7
153
- xs82ZRbFpVMCf7eAmw"
154
- 2020-07-31 12:41:59 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
155
- 2020-07-31 12:41:59 CLIENT -> SERVER:
156
- 2020-07-31 12:41:59 CLIENT -> SERVER: This is a multi-part message in MIME f
157
- ormat.
158
- 2020-07-31 12:41:59 CLIENT -> SERVER:
159
- 2020-07-31 12:41:59 CLIENT -> SERVER: --b1_1q1hKQR6FprjUIdsqzX1aud7xs82ZRbFp
160
- VMCf7eAmw
161
- 2020-07-31 12:41:59 CLIENT -> SERVER: Content-Type: text/plain; charset=us-a
162
- scii
163
- 2020-07-31 12:41:59 CLIENT -> SERVER:
164
- 2020-07-31 12:41:59 CLIENT -> SERVER: hellooooo
165
- 2020-07-31 12:41:59 CLIENT -> SERVER:
166
- 2020-07-31 12:41:59 CLIENT -> SERVER: --b1_1q1hKQR6FprjUIdsqzX1aud7xs82ZRbFp
167
- VMCf7eAmw
168
- 2020-07-31 12:41:59 CLIENT -> SERVER: Content-Type: text/html; charset=us-as
169
- cii
170
- 2020-07-31 12:41:59 CLIENT -> SERVER:
171
- 2020-07-31 12:41:59 CLIENT -> SERVER: hellooooo
172
- 2020-07-31 12:41:59 CLIENT -> SERVER:
173
- 2020-07-31 12:41:59 CLIENT -> SERVER:
174
- 2020-07-31 12:41:59 CLIENT -> SERVER: --b1_1q1hKQR6FprjUIdsqzX1aud7xs82ZRbFp
175
- VMCf7eAmw--
176
- 2020-07-31 12:41:59 CLIENT -> SERVER:
177
- 2020-07-31 12:41:59 CLIENT -> SERVER: .
178
- 2020-07-31 12:42:00 SERVER -> CLIENT: 250 2.0.0 OK 1596199320 h4sm1066901pg
179
- q.9 - gsmtp
180
- 2020-07-31 12:42:00 CLIENT -> SERVER: QUIT
181
- 2020-07-31 12:42:00 SERVER -> CLIENT: 221 2.0.0 closing connection h4sm10669
182
- 01pgq.9 - gsmtp
183
- Message has been sent
184
-
185
- */
186
- ?>
92
+ ?>コード
187
93
  ```

2

タグの追加

2020/08/21 12:26

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
File without changes

1

タグの追加

2020/08/01 12:11

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
File without changes