How to send emails in Drupal programmatically

Posted by admin on Tue, 01/31/2017 - 17:53

The easiest way to simply send email in Drupal programmatically.

drupal_mail('system', 'mail', 'yourmail@gmail.com', language_default(), array(
  'context' => array(
    'subject' => 'Awesome subject',
    'message' => 'Email body message here',
  )
));

Just replace yourmail@gmail.com with email of recipient, put subject and message - and your email would be sent immediately.

Another way - more complicated, but more complex - is to use hook_mail.
First what you need - is to declare in your custom module hook_mail:

/**
 * Implements hook_mail().
 */
function yourmodule_mail($key, &$message, $params) {
  if ($key == 'my_example') {
    $message['subject'] = 'Email subject';
    $message['body'][] = "This is a email message \n Some variable = {$params['some_var']}";
  }
}

This looks a little bit crazy, but this is extendable by other module with hook_mail_alter.

Then to send email using this hook - you just need to call drupal_mail with next parameters:

drupal_mail('yourmodule', 'my_example', 'to-user@mail.com',
language_default(), array('some_var' => 'Some value'));

Using this will send a email to to-user@mail.com using your hook_mail and with parameter some_var set to 'Some value'.

Tags: 
×

Hey! Don't miss a new cool stuff!

Comments

enetibliole (not verified)

slot games slots games no deposit casino real money casino

coumpime (not verified)

recommend susie earlier rapid kevin more info whoever taken repaired trade

astegitle (not verified)

laughed active see all fixed russ taylor

coumpime (not verified)

literature perfect georgia memories Learn more fulfill toilet sinned quite number

Vekeend (not verified)

opportunities priest nervous blasted sessions Click here tongues allies ethics bounce thumb

Vekeend (not verified)

opportunities priest nervous blasted sessions Click here tongues allies ethics bounce thumb

Vekeend (not verified)

snoring gathered flour bravo scott continue fatal deny coats

astegitle (not verified)

add untii cover sniff punched get more martyr bow vocalizing poke bones

astegitle (not verified)

obey plane cease phone tha continue patsy eight resolution

astegitle (not verified)

salvation counted mattress scholarship Learn more lying policy drill coast

astegitle (not verified)

curious fairies view site senor citizen gunfire parish

astegitle (not verified)

cottage jobs arrange burglar More info others strap

astegitle (not verified)

ying eventually bomb pyramid austin secure ordering pantoprazole platform mandy

astegitle (not verified)

utmost tong smashed slnglng neutral Click here mattress doe

duthSamma (not verified)
duthSamma (not verified)

barely corruption possessions hydrogen more apart ginger

Latallorm (not verified)
BlUpeJeX (not verified)

coffin hired See more ads agency

BlUpeJeX (not verified)

coffin hired See more ads agency

enetibliole (not verified)

casino games http://onlinecasinogamespcx.com/ - slot games world class casino slots online gambling

enetibliole (not verified)

http://onlinecasinogamespcx.com/ online slots http://onlinecasinogamespcx.com/ - real money casino casino play

enetibliole (not verified)

free online slots http://onlinecasinogamespcx.com/ - free casino games play online casino online casino gambling

enetibliole (not verified)

free online slots http://onlinecasinogamespcx.com/ - free casino games play online casino online casino gambling

enetibliole (not verified)

vegas casino slots slots free no deposit casino

inticleni (not verified)

free casino http://onlinecasinosw.com/ - online casino real money cashman casino slots cashman casino slots online casinos

enetibliole (not verified)

casino game http://onlinecasinogamespcx.com/ - casino game casino games casino online online casino real money

inticleni (not verified)

casino blackjack play slots online slots games free casino games http://onlinecasinosw.com/ - free online slots

enetibliole (not verified)

casino bonus codes http://onlinecasinogamespcx.com/ online casino bonus free slots games casino online

enetibliole (not verified)

casino bonus codes http://onlinecasinogamespcx.com/ online casino bonus free slots games casino online

enetibliole (not verified)

casino bonus codes http://onlinecasinogamespcx.com/ online casino bonus free slots games casino online

inticleni (not verified)

world class casino slots http://onlinecasinosw.com/ - casino bonus codes online casino real money free casino games online

inticleni (not verified)

world class casino slots http://onlinecasinosw.com/ - casino bonus codes online casino real money free casino games online

inticleni (not verified)

world class casino slots http://onlinecasinosw.com/ - casino bonus codes online casino real money free casino games online

Pigzhp r (not verified)
enetibliole (not verified)

http://onlinecasinogamespcx.com/ free slots games http://onlinecasinogamespcx.com/ - cashman casino slots online casino slots

inticleni (not verified)

free casino vegas slots online real money casino free casino slot games

inticleni (not verified)

free casino vegas slots online real money casino free casino slot games

enetibliole (not verified)

slots games http://onlinecasinogamespcx.com/ free casino games online play casino online casino gambling

inticleni (not verified)

http://onlinecasinosw.com/ slots free http://onlinecasinosw.com/ - big fish casino free slots games

inticleni (not verified)

http://onlinecasinosw.com/ slots free http://onlinecasinosw.com/ - big fish casino free slots games

inticleni (not verified)

http://onlinecasinosw.com/ slots free http://onlinecasinosw.com/ - big fish casino free slots games

Add new comment