Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mavimo’s picture

Issue tags: +mimemail, +attachment
FileSize
850 bytes

Improved patch to support sending mail using filepath and filecontent.

Into mimemail we can send mail using:

  $params = array(
    'attachments' => array(
      0 => array(
        'filecontent' => '<?xml version="1.0" encoding="UTF-8"? ><test><item /></test>',
        'filename' => 'test.xml',
        'filemime' => 'text/xml',
      ),
      1 => array(
        'filepath' => 'public://test.xml',
        'filename' => 'test.xml',
        'filemime' => 'text/xml',
      ),
    ),
  );

in accord with mimemail docs.

felixvang’s picture

I'm sorry, I'm new to these stuff. What should I do with this .patch file or where should I add it?

gabrielu’s picture

Status: Active » Reviewed & tested by the community

Thanks for the patch @mavimo , patch #1 worked like a charm for me.

I think this should be ported to the next release.

Gabriel

wundo’s picture

Status: Reviewed & tested by the community » Fixed

Commited, thanks! :)

killua99’s picture

Version: 7.x-1.0-beta1 » 7.x-1.x-dev
Category: feature » support
Status: Fixed » Active

When I send a attachment application/pdf mime file it's apear.

The data could not be saved because the destination /tmp/smtpc856Ac is invalid. This may be caused by improper use of file_save_data() or a missing stream wrapper.

File smtp.mail.inc line 432
version = "7.x-1.0-beta2+3-dev"

killua99’s picture

Ok I can't do a patch cause I'm not using git (sorry) but you can follow this instruction.

Replace all the attachment_new_filename for this.

              $attachment_new_filename = drupal_tempnam('temporary://', 'file');
              $file_path               = file_save_data($attachment, $attachment_new_filename, FILE_EXISTS_RENAME);
              $real_path               = drupal_realpath($file_path->uri);

              if (!$mailer->AddAttachment($real_path, $file_name)) { // , $file_encoding, $filetype);
   

This is a good use when you creat a tmp name with tmp directory.

tobiasb’s picture

I get this error with mimemail (rule) and smtp

Notice: Undefined index: filename in SmtpMailSystem->mail() (Line 455 of sites/all/modules/contrib/smtp/smtp.mail.inc).
Notice: Undefined index: filemime in SmtpMailSystem->mail() (Line 455 of sites/all/modules/contrib/smtp/smtp.mail.inc).
killua99’s picture

Status: Active » Needs review
FileSize
1.05 KB

An issue failing with temporary files.

killua99’s picture

Category: support » bug

Change status.

dandaman’s picture

Status: Needs review » Reviewed & tested by the community

The patch in #8 definitely fixed my problem with temporary files from the MIME not saving correctly. Thanks for the patch, killua99!!

wundo’s picture

Status: Reviewed & tested by the community » Fixed

Last patch was committed.

tobiasb’s picture

Status: Fixed » Needs review
FileSize
1.14 KB

Ok my bug is not solved with the latest release.

When you send a html-email via rules to use the attachments, mimemail creates only an array with the filepath key, no filename or something.

rules_action_mimemail -> drupal_mail -> mimemail_mail
(attachment is a string) -> ... -> attachment is an array.

cgdrupalkwk’s picture

Having the same issue as #7. Patch in #12 did not work to solve.

tobiasb’s picture

@cgdrupalkwk then your rule is wrong ;-)

killua99’s picture

Did you test send an attachment without using rules? Because this one seen another kind of issue but with the link of attachment. You use Rules + mimemail + smtp. This issue only use mimemail and smtp. Better to open a new one?

tobiasb’s picture

Ok lets say the patch is a followup. ;-)

chrbak’s picture

I am having a similar problem with the attachment files. I don't use rules only the mimemail with the simplenews module and the error that i am getting is the bellow:

Fatal error: Cannot use object of type stdClass as array in C:\inetpub\wwwroot\sites\all\modules\smtp\smtp.mail.inc on line 449

