I have a problem with sending mail with attachment in Drupal 7.
I tried usual module, I tried mimemail, I tried Zend Framework (or something like that)... but it just doesn't work. I get an email with a message, but it doesn't contain attachment.
here is my code:
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' => '[email protected]',
'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.
}
}
}
I installed SwiftMailer drupal module, and now he is sending attachment but without Body and Subject of mail. What should I do to send them?
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,'[email protected]');
drupal_mail('modulename', 'key',$ourpost, language_default(), $p,'[email protected]');
}
}
Some cursory googling reveals it's thorny at best.
Try this class:
https://github.com/gollyg/Attachment-Email
(via: http://www.metachunk.com/blog/sending-e-mails-attachments-drupal-7)
Or use SwiftMailer:
http://swiftmailer.org
I'm unsure if the latter will be part of Drupal 8, but it will at least be compatible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With