# This patch file was generated by NetBeans IDE
# Following Index: paths are relative to: sites\all\modules\amazon_ses
# This patch can be applied using context Tools: Patch action on respective folder.
# It uses platform neutral UTF-8 encoding and \n newlines.
# Above lines and this line are ignored by the patching process.
Index: amazon_ses.admin.inc
--- amazon_ses.admin.inc Base (BASE)
+++ amazon_ses.admin.inc Locally Modified (Based On LOCAL)
@@ -97,6 +97,60 @@
     '#default_value' => t('If you receive this message it means your site is capable of using Amazon SES to send email.'),
   );
 
+    $form['send_raw'] = array(
+    '#type'  => 'fieldset',
+    '#title' => t('Configure raw email options'),
+    '#description' => t('Use raw emails if you wish to send attachments, inline graphics, or wish to implement DKIM for improved spam handling. For more information about configuring DKIM, please see http://code.google.com/p/php-mail-domain-signer/wiki/Overview or http://www.dkim.org'),
+    '#collapsible' => TRUE,
+    '#collapsed' => FALSE,
+  );
+  $form['send_raw']['amazon_ses_send_raw'] = array(
+    '#type' => 'radios',
+    '#title' => t('Send as a fully-formatted raw email (Requires Mimemail)'),
+    '#default_value' => variable_get('amazon_ses_send_raw', 0),
+    '#options' => array(1 => t('Yes'), 0 => t('No')), 
+    '#description' => t('Use the amazon_ses_raw_send function to send email. Requires Mimemail to format message'),
+  );
+  
+  if (!module_exists("mimemail")) { //Check to ensure mimemail exists before allowing user to enable
+    $form['send_raw']['amazon_ses_send_raw']['#disabled'] = TRUE;
+    $form['send_raw']['amazon_ses_send_raw']['#description'] = t('Use the amazon_ses_raw_send function to send email. Requires Mimemail to format message. WARNING: Mimemail is not installed');
+  }
+  
+  $form['send_raw']['amazon_ses_dkim'] = array(
+    '#type' => 'radios',
+    '#title' => t('Digitally sign the message with DKIM'),
+    '#default_value' => variable_get('amazon_ses_dkim', 0),
+    '#options' => array(1 => t('Yes'), 0 => t('No')), 
+    '#description' => t('Sign the message with DKIM to improve deliverability and reduce risk of spam flagging. See www.dkim.org'),
+  );
+
+  if(!file_exists(drupal_get_path('module', 'amazon_ses') . '/lib/class.mailDomainSigner.php')) {
+    $form['send_raw']['amazon_ses_dkim']['#disabled'] = TRUE;
+    $form['send_raw']['amazon_ses_dkim']['#description'] = t('Sign the message with DKIM to improve deliverability and reduce risk of spam flagging. See www.dkim.org. WARNING: lib/class.mailDomainSigner.php not found. Please add to enable dkim');    
+  }
+  
+  $form['send_raw']['amazon_ses_dkim_domain'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Dkim Domain'),
+    '#default_value' => variable_get('amazon_ses_dkim_domain', ""),
+    '#description' => t('The domain you will be sending email from.'),
+  );
+
+    $form['send_raw']['amazon_ses_dkim_selector'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Dkim selector'),
+    '#default_value' => variable_get('amazon_ses_dkim_selector', ""),
+    '#description' => t('The selector to use. Your public key must be added as a txt dns record at SELECTOR._domainkey.DOMAIN.COM'),
+  );
+  
+    $form['send_raw']['amazon_ses_dkim_private'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Dkim private key'),
+    '#default_value' => variable_get('amazon_ses_dkim_private', ""),
+    '#description' => t('The contents of the private key.'),
+  );
+ 
   $form['#submit'][] = 'amazon_ses_admin_settings_submit';
   return system_settings_form($form);
 }
@@ -138,17 +192,23 @@
     drupal_set_message(t('A test email has been sent to @email.', array('@email' => $test_address)));
   }
 
