質問編集履歴

1

ソースコードの追加

2021/07/01 20:38

投稿

sordes1219
sordes1219

スコア1

test CHANGED
File without changes
test CHANGED
@@ -19,3 +19,275 @@
19
19
 
20
20
 
21
21
  全体的な理解が足りていないのかもしれませんが、このあたりの実装方法のヒントや参考文献についてお知恵を拝借できますと助かります。よろしくお願いします。
22
+
23
+
24
+
25
+ contact.js
26
+
27
+ ```javascript
28
+
29
+ import Layout from "../components/layout"
30
+
31
+ import Tags from "./tags"
32
+
33
+ import SearchWindow from "./search_window"
34
+
35
+ import { getUniqTags } from "../lib/posts"
36
+
37
+ import axios from 'axios'
38
+
39
+
40
+
41
+ export async function getStaticProps() {
42
+
43
+
44
+
45
+ const uniqTags = getUniqTags()
46
+
47
+
48
+
49
+ return {
50
+
51
+ props:{
52
+
53
+ uniqTags
54
+
55
+ }
56
+
57
+ }
58
+
59
+
60
+
61
+ }
62
+
63
+
64
+
65
+ const submitFormData = (e) => {
66
+
67
+ e.preventDefault()
68
+
69
+
70
+
71
+ axios.post('/api/mailSender',
72
+
73
+ {
74
+
75
+ 'company': company,
76
+
77
+ 'nickname': nickname,
78
+
79
+ 'email': email,
80
+
81
+ 'question': question
82
+
83
+ }
84
+
85
+ ).then(() => {
86
+
87
+ var myModal = new bootstrap.Modal(document.getElementById('myModal'))
88
+
89
+ myModal.toggle()
90
+
91
+ }).catch((error) => {
92
+
93
+ console.log(error)
94
+
95
+ })
96
+
97
+ }
98
+
99
+
100
+
101
+ export default function Contact({ uniqTags }) {
102
+
103
+ return(
104
+
105
+ <Layout>
106
+
107
+ <div className="container content">
108
+
109
+ <div className="row">
110
+
111
+ <div className="col-md-9 mb-5">
112
+
113
+ <form onSubmit={submitFormData}>
114
+
115
+ <label htmlFor="company" className="form-label fw-bold">会社名 <span className="badge bg-secondary">任意</span></label>
116
+
117
+ <input maxLength="30" type="text" className="form-control mb-3" id="company" placeholder="会社名を記入してください"/>
118
+
119
+ <label htmlFor="nickname" className="form-label fw-bold">お名前(ニックネーム可) <span className="badge bg-danger">必須</span></label>
120
+
121
+ <input required maxLength="30" type="text" className="form-control mb-3" id="nickname" placeholder="お名前を記入してください"/>
122
+
123
+ <label htmlFor="email" className="form-label fw-bold">Eメールアドレス <span className="badge bg-danger">必須</span></label>
124
+
125
+ <input required maxLength="50" type="email" className="form-control mb-3" id="email" placeholder="メールアドレスを記入してください"/>
126
+
127
+ <label htmlFor="question" className="form-label fw-bold">お問い合わせ内容 <span className="badge bg-danger">必須</span></label>
128
+
129
+ <textarea required maxLength="1000" className="form-control mb-5" id="question" rows="7" placeholder="お問い合わせ、ご相談内容を記入してください"/>
130
+
131
+ <div className="d-flex justify-content-center">
132
+
133
+ <input id="submitBtn" className="btn btn-success w-50" type="submit" value="この内容で送信する"/>
134
+
135
+ </div>
136
+
137
+ <div className="modal fade" id="myModal" data-bs-backdrop="static" data-bs-keyboard="false" tabIndex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
138
+
139
+ <div className="modal-dialog">
140
+
141
+ <div className="modal-content">
142
+
143
+ <div className="modal-header">
144
+
145
+ <h5 className="modal-title" id="staticBackdropLabel">送信完了</h5>
146
+
147
+ <button type="button" className="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
148
+
149
+ </div>
150
+
151
+ <div className="modal-body">
152
+
153
+ お問い合わせいただきありがとうございます。
154
+
155
+ </div>
156
+
157
+ <div className="modal-footer">
158
+
159
+ <button type="button" className="btn btn-secondary" data-bs-dismiss="modal" onClick={closeModal}>Close</button>
160
+
161
+ </div>
162
+
163
+ </div>
164
+
165
+ </div>
166
+
167
+ </div>
168
+
169
+ </form>
170
+
171
+ </div>
172
+
173
+ <div className="col-md-3">
174
+
175
+ <Tags uniqTags={uniqTags}/>
176
+
177
+ <SearchWindow />
178
+
179
+ </div>
180
+
181
+ </div>
182
+
183
+ </div>
184
+
185
+ </Layout>
186
+
187
+ )
188
+
189
+ }
190
+
191
+ ```
192
+
193
+ /api/mailSender.js
194
+
195
+ ```javascript
196
+
197
+ import sgMail from '@sendgrid/mail'
198
+
199
+ import Cors from 'cors'
200
+
201
+ import initMiddleware from '../../lib/init-middleware'
202
+
203
+
204
+
205
+ const mailto = process.env.NEXT_PUBLIC_MAILTO
206
+
207
+ const mailkey = process.env.NEXT_PUBLIC_MAILKEY
208
+
209
+
210
+
211
+ const cors = initMiddleware(
212
+
213
+ Cors({
214
+
215
+ methods: ['POST'],
216
+
217
+ })
218
+
219
+ )
220
+
221
+
222
+
223
+ export default async function handler(req, res) {
224
+
225
+
226
+
227
+ await cors(req, res)
228
+
229
+
230
+
231
+ if (req.method === 'POST') {
232
+
233
+
234
+
235
+ const company = req.body.company
236
+
237
+ const nickname = req.body.nickname
238
+
239
+ const email = req.body.email
240
+
241
+ const question = req.body.question
242
+
243
+
244
+
245
+ sgMail.setApiKey(mailkey)
246
+
247
+ const msg = {
248
+
249
+ to: mailto,
250
+
251
+ from: email,
252
+
253
+ subject: "フォームからのお問い合わせ",
254
+
255
+ html: `<p>お名前:${nickname}</p>
256
+
257
+ <p>会社名:${company}</p>
258
+
259
+ <p>お問い合わせ内容:${question}</p>`,
260
+
261
+ }
262
+
263
+
264
+
265
+ try {
266
+
267
+ await sgMail.send(msg)
268
+
269
+ console.log('Success to send mail')
270
+
271
+ res.status(200).send()
272
+
273
+ } catch(error) {
274
+
275
+ console.log(error)
276
+
277
+ res.status(500).send()
278
+
279
+ }
280
+
281
+
282
+
283
+ } else {
284
+
285
+ res.status(400).send()
286
+
287
+ }
288
+
289
+
290
+
291
+ };
292
+
293
+ ```