1

I want to send my order to my email, and also to my client's email.

I am sending an order placed on my website using this code:

function MYMODULE_form_submit($form, &$form_state) {
  order_mail_send($form_state['values']);
}

This is the order_mail_send() code:

function order_mail_send($form_values) {
  $module = 'shop';
  $key = 'key';
  $to = $form_values['email'];

  $language = language_default();
  $params = array();
  $from = NULL;
  $send = FALSE;
  $message = drupal_mail($module, $key, $to, $language, $params, $from, $send);
  $message2 = drupal_mail($module, $key, $from, $language, $params, $from, $send);

  $subject = 'Order Status';

  $message['subject'] = $subject;
  $message['body'] = array();

  $message['body'][] = "<h3>Your order: ".$form_values['first']." ".$form_values['last']."</h3>";

  // …

  // Retrieve the responsible implementation for this message.
  $system = drupal_mail_system($module, $key);


  // Format the message body.
  $message = $system->format($message);
  $message2 = $system->format($message2);

  // Send e-mail.
  $message['result'] = $system->mail($message);
  $message['result2'] = $system->mail($message2);


  if ($message['result'] && $message['result2'] == TRUE) {
    drupal_set_message(t('Your order has been sent. Thank you!'));
  }
  else {
    drupal_set_message(t('There was a problem sending your message and it was not sent.'), 'error');
  }
}
  1. Now it sends the first email ($message = $system->format($message); but not the second one. How can I make it work?
  2. How can I empy the cart values after sending the emails? The cart empties when I close the browser, but I want to delete it after sending as well.

2 Answers 2

4

You can use CC to send a copy to admin/yourself
$message['headers']['CC'] = "[email protected]"

2
  • Seems that this will stop my emails to be send. It throws me an error. Commented Mar 7, 2012 at 13:05
  • As suggested by kiamlaluno, you need to change your code a bit , I suggest you have a look here for detailed explanation drupalcontrib.org/api/drupal/… Click on view source
    – GoodSp33d
    Commented Mar 7, 2012 at 13:41
3

Your code is not correct, as a module that calls drupal_mail() doesn't then call drupal_mail_system(), but it implements hook_mail(). An example of this hook implementation is contact_mail(), which handles the emails sent from the Contact module in contact_personal_form_submit(), and contact_site_form_submit().

  // contact_site_form_submit()
  $values = $form_state['values'];
  $values['sender'] = $user;
  $values['sender']->name = $values['name'];
  $values['sender']->mail = $values['mail'];
  $values['category'] = contact_load($values['cid']);

  // Save the anonymous user information to a cookie for reuse.
  if (!$user->uid) {
    user_cookie_save(array_intersect_key($values, array_flip(array('name', 'mail'))));
  }

  // Get the to and from e-mail addresses.
  $to = $values['category']['recipients'];
  $from = $values['sender']->mail;

  // Send the e-mail to the recipients using the site default language.
  drupal_mail('contact', 'page_mail', $to, language_default(), $values, $from);

  // If the user requests it, send a copy using the current language.
  if ($values['copy']) {
    drupal_mail('contact', 'page_copy', $from, $language, $values, $from);
  }

  // Send an auto-reply if necessary using the current language.
  if ($values['category']['reply']) {
    drupal_mail('contact', 'page_autoreply', $from, $language, $values, $to);
  }

As also said from kantu, you can CC send the email to any other user that is not the receiver.

4
  • Hello kiamluno. I still don't understand what's wrong by using: $message['result'] = $system->mail($message); ? Commented Mar 6, 2012 at 14:06
  • 1
    Modules sending an email never call drupal_mail_system(); as consequence, your code should not contain $system->mail($message).
    – avpaderno
    Commented Mar 6, 2012 at 14:13
  • Thanks, I found this code snippet somewhere and I used it for system->format in order to format the body...I found your information very useful. I will format it using mimemail module now. Commented Mar 6, 2012 at 14:48
  • Hello @kiamluno, I managed to rewrite my code using your suggestion. Now I am using hook_mail() (tough I am not very convinced of its utility as long as I can compose my email body in hook_mail_submit). Commented Mar 7, 2012 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.