Concrete5.7.5.2 - Where to put form file attachment headers?

Permalink
I build my email headers like this:
$txt_message .= $this->txt_message;
$html_message .= $this->html_message;
$mh = Core::make('helper/mail');
$mh->to($this->email_to, $this->site_name);
$mh->from($this->email, $this->name);
$mh->replyto($this->email, $this->name);
$mh->setSubject($this->subject);
$mh->setBody($txt_message);
$mh->setBodyHtml($html_message);
@$mh->sendMail();

But where do I put headers for a file attachment? Where can I find out more about what really sends the message (is it still a Zend Mail?) and what methods are available?

Thank you.

linuxoid
 
linuxoid replied on at Permalink Reply
linuxoid
Some posts say an attachment can be added with
$mh->addAttachment($file);

but $file must be a file object. How can I make the uploaded file a file object?

I also found this post:http://www.adrikodde.nl/blog/2012/mail-attachments-concrete5/...

But I get errors for all Zend stuff. Is Zend Mail still available in C5.7?
linuxoid replied on at Permalink Best Answer Reply
linuxoid
Got it! Got it! Got it!

Here's a working code for attaching files to your form:
$txt_message .= $this->txt_message;
$html_message .= $this->html_message;
$mh = Core::make('helper/mail');
$mh->to($this->email_to, $this->site_name);
$mh->from($this->email, $this->name);
$mh->replyto($this->email, $this->name);
$mh->setSubject($this->subject);
$mh->setBody($txt_message);
$mh->setBodyHtml($html_message);
$file = $_FILES['image']['tmp_name'];
$filename = $_FILES['image']['name'];
$importer = new \Concrete\Core\File\Importer();
$file_version = $importer->import($file, $filename);
$attachment = $file_version->getFile();
$mh->addAttachment($attachment);
linuxoid replied on at Permalink Reply
linuxoid
I've just realized, even though that works, but there is an unwanted effect - it saves all the uploaded files in the file manager. This is not what I need. All I need is to send an attached file by email, it should not save it on the server anywhere. Is there any other way to attach a file, without the file importer? Thank you.
linuxoid replied on at Permalink Reply
linuxoid
I hoped there would be a better way than uploading a file and then deleting it. Is there?

But in case someone doesn't want it on the server in the file manager, you can delete it after sending the mail:
@$mh->sendMail();
$attachment->delete();

PS. Don't forget to check the file really selected/exists/uploaded before you try to send it!
if (!empty($this->image)) {
    $importer = new \Concrete\Core\File\Importer();
    $image_version = $importer->import($this->image, $file_name);
    if ($image_version instanceof \Concrete\Core\File\Version) {
        $attachment = $image_version->getFile();
        $mh->addAttachment($attachment);
    }
}
@$mh->sendMail();