How to a send mail in Drupal?

To send a mail, we will use drupal_mail() function.
Here I am giving you an example, how you can send mail.

call this function where you want to send mail.
/*
  here test is the name of function, first_mail is the case value we are passing for switch case.
  $toEmail is the email id to whom you want to send mail, language is English and $params is the array of     parameters you want to send in mail.
 */
drupal_mail ('test', 'first_mail', $toEmail, "English", $params);
 
// The same way pass other case values to send some other mail.
drupal_mail ('test', 'second_mail', $toEmail, "English", $params);

Here we are writing mail function.
 
function test_mail($key, &$message, $params) {
$headers = array(
                        'Content-Type' => ' text/html'  // this is the header of the mail
                    );
$headers['From']="test@test.com";   // Write the email id from which you want to send the mails
$message['headers'] = $headers;
    switch($key) {
    case 'first_mail':    //case value which we are passing in drupal_mail
        $subject="Write Your Mail Subject";  // Write the subject of the mail here
        $body="";   // Write the body of your mail
        $message['subject'] = t($subject);
        $message['body'][] = $body;
    break;
    case 'second_mail':
        $subject="Write Your Mail Subject";  // Write the subject of the mail here
        $body="";   // Write the body of your mail
        $message['subject'] = t($subject);
        $message['body'][] = $body;
    break;
    }
}

No comments:

Post a Comment