thelinh bui
thelinh bui

Reputation: 226

drupal 7: mail does sent but body and subject are empty

I am trying sending email with Drupal 7. The email is sent to my email account but somehow the body and subject is empty. Please help me to explain why. Thank you very much.

Below is my code:

This is the form

function get_friendform($form,&$form_submit){
    $form['fullname'] = array(
        '#title' => t('Your Full Name: '),
        '#type' => 'textfield',
        '#required' => TRUE,
        '#size' => 30,
    );

    $form['email'] = array(
        '#title' => t('Your Full Email: '),
        '#type' => 'textfield',
        '#required' => TRUE,
        '#size' => 30,
    );

    $form['friend_email'] = array(
        '#title' => t('Your Friend Email: '),
        '#type' => 'textfield',
        '#required' => TRUE,
        '#size' => 30,
    );


    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'submit',
        '#submit' => array('sendform_form_submit')

    );

    return $form;
}



function sendform_form_submit($form,&$form_submit){
    $fullname = $form_submit['values']['fullname'];
    $email = $form_submit['values']['email'];
    $friend_email = $form_submit['values']['friend_email'];
    $current_page = $GLOBALS['base_url'] .'/'.current_path();

    $mailto = '[email protected]';
    $mailfrom = '[email protected]';
    $subject = "Links to event";
    $body = $current_page;
    $params = array(
        'body' => $body,
        'subject' => $subject,
    );

    if (drupal_mail('get_friendform', 'send_link', $mailto, language_default(),$params,$mailfrom,TRUE)) {
        drupal_set_message(t('Your message was sent successfully!!!'));
    }

}



function sendform_mail($key,&$message,$params) {
    $language = $message['language'];
    switch ($key) {
        case 'send_link':
            $message['subject']=t($params['subject'], $var, $language->language);
            $message['body'][]=$params['body'];
            $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
        break;
    }

}

Thank you very much

new code

function sendform_form_submit($form,&$form_submit){
    $fullname = $form_submit['values']['fullname'];
    $email = $form_submit['values']['email'];
    $friend_email = $form_submit['values']['friend_email'];
    $current_page = $GLOBALS['base_url'] .'/'.current_path();

    $mailto = '[email protected]';
    $mailfrom = '[email protected]';
    $subject = "Links to event";
    $params ='';
    $body[] = $current_page;
    $mail_message = drupal_mail('send_form', 'some_key', $mailto, language_default(), $params, $mailfrom, TRUE);
    $mail_message['subject'] = $subject;
    $mail_message['body'] = $body;

    $mail_system = drupal_mail_system($module, $key);
    $mail_message = $mail_system->format($mail_message);

    $mail_message['result'] = $mail_system->mail($mail_message);

}

Upvotes: 4

Views: 4024

Answers (1)

Muhammad Reda
Muhammad Reda

Reputation: 27043

Use the following code example to send emails using drupal:

$body = array();
$body[] = "Mail body";
$mail_message = drupal_mail($module, $key, $to, $lang, $params, $from, $send);
$mail_message['subject'] = $subject;
$mail_message['body'] = $body;

$mail_system = drupal_mail_system($module, $key);
$mail_message = $mail_system->format($mail_message);

$mail_message['result'] = $mail_system->mail($mail_message);

Update 1:

There's a problem in your code. You have created a function sendform_mail() and you never called it.

You can use the code example above inside the form submission callback sendform_form_submit(). This is much simpler

Update 2:

$mail_message = drupal_mail('send_form', 'some_key', $mailto, language_default(), $params, $mailfrom, TRUE);

In your code, change the last value to false, because you need to stall the mail sending till the last line $mail_message['result'].

Hope this works... Muhammad.

Upvotes: 6

Related Questions