Sending Using Email Capture

Is there sample code in PHP for using the Sending Using Email Capture

Hi Amit,

Welcome to the forums. We don’t have a sample code in PHP. However, I provided our current example to ChatGPT and it generated the following. I’m not a PHP developer, but much of the code looks reasonable.

Here’s a PHP version of the Python script that demonstrates how to use the Mailsac SMTP capture service:

<?php

$SMTP_SERVER = 'capture.mailsac.com';
$SMTP_PORT = 5587;
$SMTP_USERNAME = 'MAILSAC_USERNAME';
$SMTP_PASSWORD = 'MAILSAC_API_KEY';
$FROM_ADDRESS = '[email protected]';
$FROM_NAME = 'Mywebapp Customer Support';
$TO_ADDRESS = '[email protected]';
$SUBJECT = 'Password reset';
$BODY_TEXT = 'Click on the link to reset your password' . "\r\n" .
             'https://mywebapp.com/password-reset/PasswordResetToken';

// Create the email headers
$headers = array(
    'From' => "$FROM_NAME <$FROM_ADDRESS>",
    'To' => $TO_ADDRESS,
    'Subject' => $SUBJECT
);

// Set SMTP options
$options = array(
    'host' => $SMTP_SERVER,
    'port' => $SMTP_PORT,
    'auth' => true,
    'username' => $SMTP_USERNAME,
    'password' => $SMTP_PASSWORD,
    'tls' => true
);

// Create a mail object
$mail_object = Mail::factory('smtp', $options);

// Send the email
$mail_result = $mail_object->send($TO_ADDRESS, $headers, $BODY_TEXT);

if (PEAR::isError($mail_result)) {
    echo 'Error: ' . $mail_result->getMessage();
} else {
    echo 'Email Sent';
}

?>

Key Points:

  • This script requires the PEAR Mail package, which provides a simple API for sending emails via SMTP.
  • Ensure that PEAR is installed, and the Mail package is available in your PHP environment.
  • Replace MAILSAC_USERNAME and MAILSAC_API_KEY with your actual Mailsac username and API key.

To install the necessary PEAR Mail package, you can run:

pear install Mail

This PHP script mirrors the functionality of the original Python script, capturing an SMTP email and sending it via Mailsac’s service.