11const { EmailClient } = require ( "@azure/communication-email" ) ;
22
3- // Connection string from Azure portal
4- const connectionString = "endpoint=https://eoagritool.africa.communication.azure.com/;accesskey=EKcca3DMi2dnF9WCgoAcdaFbg5x1zyXiJwnwQ3hRTSbwJI0njIVdJQQJ99BCACULyCphvvTgAAAAAZCSk3Vm" ;
5-
6- // Initialize the email client
7- const emailClient = new EmailClient ( connectionString ) ;
8-
93async function sendEmail ( ) {
4+ const connectionString = "endpoint=https://eoagritool.africa.communication.azure.com/;accesskey=EKcca3DMi2dnF9WCgoAcdaFbg5x1zyXiJwnwQ3hRTSbwJI0njIVdJQQJ99BCACULyCphvvTgAAAAAZCSk3Vm" ;
5+
106 try {
11- // Create the email message
7+ const emailClient = new EmailClient ( connectionString ) ;
8+
129 const emailMessage = {
13- senderAddress : "<Verified-Sender-Email> " , // Replace with your verified sender email
10+ senderAddress :
"[email protected] " , // MUST BE PRE-VERIFIED 1411 content : {
15- subject : "Welcome to Azure Communication Services Email" ,
16- plainText : "This is plain text body" ,
17- html : "<html><body>This is html body</body></html>" ,
12+ subject : "Test Email from ACS" ,
13+ plainText : "This is a test email body" ,
1814 } ,
1915 recipients : {
20- to : [
21- {
22- address : "<recipient-email>" , // Replace with recipient email
23- displayName : "Customer Name" ,
24- } ,
25- ] ,
16+ to :
[ { address :
"[email protected] " } ] , 2617 } ,
2718 } ;
2819
29- // Send the email
3020 const poller = await emailClient . beginSend ( emailMessage ) ;
31- const response = await poller . pollUntilDone ( ) ;
32-
33- console . log ( "Email sent successfully. Operation ID:" , response . id ) ;
21+ const result = await poller . pollUntilDone ( ) ;
22+
23+ console . log ( "Email sent with ID:" , result . id ) ;
3424 } catch ( error ) {
35- console . error ( "Error sending email:" , error ) ;
25+ console . error ( "Failed to send email:" ) ;
26+ console . error ( "- Status code:" , error . statusCode ) ;
27+ console . error ( "- Error message:" , error . message ) ;
28+ if ( error . details ) console . error ( "- Details:" , error . details ) ;
3629 }
3730}
3831
32+ // Execute the function
3933sendEmail ( ) ;
0 commit comments