Zig
Zig
Add Email Attachment from FTP
Downloads (into memory) a file from an FTP server and adds it as an attachment to an email.Note: This example requires Chilkat v9.5.0.63 or later.
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
var success: bool = false;
// Note: This example requires Chilkat v9.5.0.63 or later.
// This example assumes Chilkat Ftp2 to have been previously unlocked.
// See Unlock Ftp2 for sample code.
const ftp = try chilkat.Ftp2.init();
defer ftp.deinit();
ftp.setHostname("www.my-ftp-server.com");
ftp.setUsername("mFtpLogin");
ftp.setPassword("myFtpPassword");
// Connect to the FTP server.
success = if (ftp.connectOnly()) true else |_| false;
if (!success) {
std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
return;
}
// Authenticate with the FTP server.
success = if (ftp.loginAfterConnectOnly()) true else |_| false;
if (!success) {
std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
return;
}
// Move to the remove directory where our file is located.
success = if (ftp.changeRemoteDir("qa_data")) true else |_| false;
if (success) {
success = if (ftp.changeRemoteDir("pdf")) true else |_| false;
}
if (!success) {
std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
return;
}
// Download...
const pdf_data = try chilkat.BinData.init();
defer pdf_data.deinit();
ftp.getFileBd("fishing.pdf", pdf_data) catch {
std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
return;
};
ftp.disconnect() catch {};
// Create an email object, and add the PDF as an attachment.
const email = try chilkat.Email.init();
defer email.deinit();
email.setSubject("Test with PDF attachment.");
email.setBody("This is a plain-text body..");
// Add the PDF attachment. (This method call requires Chilkat v9.5.0.63 or later)
email.addAttachmentBd("fishing.pdf", pdf_data, "application/pdf") catch {
std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
return;
};
// Save the email and examine with a text editor to see the PDF attachment is present..
email.saveEml("qa_output/out.eml") catch {};
std.debug.print("Success.\n", .{});
}