-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* tst * updated to gotify for contact forms * clean-up * clean-up * Use dynamic ENV * Use only ArmIT email and not personal --------- Co-authored-by: Oscar Eriksson <[email protected]>
- Loading branch information
1 parent
10c61fd
commit a8a2a94
Showing
4 changed files
with
40 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
GOTIFY_URL=http://gotify/mail | ||
GOTIFY_TOKEN=gotify |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,41 @@ | ||
// src/routes/contact/+page.server.js | ||
import nodemailer from 'nodemailer'; | ||
import { env } from '$env/dynamic/private' | ||
|
||
export const actions = { | ||
default: async ({ request }) => { | ||
const form = await request.formData(); | ||
const email = form.get('e-mail'); | ||
|
||
let transporter = nodemailer.createTransport({ | ||
service: 'Gmail', | ||
auth: { | ||
user: '[email protected]', | ||
pass: 'bcsvlrwvobdfacwg' | ||
} | ||
}); | ||
|
||
// Can change this in the future if u want: | ||
let mailOptions = { | ||
from: '"Armit (Contact ME)" <motin@chalmers.it>', | ||
to: '[email protected], [email protected]', | ||
from: '"Armit (Contact ME)" <armit@chalmers.it>', | ||
to: '[email protected]', | ||
subject: '💃💃Contact ME Form', | ||
text: "Please reach out to me at: " + email | ||
}; | ||
|
||
try { | ||
await transporter.sendMail(mailOptions); | ||
const info = await fetch(env.GOTIFY_URL, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': 'pre-shared: ' + env.GOTIFY_TOKEN | ||
}, | ||
body: JSON.stringify(mailOptions) | ||
}); | ||
|
||
return { | ||
status: 200 | ||
}; | ||
} catch (error) { | ||
console.error('Failed to send email:', error); | ||
console.log('Message sent: %s', info.messageId); | ||
|
||
} catch (error) { | ||
console.error('Error occurred while sending email:', error); | ||
return { | ||
status: 500, | ||
errors: { message: 'Failed to send email' } | ||
}; | ||
} | ||
return { | ||
status: 200 | ||
}; | ||
|
||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
import { fail } from "@sveltejs/kit"; | ||
import { z } from "zod"; | ||
import { superValidate, message } from "sveltekit-superforms/server"; | ||
import nodemailer from 'nodemailer'; | ||
|
||
|
||
import { env } from '$env/dynamic/private' | ||
|
||
const newContactSchema = z.object({ | ||
company: z.string().min(1), | ||
|
@@ -14,77 +12,43 @@ const newContactSchema = z.object({ | |
}); | ||
|
||
export const actions = { | ||
// default: async ({ request }) => { | ||
// const form = await superValidate(request, newContactSchema); | ||
// console.log('POST', form); | ||
|
||
// // Convenient validation check: | ||
// if (!form.valid) { | ||
// // Again, return { form } and things will just work. | ||
// return fail(400, { form }); | ||
// } | ||
|
||
// // TODO: Do something with the validated form.data | ||
|
||
// // Yep, return { form } here too | ||
// return { form }; | ||
// }, | ||
sendEmail: async ({ request }) => { | ||
const form = await superValidate(request, newContactSchema); | ||
console.log(form) | ||
|
||
|
||
if (!form.valid) { | ||
// Again, return { form } and things will just work. | ||
|
||
return fail(400, { form }); | ||
} | ||
|
||
// TODO - send email: | ||
// - send email: | ||
try { | ||
// Create a Nodemailer transporter using SMTP | ||
let transporter = nodemailer.createTransport({ | ||
service: 'Gmail', | ||
auth: { | ||
user: '[email protected]', | ||
pass: 'bcsvlrwvobdfacwg' | ||
} | ||
}); | ||
|
||
// Define the email message | ||
let mailOptions = { | ||
from: '"Armit Website Contact Form" motin@chalmers.it', // sender address | ||
to: '[email protected], [email protected]', // list of receivers | ||
to: 'armit@chalmers.it', // list of receivers | ||
from: '"ArmIT Website Contact Form" [email protected]', // sender address | ||
subject: '$CASH SEASON! 💵🤑ArmIT Contact Form Proposal', // Subject line | ||
text: "Company: " + form.data.company + "\n" + | ||
body: "Company: " + form.data.company + "\n" + | ||
"Name: " + form.data.name + "\n" + | ||
"Email: " + form.data.email + "\n" + | ||
"Phone: " + form.data.phone + "\n" + | ||
"Message: " + form.data.message // plain text body | ||
}; | ||
|
||
// Send mail with defined transport object | ||
let info = await transporter.sendMail(mailOptions); | ||
|
||
const info = await fetch(env.GOTIFY_URL, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': 'pre-shared: ' + env.GOTIFY_TOKEN | ||
}, | ||
body: JSON.stringify(mailOptions) | ||
}); | ||
|
||
console.log('Message sent: %s', info.messageId); | ||
} catch (error) { | ||
console.error('Error occurred while sending email:', error); | ||
} | ||
|
||
return message(form, "Thanks for reaching out!" ); | ||
} | ||
}; | ||
|
||
|
||
|
||
|
||
|
||
|
||
// /** @type {import('./$types').Actions} */ | ||
// export const actions = { | ||
// sendEmail: async ({ request }) => { | ||
// const data = await request.formData(); | ||
// const email = data.get('email'); | ||
|
||
// console.log("Email is being delivered" + email) | ||
// } | ||
// }; |