我在 Drupal 7 中发送带有附件的邮件时遇到问题。
我尝试了通常的模块,我尝试了 mimemail,我尝试了 Zend Framework(或类似的东西)......但它只是不起作用。我收到一封带有消息的电子邮件,但它不包含附件。
这是我的代码:
function my_form_submit() {
if(!empty($_POST['body'])) {
$postbody = $_POST['body'];
$userpost = $_POST['usermail'];
$attachment = array(
'filecontent' => file_get_contents('sites/default/files/test.txt'),
'filename' => 'test.txt',
'filemime' => 'text/plain',
);
$body = ' <html>
<body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0" bgcolor="#ffffff" >
<span style="width:100%;float:left">
<img style="width:20%; float:left" src="cid:logo" alt="" />
<div style="width:80%; float:left">
</div></span>
<span style="width:100%; float:left">'.$_POST['body'].'</span>
</body></html>';
$my_module = 'mime';
$my_mail_token = 'notice';
$message = array(
'to' => '"'.addslashes(mime_header_encode('Request')) .'"<'.$_POST['mail'].'>',
'subject' => t('[Hinnaparing]'),
'body' => $body,
'headers' => array(
'From' => 'noreply@test.com',
'MIME-Version' => '1.0',
'Content-Type' => 'text/html;charset=utf-8',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal',
),
);
$message['headers']['CC'] = '<'.$_POST['usermail'].'>';
$message['params']['attachments'][] = $attachment;
$system = drupal_mail_system($my_module, $my_mail_token);
if ($system->mail($message)) {
// Success.
}
else {
// Failure.
}
}
}
我安装了 SwiftMailer drupal 模块,现在他正在发送附件,但没有邮件的正文和主题。我应该怎么做才能发送它们?
function my_form_submit() {
if(!empty($_POST['body'])) {
$postbody = $_POST['body'];
$userpost = $_POST['usermail'];
$ourpost = $_POST['mail'];
$body = ' <html>
<body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0" bgcolor="#ffffff" >
<span style="width:100%;float:left">
<img style="width:20%; float:left" src="cid:logo" alt="" />
<div style="width:80%; float:left">
<h1>Hinnaparing № '.$file.'</h1>
</div></span>
<span style="width:100%; float:left">'.$_POST['body'].'</span>
</body></html>';
//File two (not managed by Drupal).
$fileone = new stdClass();
$fileone->uri = 'sites/default/files/034.jpg';
$fileone->filename = 'drupal_logo.jpg';
$fileone->filemime = 'image/jpeg';
// Add attachments.
$p['files'][] = $fileone;
// Send e-mail.
drupal_mail('modulename', 'key',$userpost, language_default(), $p,'noreply@test.com');
drupal_mail('modulename', 'key',$ourpost, language_default(), $p,'noreply@test.com');
}
}