The solution i found is to delete the lines 447 to 456 and the problem solved. Now i can send newsletters with the attached files.

chrbak’s picture

I found that at line 449 tries to check a variable that is actualy an oblect like the bellow:

$ Object
(
    fid = String(4) "2772"
    uid = String(4) "1235"
    filename = String(45) "filename.pdf"
    uri = String(69) "public://(...)/filename.pdf"
    filemime = String(15) "application/pdf"
    filesize = String(6) "372540"
    status = String(1) "1"
    timestamp = String(10) "1365774040"
    rdf_mapping => Array (0)
    (
    )
)
moonberry’s picture

FileSize
1.15 KB

I have same problem as #17 with sending attachements, here is a patch for dev version 2013-Mar-17.

MarcusOaten’s picture

For some reason I seem to get 2 (duplicate) attachments when using rules to send attachments, with both the patch and current Dev version.

killua99’s picture

+++ b/smtp.mail.inc	2013-04-29 12:22:37.333324000 +0200
@@ -441,12 +441,14 @@
+        if (!is_object($attachment)) {

Why you need to check is not a object? I mean with an isset or !empty you have done that kind of confirmation.

kolafson’s picture

Had the same issue as #7 and patch in #12 solved it.

jurgenhaas’s picture

Patch #12 works fine for me.

wundo’s picture

Status: Needs review » Fixed

Patch from #12 was committed, thanks ;)

Automatically closed -- issue fixed for 2 weeks with no activity.

tetter36’s picture

Version: 7.x-1.x-dev » 7.x-1.2
Issue summary: View changes
Status: Closed (fixed) » Active
FileSize
1.29 KB

I couldn't send out emails using SMTP, Simplenews, Mimemail, and Elysia Cron. I was receiving the following error in my logs:

Unexpected termination of cron channel default, aborted. Last job executed: simplenews_cron

I then looked in the apache error log and saw this:
PHP Fatal error: Cannot use object of type stdClass as array in /var/www/financesociety/sites/all/modules/smtp/smtp.mail.inc on line 469,

Attached is the patch I used to fix the issue. After adding the patch, I didn't get any more errors in the apache log and the emails began sending without the "Unexpected termination of cron channel default, aborted. Last job executed: simplenews_cron" error.

HansKuiters’s picture

Thanks @tetter36, patch #26 works fine for me.

Allerby’s picture

I had a similar problem using Webform2pdf, SMTP and Mimemail. #26 worked for me - however I had to remove the filecontent object reference in the if statement from line 469 and 472. Not a great solution but it did the trick. My final solution looked like this:

<?php
    if (isset($message['params']['attachments'])) {
      foreach ($message['params']['attachments'] as $attachment) {
        if (isset($attachment)) {
          $mailer->AddStringAttachment($attachment->filecontent, $attachment->filename, 'base64', $attachment->filemime);
        }
        if (isset($attachment)) {
          $filename = isset($attachment->filename) ? $attachment->filename : basename($attachment->filepath);
          $filemime = isset($attachment->filemime) ? $attachment->filemime : file_get_mimetype($attachment->filepath);
          $mailer->AddAttachment($attachment->filepath, $filename, 'base64', $filemime);
        }
      }
    }
?>
AgentSeo’s picture

Have similar problem #26, after using patch I didn't get any more errors, but email not sending. If I remove attachments and send email next time, both emails will be sent.

DamienMcKenna’s picture

Status: Active » Needs work

Don't forget to set the status to "needs review" when you upload a patch. Also, the patch doesn't apply.

DamienMcKenna’s picture

Version: 7.x-1.2 » 7.x-1.x-dev

The patch needs to be updated to match the Drupal coding standards.

james.williams’s picture

Status: Needs work » Needs review
FileSize
995 bytes

Patch #26 will only work for file objects. Mimemail supports attachments being added as arrays (when sent through its own hook_mail implementation), but also allows file objects to be sent directly (e.g. when other module's seek to use its mail system).
So if the SMTP module wants to be able to handle them as Mimemail does, it needs to handle both, not just arrays (as currently since #12 was committed), or just objects (as in patch #26).
The attached patch allows either method to be used, by recognising the same properties of either type that Mimemail uses.

BD3’s picture

The patch in #32 is attaching the file in the email for me, but using Webform2PDF it doesn't attach the PDF from Webform2PDF in the email and below is from the error log when it tries to do so.

Notice: Undefined index: exclude_empty in webform_submission_render() (line 645 of /sites/all/modules/webform/includes/webform.submissions.inc).
Notice: Undefined property: stdClass::$filepath in SmtpMailSystem->mailWithoutQueue() (line 495 of /sites/all/modules/smtp/smtp.mail.inc).

Webform 7.x-4.12
Webform2PDF 7.x-4.0
Mime Mail 7.x-1.0-beta4
Mail System 7.x-2.34
SMTP Authentication Support 7.x-1.3

DamienMcKenna’s picture

Status: Needs review » Needs work

Sounds like it needs a little more work.

SkyBuzz’s picture

I've made the few modifications to the #32 and it is working fine for me.

The webform2pdf provides only filecontent #32 provides attachment keys for the filepath. This modification covers both the cases.

BD3’s picture

Status: Needs work » Needs review

I can confirm that #35 works for me with Webform2PDF PDFs being attached and file uploads from the Webform are also being attached.

Status: Needs review » Needs work

The last submitted patch, 35: smtp-sending_attachments-1442514-35.patch, failed testing.

trumanru’s picture

#35 resolved my problem with attachments in Webform

  • wundo committed 364afc7 on 7.x-2.x
    Issue #1442514 by tobiasb: Sending attachments using modules smtp and...
  • wundo committed 542f920 on 7.x-2.x
    Issue #1442514 by lavisrap, mavimo, gabrielu: Fixes sending attachments...
  • wundo committed d37eabe on 7.x-2.x
    Issue #1442514 by killua99: Fixes attachments of temporary files
    
  • wundo committed ec93c27 on 7.x-2.x
    Issue #1442514 by wundo: Fixing indentation added by last patch
    
DamienMcKenna’s picture

Rerolled, and I fixed the errant whitespace and minor coding standards issues.

jroberts’s picture

The patch in #40 resolved the issue for us when using Webform2PDF with SMTP Authentication, MailSystem, MimeMail and attached files in Webform email notifications. Attachments are included properly in all cases.

sam-elayyoub’s picture

Just to make life easier try to build a simple module calling your file and attach it to any email you want to send

/**
* Get your file info 
*/
function YOURMODULE_attach($fid, $language){
return file_load($fid);
}
/**
 * Send an e-mail.
 */
function YOURMODULE_mail($fid){

$key = 'YOURMODULE_mail';
$from = variable_get('site_mail', 'sam@whywebs.com');
$to = 'TEST@TO_WHO.COM';
$file = YOURMODULE_attach($fid);

$msg = array(
      'subject' => 'YOUR SUBJECT',
      'message' => 'YOUR MESSAGE',
      'files' => array(),
    );
if(isset($file)){
          $f1 = new stdClass();
          $f1->uri  = $file->uri;
          $f1->filename  = $file->filename;
          $f1->filemime  = $file->filemime;
          $f1->type  = $file->type;
$msg[files][] = $f1;
}else{
  $msg = array(
      'subject' => 'YOUR SUBJECT',
      'message' => 'YOUR MESSAGE',
);
}

// add watchdog() to make sure you debug everything

watchdog('YOURMODULE', '<pre>' . print_r($msg, TRUE) . '</pre>', array(), WATCHDOG_NOTICE, NULL);

drupal_mail('YOURMODULE', $key, $to, $language, $msg, $from, TRUE);

}

hope this module would solve any problem with the attachments and SWIFTMail or MIMEMail

regards
Sam

Johan den Hollander’s picture

#40 works for me. I applied this patch to the lastest dev.
7.x-2.x-dev - 2016-Dec-26

Using this with Simplenews including pdf attachment.

Sam152’s picture

Any reason this wasn't backported/committed to the 1.x branch?

ann b’s picture

Status: Needs review » Reviewed & tested by the community

#40 worked for me.

Modules Used:

Webform 7.x-4.7
WebForm 2 PDF 7.x-4.0
SMTP Authentication Support 7.x-1.7+3-dev + patch

The site I maintain doesn't use any other mail modules. We do not use mimemail (Am I missing out on something important by not using this module? Why are so many people using this?). Anyway we send out a small number of simple html emails and so far this is all I need.

I tweaked the WebForm 2 PDF code locally and will submit a patch if this patch gets committed here.

       if ($html_capable) {
          if (module_exists('mimemail') || module_exists('smtp')) {
            $attachment = new stdClass;
            $attachment->filecontent = theme('webform2pdf_mail2pdf', array('nid'=>$webform2pdf_send2pdf['nid'],'sid'=> $webform2pdf_send2pdf['sid']));
            $attachment->filename = $filename;
            $attachment->filemime = 'application/pdf';
            $message['params']['attachments'][] = $attachment;
          }
        }
        else { ...

All I did was add the module_exists('smtp') condition to also create the attachment the mimemail way if the smtp module exists. This tweak along with patch #40 worked.

rreiss’s picture

The patch in #40 works for me on SMTP 7.x-1.7 .
Thanks!

mmaldonado’s picture

The patch in # 40 works for me on SMTP 7.x-1.7.
but he gave the error
Notice: Undefined property: stdC lass :: $filecontent in SmtpMailSystem-> mailWithoutQueue () (line 495 of /var/www/html/mysite/sites/all/modules/contrib/smtp/smtp.mail.inc).
This would be the solution to this error:
where is it:

  $attachment [$key] = $file -> {$key};

change for:

  $attachment [$key] = (isset ($file -> $key))? $file -> $key: "";
mmaldonado’s picture

mmaldonado’s picture

robcarr’s picture

The patch at #40 works well for me. The patches and #47 and #49 almost worked, but created a 'ghost' copy of the first attachment (ie, has the title, but nil (0KB) content

wundo’s picture

Status: Reviewed & tested by the community » Needs work
Chris Matthews’s picture

uberhacker’s picture

Johan den Hollander’s picture

Status: Needs work » Reviewed & tested by the community

The #53 patch applies succesfully to the 7.x-1.x-dev version.
Without the patch attachments are not send. No errors.

With the path attachments are send. No errors no problems ;-)

rasmusknabe@gmail.com’s picture

Can anyone point to the correct solution for sending email attachments with mimemail through SMTP Authentication..

I have tried almost every patch I can find and different versions of SMTP Auth and Mimemail...

I keep getting an error about the message body being empty!?

bdeleasa’s picture

Thank you so much!! I have a client running D7, Simplenews and SMTP and when sending a test email that had file attachments, the page would redirect immediately to a fatal error, and it was also logging me out after every time I tried to send a test email. Nodes without file attachments weren't causing any issues sending tests; those sent perfectly.

Patch #53 worked perfectly in my case, no more issues sending test emails with file attachments. Thank you!!

DamienMcKenna’s picture

joseph.olstad’s picture

Assigned: Unassigned » joseph.olstad

  • joseph.olstad committed 9088d6ab on 7.x-1.x authored by mavimo
    Issue #1442514 by mmaldonado, killua99, DamienMcKenna, tobiasb, mavimo,...
joseph.olstad’s picture

Assigned: joseph.olstad » Unassigned
Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.