-  if ($values['amazon_ses_on']) {
+  if ($values['amazon_ses_on'] && !$values['amazon_ses_send_raw']) {
     variable_set('smtp_library', drupal_get_filename('module', 'amazon_ses'));
     drupal_set_message(t('Amazon SES will be used to deliver all site e-mails.'));
     watchdog('amazon-ses', 'Aamazon SES has been enabled.');
-  }
-  else {
+  } else {
+    if ($values['amazon_ses_send_raw']) {
+      variable_set('smtp_library', drupal_get_filename('module', 'mimemail'));
+      variable_set('mimemail_engine', "amazon_ses");
+      drupal_set_message(t('Mimemail + Amazon SES will be used to deliver all site e-mails.'));
+      watchdog('amazon-ses', 'Amazon SES has been enabled.');
+    } else {
     variable_del('smtp_library');
     drupal_set_message(t('Amazon SES has been disabled.'));
     watchdog('amazon-ses', 'Amazon SES has been disabled.');
   }
 }
+}
 
 /**
  * Submission for the administrative settings form - new address.
Index: amazon_ses.mail.inc
--- amazon_ses.mail.inc Base (BASE)
+++ amazon_ses.mail.inc Locally Modified (Based On LOCAL)
@@ -100,3 +100,74 @@
     }
     return TRUE;
   }
+
+  
+    /**
+ * Send a raw email message, for example, one generated by mimemail
+ *
+ * @param $message
+ *   A message array, as output by mimemail
+  'address' => to email address,
+  'subject' => $subject,
+  'body'    => email body, already formatted as a mime multipart message,
+  'headers' => array(header_type=>data)
+ * @return
+ *   TRUE if the mail was successfully accepted, otherwise FALSE.
+ */
+function amazon_ses_raw_send($message) {
+  $source = variable_get('amazon_ses_from', '');
+  if (!$source) {
+    drupal_set_message(t('Amazon SES can\'t send email. Please <a href="!config">configure a valid From address', array('!from' => url('admin/settings/amazon_ses'))), 'error');
+    return FALSE;
+  }
+
+  //Add subject and to fields to headers
+  $message['headers']['Subject'] = $message['subject'];
+  $message['headers']['To'] = $message['address'];
+
+  //Fix headers
+  $headers = "";
+  unset($message['headers']['Errors-To']); //Mimemail is setting this, but it's depreciated and AWS SES doesn't like it at all.
+  if ($message['headers']['Return-Path'] == '<>')
+    unset($message['headers']['Return-Path']);  //If Return-Path isn't set, kill it (@see http://drupal.org/node/1190144)
+
+    
+//Break out the headers
+  foreach ($message['headers'] as $key => $value) {
+    if (is_array($value)) {
+      $value = '"' . $value['name'] . '" <' . $value['mail'] . '>'; //Check if we need to break up an array
+    }
+    $headers .= $key . ': ' . $value . "\r\n";
+  }
+
+  $headers .= "\r\n";
+
+  //Add headers to body to create final message
+
+  $message['body'] = $headers . $message['body'];
+
+  //Run DKIM Signing
+
+  if (variable_get('amazon_ses_dkim', 0)) {
+    module_load_include('php', 'amazon_ses', 'lib/class.mailDomainSigner');
+
+    $dkim = new mailDomainSigner(variable_get('amazon_ses_dkim_private', ""), variable_get('amazon_ses_dkim_domain', ""), variable_get('amazon_ses_dkim_selector', ""));
+    $message['body'] = $dkim->sign($message['body']);
+  }
+
+  //Send the message
+  if (variable_get('amazon_ses_debugging', 0)) {
+    watchdog('amazon-ses', 'Sending mail to: @to', array('@to' => $message['address']));
+    drupal_set_message(t('Sending mail to: @to.', array('@to' => $message['address'])), 'notice');
+  }
+
+  $mailer = new AmazonSES();
+  $status = $mailer->send_raw_email(array('Data' => base64_encode($message['body'])));
+
+  if (!$status->isOK()) {
+    watchdog('amazon-ses', 'Error sending email from @from to @to : !error_message', array('@from' => $message['sender']['mail'], '@to' => print_r($message['address'], TRUE), '!error_message' => (string) $status->body->Error->Message), WATCHDOG_ERROR);
+    watchdog('amazon-ses', '<pre>Headers: !source <br />', array('!source' => $headers), WATCHDOG_ERROR);
+    return FALSE;
+  }
+  return TRUE;
+}
\ No newline at end of file
Index: amazon_ses.module
--- amazon_ses.module Base (BASE)
+++ amazon_ses.module Locally Modified (Based On LOCAL)
@@ -96,6 +96,10 @@
     case 'single':
     case 'send':
       module_load_include('inc', 'amazon_ses', 'amazon_ses.mail');
+      if(variable_get('amazon_ses_send_raw', 0)) {
+        return amazon_ses_raw_send($message);        
+      } else {
       return amazon_ses_send($message);
   }
 }
+}
\ No newline at end of file
Index: Raw+DKIM_README.txt
--- Raw+DKIM_README.txt Locally New
+++ Raw+DKIM_README.txt Locally New
@@ -0,0 +1,32 @@
+In order to enable sending RAW MIME-encoded messages, you will need to have the mimemail module installed and configured.
+
+http://drupal.org/project/mimemail
+
+IMPORTANT:
+There are several bugs in the current version of mimemail which may cause issues when used with Amazon SES. 
+You may need to include the following patches if they have not yet been integrated into mimemail:
+
+http://drupal.org/node/1270656
+http://drupal.org/node/1270686
+
+
+DKIM support:
+
+DKIM allows you to cryptographically sign your emails to increase deliverabilty and reduce the chances of your messages being marked as spam.
+
+To enable DKIM support with Amazon SES, you must download the php-mail-domain-signer package:
+http://code.google.com/p/php-mail-domain-signer
+
+Place the lib folder within the amazon_ses directory.
+
+Visit the amazon_ses configuration page in order to set up the details necessary for using DKIM, including entering your private key.
+You will also need to correctly configure your DNS records.
+
+For additional information, please see:
+http://code.google.com/p/php-mail-domain-signer/wiki/Overview
+
+For questions, raves, etc:
+http://drupal.org/user/1013786/
+
+
+
