Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use GOTIFY #6

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
34 changes: 18 additions & 16 deletions src/routes/+page.server.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
// src/routes/contact/+page.server.js
import nodemailer from 'nodemailer';
import { GOTIFY_TOKEN, GOTIFY_URL } from '$env/static/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)" <[email protected]>',
to: '[email protected], [email protected]',
Expand All @@ -22,18 +15,27 @@ export const actions = {
};

try {
await transporter.sendMail(mailOptions);
const info = await fetch(GOTIFY_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'pre-shared: ' + 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
};

}
};
};
66 changes: 16 additions & 50 deletions src/routes/contact/+page.server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fail } from "@sveltejs/kit";
import { z } from "zod";
import { superValidate, message } from "sveltekit-superforms/server";
import nodemailer from 'nodemailer';
import { GOTIFY_TOKEN, GOTIFY_URL } from '$env/static/private'



Expand All @@ -14,77 +14,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" [email protected]', // sender address
to: '[email protected], [email protected]', // 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(GOTIFY_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'pre-shared: ' + 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)
// }
// };
Loading