To send an email via Outlook.com using `curl`, you would typically need to use the Microsoft Graph API, which allows you to send emails programmatically. Below are the steps to achieve this:
### Prerequisites:
1. **Microsoft Azure Account**: You need to register an application in Azure to get the necessary credentials.
2. **API Permissions**: Ensure your application has the `Mail.Send` permission.
3. **Access Token**: You need to obtain an OAuth 2.0 access token to authenticate your requests.
### Steps:
1. **Register an Application in Azure**:
- Go to the [Azure Portal](
https://portal.azure.com/).
- Navigate to "Azure Active Directory" > "App registrations" > "New registration".
- Register your application and note down the `Client ID` and `Tenant ID`.
2. **Configure API Permissions**:
- In your app registration, go to "API permissions" > "Add a permission" > "Microsoft Graph".
- Select "Delegated permissions" and check `Mail.Send`.
- Click "Grant admin consent" to grant the permissions.
3. **Obtain an Access Token**:
- Use the OAuth 2.0 authorization code flow to obtain an access token. You can use the following `curl` command to get the token:
```bash
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" \
-d "client_id=YOUR_CLIENT_ID" \
-d "scope=
https://graph.microsoft.com/.default" \
-d "client_secret=YOUR_CLIENT_SECRET" \
-d "grant_type=client_credentials" \
"
https://login.microsoftonline.com/YOUR_TENANT_ID/oauth2/v2.0/token"
```
- Replace `YOUR_CLIENT_ID`, `YOUR_CLIENT_SECRET`, and `YOUR_TENANT_ID` with your actual values.
4. **Send Email Using Microsoft Graph API**:
- Once you have the access token, you can use it to send an email via the Microsoft Graph API. Here is an example `curl` command:
```bash
curl -X POST
https://graph.microsoft.com/v1.0/me/sendMail \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"message": {
"subject": "Test Email",
"body": {
"contentType": "Text",
"content": "This is a test email sent via Microsoft Graph API."
},
"toRecipients": [
{
"emailAddress": {
"address": "
reci...@example.com"
}
}
]
}
}'
```
- Replace `YOUR_ACCESS_TOKEN` with the access token you obtained in the previous step, and `
reci...@example.com` with the actual recipient's email address.
### Notes:
- The above example uses the `client_credentials` grant type, which is suitable for app-only scenarios. If you need to send emails on behalf of a user, you should use the `authorization_code` grant type and handle user consent.
- Ensure that the `Mail.Send` permission is correctly configured and that the access token has the necessary scopes.
### Example with Authorization Code Flow:
If you are using the authorization code flow, you would first need to redirect the user to the Microsoft login page to obtain an authorization code, then exchange that code for an access token.
```bash
# Step 1: Redirect user to login and get authorization code
# Step 2: Exchange authorization code for access token
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" \
-d "client_id=YOUR_CLIENT_ID" \
-d "scope=
https://graph.microsoft.com/.default" \
-d "client_secret=YOUR_CLIENT_SECRET" \
-d "grant_type=authorization_code" \
-d "code=YOUR_AUTHORIZATION_CODE" \
-d "redirect_uri=YOUR_REDIRECT_URI" \
"
https://login.microsoftonline.com/YOUR_TENANT_ID/oauth2/v2.0/token"
```
Replace `YOUR_AUTHORIZATION_CODE` and `YOUR_REDIRECT_URI` with the actual values.
### Conclusion:
Using `curl` to send an email via Outlook.com involves interacting with the Microsoft Graph API, obtaining an access token, and then making a POST request to the `sendMail` endpoint. Ensure you have the correct permissions and follow the OAuth 2.0 flow appropriate for your use case.