-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-usage.js
More file actions
83 lines (70 loc) · 2.34 KB
/
example-usage.js
File metadata and controls
83 lines (70 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Example usage in your Vite app
import EmailService from './email-service.js'
const emailService = new EmailService('http://localhost:3000') // Your Next.js app URL
// Example 1: Send simple email
async function sendSimpleEmail() {
const result = await emailService.sendEmail({
to: 'recipient@example.com',
subject: 'Hello from Vite App',
body: '<h1>Hello!</h1><p>This email was sent from my Vite app using the Next.js API.</p>'
})
if (result.success) {
console.log('Email sent successfully!')
} else {
console.error('Failed to send email:', result.error)
}
}
// Example 2: Send email with file attachments
async function sendEmailWithFiles() {
const fileInput = document.getElementById('file-input') // Your file input element
const files = Array.from(fileInput.files)
const result = await emailService.sendEmailWithAttachments({
to: 'recipient@example.com',
subject: 'Email with attachments',
body: '<p>Please find the attached files.</p>',
attachments: files
})
if (result.success) {
console.log('Email with attachments sent successfully!')
} else {
console.error('Failed to send email:', result.error)
}
}
// Example 3: Convert file to base64 and send
async function sendEmailWithBase64() {
const file = document.getElementById('file-input').files[0]
if (file) {
const base64Content = await fileToBase64(file)
const result = await emailService.sendEmailWithBase64Attachments({
to: 'recipient@example.com',
subject: 'Email with base64 attachment',
body: '<p>File attached as base64.</p>',
attachments: [{
filename: file.name,
content: base64Content
}]
})
if (result.success) {
console.log('Email sent successfully!')
} else {
console.error('Failed to send email:', result.error)
}
}
}
// Helper function to convert file to base64
function fileToBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = () => {
const base64 = reader.result.split(',')[1] // Remove data:type;base64, prefix
resolve(base64)
}
reader.onerror = error => reject(error)
})
}
// Example 4: Check API health
async function checkApiHealth() {
const result = await emailService.checkHealth()
console.log('API Health:', result)
}