When sending mail with a custom module I am receiving the following error:
Notice: Undefined index: from in SmtpMailSystem->mail() (line 51 of /var/www/vhosts/example.com/old/sites/all/modules/smtp/smtp.mail.inc).
Notice: Undefined variable: content_type in SmtpMailSystem->mail() (line 304 of /var/www/vhosts/example.com/old/sites/all/modules/smtp/smtp.mail.inc).
Notice: Undefined variable: content_type in SmtpMailSystem->mail() (line 314 of /var/www/vhosts/example.com/old/sites/all/modules/smtp/smtp.mail.inc).
Notice: Undefined variable: content_type in SmtpMailSystem->mail() (line 335 of /var/www/vhosts/example.com/old/sites/all/modules/smtp/smtp.mail.inc).
My custom module mail function is
function my_module_drupal_mail($from = 'default_from', $to, $subject, $message) {
$my_module = 'm_relation';
$my_mail_token = microtime();
if ($from == 'default_from') {
// Change this to your own default 'from' email address.
$from = variable_get('system_mail', 'My Email Address <example@example.com>');
}
$message = array(
'id' => $my_module . '_' . $my_mail_token,
'to' => $to,
'subject' => $subject,
'body' => array($message),
'headers' => array(
'From' => $from,
'Sender' => $from,
'Return-Path' => $from,
),
);
$system = drupal_mail_system($my_module, $my_mail_token);
$message = $system->format($message);
if ($system->mail($message)) {
return TRUE;
}
else {
return FALSE;
}
}
Server PHP info:
PHP 5.3.2-1ubuntu4.9
PHP extensions Enabled
PHP memory limit 128M
PHP register globals Disabled
Let me know how I can help. I am a jr developer who know very little about sending mail via php. I will continue to dig deeper and report back
Comments
Comment #1
nikolaosinlight commentedI am seeing the exact same error messages in the logs though emails are being sent out successfully.
However, there are 2 issues:
1) Only the default from is ever used... i.e. it will not accept the from that I pass in to the call.
2) For EACH email I am seeing 5 log messages that are needlessly filling logs.
Our code is pretty much identical to that of the OP.
Comment #2
renoise commentedGot the samle problem here and fixed this by providing "from" and "Content-Type":
But yay, this should not be necessary according to http://api.drupal.org/api/drupal/includes%21mail.inc/function/drupal_mail/7 ?
Comment #3
jpshayes commentedThanks taktik. That fixed the problem for me as well. I am marking this issue priority to minor because the above solution works. If anyone believes this is "works as designed", Feel free to close this issue.
Comment #4
cutcopypaste commentedHaving the same issue
my message headers look like
(already have the content type and From set
Comment #5
anavrin commentedHi cutcopypaste!
It's probably late ... however maybe someone else will need this tip...
You're writeing about 'From' an index in message array but in headers array...
The warning is about the from index in the message array
$message = array(
'id' => $my_module . '_' . $my_mail_token,
'from' => $from, //<-- THIS ONE - 1
'to' => $to,
'subject' => $subject,
'body' => array($message),
'headers' => array(
'From' => FROM, //<-- NOT THIS ONE - 2
'Sender' => FROM,
'Return-Path' => FROM,
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=UTF-8; format=flowed; delsp=yes',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal',
),
);
Check this out I had missing the first (1) from in my code.
regards,
Marcin
Comment #6
knibals@anavrin Thx, this helped!
Comment #6.0
knibalsRemoved server address and replaced with example.com
Comment #7
wundo commentedClosing very old (dead) issues, if you think this is still relevant please re-open.