86 lines
No EOL
2.7 KiB
TypeScript
86 lines
No EOL
2.7 KiB
TypeScript
#!/usr/bin/env ts-node
|
|
|
|
/**
|
|
* Nodemailer transport example for SMTP over WebSocket
|
|
*/
|
|
|
|
import nodemailer from 'nodemailer';
|
|
import { createTransport } from '../src/index';
|
|
|
|
async function nodemailerTransportExample() {
|
|
console.log('Nodemailer SMTP WebSocket Transport Example\n');
|
|
|
|
// Create the WebSocket transport
|
|
const transport = createTransport({
|
|
host: '192.168.0.62',
|
|
apiKey: 'cebc9a7f-4e0c-4fda-9dd0-85f48c02800c',
|
|
port: 80,
|
|
secure: false, // Set to true for wss://
|
|
debug: false
|
|
});
|
|
|
|
// Create Nodemailer transporter
|
|
const transporter = nodemailer.createTransport(transport);
|
|
|
|
// Verify the transport configuration
|
|
try {
|
|
console.log('Verifying transport configuration...');
|
|
await transporter.verify();
|
|
console.log('Transport verification successful\n');
|
|
} catch (error) {
|
|
console.error('Transport verification failed:', error);
|
|
return;
|
|
}
|
|
|
|
// Send a test email
|
|
try {
|
|
console.log('Sending test email...');
|
|
|
|
const info = await transporter.sendMail({
|
|
from: 'cudconnex@satitm.chula.ac.th',
|
|
to: 'siwat.s@chula.ac.th',
|
|
subject: 'Test Email via SMTP WebSocket',
|
|
text: 'This email was sent using the SMTP WebSocket transport!',
|
|
html: `
|
|
<h1>Test Email</h1>
|
|
<p>This email was sent using the <strong>SMTP WebSocket transport</strong>!</p>
|
|
<p>Features:</p>
|
|
<ul>
|
|
<li>Automatic connection management</li>
|
|
<li>Queue-based message handling</li>
|
|
<li>Nodemailer compatibility</li>
|
|
<li>WebSocket-based SMTP relay</li>
|
|
</ul>
|
|
`
|
|
});
|
|
|
|
console.log('Email sent successfully!');
|
|
console.log('Message ID:', info.messageId);
|
|
console.log('Accepted recipients:', info.accepted);
|
|
console.log('Rejected recipients:', info.rejected);
|
|
console.log('Response:', info.response);
|
|
|
|
} catch (error) {
|
|
console.error('Failed to send email:', error);
|
|
} finally {
|
|
// Close the transport
|
|
console.log('\nClosing transport...');
|
|
await transport.close();
|
|
console.log('Transport closed');
|
|
}
|
|
}
|
|
|
|
// Run the example
|
|
if (require.main === module) {
|
|
nodemailerTransportExample()
|
|
.then(() => {
|
|
console.log('\nNodemailer transport example completed successfully');
|
|
process.exit(0);
|
|
})
|
|
.catch((error) => {
|
|
console.error('\nExample failed:', error);
|
|
process.exit(1);
|
|
});
|
|
}
|
|
|
|
export { nodemailerTransportExample }; |