Skip to content

Commit

Permalink
use GOTIFY (#6)
Browse files Browse the repository at this point in the history
* 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
mikaelmotin and Oscariremma authored Nov 7, 2024
1 parent 10c61fd commit a8a2a94
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 75 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GOTIFY_URL=http://gotify/mail
GOTIFY_TOKEN=gotify
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,5 @@
"description": "Everything you need to build a Svelte project, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/master/packages/create-svelte).",
"main": "postcss.config.js",
"author": "",
"license": "ISC",
"dependencies": {
"nodemailer": "^6.9.9"
}
"license": "ISC"
}
38 changes: 20 additions & 18 deletions src/routes/+page.server.js
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
};

}
};
};
70 changes: 17 additions & 53 deletions src/routes/contact/+page.server.js
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),
Expand All @@ -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)
// }
// };

0 comments on commit a8a2a94

Please sign in to comment.