Has anyone had any luck sending emails via Amazon ...
# cfml-general
a
Has anyone had any luck sending emails via Amazon SES http API which contain contentid base64 encoded images?
I can send an HTML email via https://docs.aws.amazon.com/ses/latest/APIReference-V2/API_SendEmail.html. But I can not get the
<img src=cid:>
to work
p
Just asked ChatGPT and got what appears to be a perfect example
Copy code
<cfscript>
// Base64-encoded image
var base64Image = "base64-encoded-image-data";

// Destination email address
var toEmail = "<mailto:recipient@example.com|recipient@example.com>";

// Sender email address
var fromEmail = "<mailto:sender@example.com|sender@example.com>";

// AWS SES configuration
var awsAccessKey = "your-aws-access-key";
var awsSecretKey = "your-aws-secret-key";
var awsRegion = "your-aws-region";

// Set AWS SDK configuration
aws = createObject("java", "com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient");
credentials = createObject("java", "com.amazonaws.auth.BasicAWSCredentials").init(awsAccessKey, awsSecretKey);
aws.setCredentials(credentials);
aws.setRegion(awsRegion);

// Convert base64 image to binary
binaryImage = binaryDecode(base64Image, "base64");

// Generate CID (Content-ID) for the embedded image
cid = createUUID();

// Create a temporary file path for the embedded image
embeddedImagePath = expandPath("/path/to/embedded/image.png");

// Save binary image to a file
fileWrite(embeddedImagePath, binaryImage);

// Construct the HTML email body with embedded image
htmlBody = "<h1>Email with Embedded Image</h1>";
htmlBody &= "<p>Here's an embedded image:</p>";
htmlBody &= "<img src='cid:#cid#' alt='Embedded Image'>";

// Send email with the embedded image
cfmail(
    to = toEmail,
    from = fromEmail,
    subject = "Email with Embedded Image",
    type = "html"
) {
    // Set the HTML body
    cfmailpart(type = "text/html") {
        writeOutput(htmlBody);
    }

    // Attach the embedded image using Content-ID
    cfmailparam(
        file = embeddedImagePath,
        contentid = cid,
        disposition = "inline",
        content = "image/png"
    );
}

// Delete the temporary embedded image file
fileDelete(embeddedImagePath);
</cfscript>
a
Well, that's using
cfmail
🙂
p
yea but the core method of creating the CID is in there
a
I know how to do the cid stuff. Doing that with cfmail already. Can not get it to work via the Amazon SES API though
I've asked both ChatGPT and Bard - both provide code that does not work (seems to be an older version of the API signature)
p
So are you unable to use the API at all due to signature or because of the image?
a
Yes - I can send HTML emails just fine.
If you want to attach files you have to basically construct the email yourself though 😕
p
Ahh, yea havent had to do that. Have you seen this module: https://github.com/anujgakhar/AmazonSESCFC/blob/master/coldbox-plugin/AmazonSES.cfc
Not sure it does direct html though
a
I've literally just managed to solve it....
Wow - that was quite the battle
When you do the base64 image each line can not exceed 1000 characters so you have to slice it
p
ahh chunking the request yeaaaa
p
Well always helpful to share the fix; congrats!
a
Save someone the pain!
I had the HTML version working yesterday so figured I was almost home and dry. Many hours later....
p
I spent 5+ hours trying to figure out something on Friday and yesterday decided to sign up and use ChatGPT for the first time and first question asked it kicked me back some fairly complex code in seconds. So it won me over lol
a
I have used it - and it can be handy to point you i the right direction but it has never actually solved a problem for me yet. It's not bad at JS stuff.
bard does code as well now
p
I needed a signature to be equal to what is generated in a JS lib to be done in CF/Java and it spit it right out. Couldnt get the HMAC() tag to work as expected.
a
Ah
HMAC
is a PITA. Nice win 🙂
💯 1