2

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();

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?

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.

[SOLVED]
Thanks to Nicolai, here's a working example for attaching files:

$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);

//Delete the file if not wanted on server
$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();
linuxoid
  • 1,415
  • 3
  • 14
  • 32

1 Answers1

1

To add the file to your filesystem, you should take a look at this

http://concrete5.org/api/class-Concrete.Core.File.Importer.html.

On the returned object (which is a FileVersion on success), you should be able to call getFile( ) to get the actual Concrete5 File object

Nicolai Krüger
  • 416
  • 4
  • 17
  • Nicolai, I tried $file = $_FILES['photo']['tmp_name']; $filename = $_FILES['photo']['name']; $importer = new \Concrete\Core\File\Importer(); $result = $importer->import($file, $filename); $mh->addAttachment($result); but it gives me an error: "Argument 1 passed to Concrete\Core\Mail\Service::addAttachment() must be an instance of Concrete\Core\File\File, instance of Concrete\Core\File\Version given" This example is also from here: https://www.concrete5.org/documentation/developers/5.7/working-with-files-and-the-file-manager/importing-new-files/ – linuxoid Oct 16 '15 at 02:26
  • 1
    Got it! Got it! Got it! I forgot $file_version = $importer->import($file, $filename); $attachment = $file_version->getFile(); and now it works!!! Thank you very much, Nicolai for your suggestion about the getFile() – linuxoid Oct 16 '15 at 02:34
  • 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 Oct 17 '15 at 02:30
  • Hmm.. I that the "easy" way will be to delete the file afterwards. Else it seems that the mailer object (from C5) is a subclass of the mail object from Zend. So you could find the version C5 is using and look at the documentation – Nicolai Krüger Oct 18 '15 at 12:34
  • 1
    Yes, this is what I had to do. But I htought there should be a better way without uploading and then deleting. – linuxoid Oct 19 '15 at 02:02
  • @linuxoid i can only agree with you! You could check if there already is an issue on their github, else you could create one. https://github.com/concrete5/concrete5 – Nicolai Krüger Oct 19 '15 at 07:19