How to send email with azure communication services using php sdk

Favour Afula 0 Reputation points
2023-07-16T18:35:32.1733333+00:00

i already have the code working in npm but i want it in php. i can't find the library forphp. below is the js code that works perfectly:

const { EmailClient, KnownEmailSendStatus } = require("@azure/communication-email");
require("dotenv").config();

const connectionString = "##################";
const senderAddress = "##################"
const recipientAddress = "##################"



async function main() {
  const POLLER_WAIT_TIME = 10

  const message = {
    senderAddress: senderAddress,
    recipients: {
      to: [{ address: recipientAddress }],
    },
    content: {
      subject: "Test email from JS Sample",
      plainText: "This is plaintext body of test email.",
      html: "<html><h1>This is the html body of test email.</h1></html>",
    },
  }

  try {
    const client = new EmailClient(connectionString);

    const poller = await client.beginSend(message);

    if (!poller.getOperationState().isStarted) {
      throw "Poller was not started."
    }

    let timeElapsed = 0;
    while(!poller.isDone()) {
      poller.poll();
      console.log("Email send polling in progress");

      await new Promise(resolve => setTimeout(resolve, POLLER_WAIT_TIME * 1000));
      timeElapsed += 10;

      if(timeElapsed > 18 * POLLER_WAIT_TIME) {
        throw "Polling timed out.";
      }
    }

    if(poller.getResult().status === KnownEmailSendStatus.Succeeded) {
      console.log(`Successfully sent the email (operation id: ${poller.getResult().id})`);
    }
    else {
      throw poller.getResult().error;
    }
  }
  catch(ex) {
    console.error(ex);
  }
}

main();












Azure Communication Services
Azure Communication Services
An Azure communication platform for deploying applications across devices and platforms.
810 questions
{count} votes

2 answers

Sort by: Most helpful
  1. VenkateshDodda-MSFT 19,646 Reputation points Microsoft Employee
    2023-07-17T04:17:55.2966667+00:00

    @Favour Afula Thanks for reaching out to Microsoft Q&A, apologize for any inconvenience caused on this.

    Unfortunately, we don't have any PHP SDK/library in Azure communication service for sending an email. you can refer here to know more about the SDK which are currently supported in Azure Communication service.

    Alternatively, you can make use of existing Send email Restful API's to do that.

    Feel free to reach back to me if you have any further questions on this.

    0 comments No comments

  2. Rola Naser 0 Reputation points
    2023-11-09T10:59:26.2+00:00

    Hello Favour Afula,
    may be this library will be helpful
    https://github.com/hafael/azure-mailer-driver

    0 comments No comments