I tried sending an email of our app via Shopify's email processing PHP code

I tried sending an email of our app via Shopify's email processing PHP code

Ishan_Makkar
Shopify Partner
115 7 8

Here is the PHP code for the email processing.

I created this code for our app through Shopify's email feature, intending to send emails from the app admin to customers' mail. For example, when there's an event triggered by the admin, an email needs to be sent from the admin's email to the customer. However, it's not yielding any response. Could you please assist me in resolving this issue for smooth functionality? Additionally, I'd like to confirm if this is the correct method or if Shopify provides this feature for such functionality.


// Replace with your actual Shopify store URL, access token, and other necessary details
$shopifyStoreUrl = 'https://your-store.myshopify.com';
$accessToken = 'your-access-token';
$recipientEmail = 'recipient@example.com';
$senderEmail = 'sender@example.com';
$emailSubject = 'Subject of the Email';
$emailBody = 'Body of the Email';

// Endpoint for sending emails
$endpoint = $shopifyStoreUrl . '/admin/api/2022-01/email/send.json';

// Email payload
$emailData = [
    'email' => [
        'subject' => $emailSubject,
        'body' => $emailBody,
        'to' => $recipientEmail,
        'from' => $senderEmail
    ]
];

// Initialize cURL session
$curl = curl_init($endpoint);

// Set cURL options
curl_setopt_array($curl, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => json_encode($emailData),
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/json',
        'X-Shopify-Access-Token: ' . $accessToken
    ]
]);

// Execute the cURL request
$response = curl_exec($curl);

// Check for errors
if ($response === false) {
    echo 'Error sending email: ' . curl_error($curl);
} else {
    $responseData = json_decode($response, true);
    if (isset($responseData['email']) && $responseData['email']['status'] === 'sent') {
        echo 'Email sent successfully!';
    } else {
        echo 'Failed to send email. Error: ' . $response;
    }
}

// Close cURL session
curl_close($curl);
?>

Shopify Expert Since 2012
Sr. Web Solution Manager at MakkPress Technologies
Website Speedy (SAAS tool for Shopify Speed Optimization - Trusted by over 300 Shopify store owners)
Contact Us or Hire Us Or Email Us
Replies 0 (0)