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 = 'thelinhuk@gmail.com';
$mailfrom = 'thelinhuk@yahoo.com';
$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 = 'thelinhuk@gmail.com';
$mailfrom = 'thelinhuk@yahoo.com';
$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);
}
Use the following code example to send emails using drupal:
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 simplerUpdate 2:
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.