I've successfully managed to send email by using drupal_mail and hook_mail, but the next step is to send attachment within the email. I've read that this is not possible without adding extra modules, like MIME and Mail System.
OK, here's my code that sends email without attachments:
<?php
//The form page callback
function mymodule_news_page($form, &$form_state) {
$form['title'] = array(
'#type' => 'markup',
'#markup' => '<div class="add-news-title">Add news</div>',
);
$form['upload'] = array(
'#type' => 'managed_file',
'#title' => t('Upload image'),
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => t('Description'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#submit' => array('mymodule_news_submit'),
);
return $form;
}
function mymodule_news_submit($form, &$form_state) {
$upload = $form_state['values']['upload'];
$description = $form_state['values']['description'];
$params = array('upload' => $upload, 'description' => $description );
drupal_mail('mymodule', 'mymodule_news_mail', 'myemail', language_default(), $params);
drupal_set_message(t('Message sent!'));
//dpm($form_state);
}
/*
** Implements hook_mail().
*/
function mymodule_mail($key, &$message, $params) {
switch($key) {
case 'mymodule_news_mail':
$message['subject'] = t('Hello');
$message['body'][] = t('This is a test for adding news');
$message['body'][] = t('Image upload: @upload', array('@upload' => $params['upload']));
$message['body'][] = t('Description: @description', array('@description' => $params['description']));
break;
}
}
As you can see I have a form that has three fields: upload, description and submit button. I'm trying to upload a image, but on submitting I receive an email with filled description and the uploaded image is not in the email, but it shows some numbers like 110...
Now I've enabled the MIME and Mail System module, and from their README module I can see this line: "'attachments': Array of arrays with the path or content, name and MIME type of the file (optional)."
I guess this is for uploading files, but there's no other documentation, so it's not clear how can I use MIME mail with hook_mail and drupal_mail().
What do I have to add in my code, so the uploaded file will be sent into the email?