Can’t help you with the creation of the .doc-file, as i have never done that. But if you manage to get the encoded file into a string, you can work with the WordPress PHP-Mailer instance like this:
function send_my_mail_with_attachment($from,$to,$subject,$body,$attachmentstring=""){
global $phpmailer;
// (Re)create it, if it's gone missing
if ( ! ( $phpmailer instanceof PHPMailer ) ) {
require_once ABSPATH . WPINC . '/class-phpmailer.php';
require_once ABSPATH . WPINC . '/class-smtp.php';
$phpmailer = new PHPMailer( true );
}
$phpmailer->CharSet="UTF-8";
$phpmailer->ClearAllRecipients();
$phpmailer->ClearAttachments();
$phpmailer->ClearCustomHeaders();
$phpmailer->ClearReplyTos();
$fromaddress=$from;
$phpmailer->setFrom($fromaddress);
$phpmailer->Sender = $phpmailer->From;
$phpmailer->addAddress($to);
$phpmailer->isHTML(true); // Set email format to HTML
$phpmailer->Subject = $subject;
$phpmailer->Body = $body;
if($attachmentstring){
$phpmailer->AddStringAttachment($attachmentstring,'my_attachment.doc');
}
$rueckgabe = $phpmailer->Send();
return $rueckgabe;
}
You can learn more about PHPMailer here