John Davidson

php - Generating EML and sending to the client's Outlook

0 comments
Message:


I want to dynamically create EML files and send them to the client, which would open Outlook (corporate network thus Outlook will be present).


So far, Outlook will open, but the entire $test string will be featured in the body and will not populate the subject.


I'm probably missing something in the formatting of the string for it to be interpreted correctly by Outlook?


$test = 'MIME-Version: 1.0

Subject: EMAIL_SUBJECT

Content-Type: multipart/mixed; boundary="080107000800000609090108"

This is a message with multiple parts in MIME format.

--080107000800000609090108

Content-Type: text/html

EMAIL_BODY

--080107000800000609090108';
// send to browser
header('Content-Description: File Transfer');
// handled by default e-mail client
header('Content-Type: message/rfc822');
header('Content-Disposition: inline; filename="template.eml"');
header('Cache-Control: private');
echo $test;
exit;

Edit #1


Sample that works as intended. Plus, found a ton of examples here:
https://github.com/mikel/mail/tree/master/spec/fixtures/emails/plain_emails


$test = 'MIME-Version: 1.0
Subject: Testing, 1 2 and 3
Content-Type: text/html

Content here

';
// send to browser
header('Content-Description: File Transfer');
// handled by default e-mail client
header('Content-Type: message/rfc822');
header('Content-Disposition: inline; filename="template.eml"');
header('Cache-Control: private');
echo $test;
exit;


My Answer:
To generate an EML file in PHP and send it to a client, you can use the following code:

php

// Create a new EML file
$eml_content = "From: sender@example.com\r\n";
$eml_content .= "To: recipient@example.com\r\n";
$eml_content .= "Subject: Test EML\r\n";
$eml_content .= "Date: " . date('r') . "\r\n";
$eml_content .= "Content-Type: text/plain; charset=utf-8\r\n\r\n";
$eml_content .= "This is a test email in EML format.";

// Save the EML content to a file
$eml_file = 'test.eml';
file_put_contents($eml_file, $eml_content);

// Send the EML file to the client
header('Content-Type: message/rfc822');
header('Content-Disposition: attachment; filename="test.eml"');
readfile($eml_file);

// Delete the EML file after sending
unlink($eml_file);

?>


This code creates a simple EML file with sender, recipient, subject, date, and content information. It then saves the EML content to a file, sends the file to the client as an attachment with the appropriate headers, and finally deletes the file after sending.

You can customize the EML content and file name as needed for your specific use case.

Rate this post

3 of 5 based on 9042 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us