Make WordPress Core

Opened 10 years ago

Closed 18 months ago

Last modified 16 months ago

#28407 closed enhancement (fixed)

You are unable to override the attachment name in wp_mail() when adding attachments

Reported by: syntaxart's profile syntaxart Owned by: johnjamesjacoby's profile johnjamesjacoby
Milestone: 6.2 Priority: normal
Severity: normal Version: 3.9.1
Component: Mail Keywords: has-patch has-unit-tests needs-testing add-to-field-guide
Focuses: Cc:

Description

Hi,

When you create an email and add an attachment your may have obtained the attachment filepath using the file upload control. This means you will have a .tmp file extension on your email attachment. When the user receives the email, the attachment is not useful. In order to get the attachment to work correctly you need to first add the tmp filepath and then override the filename. PHPMailer allows you to do this with the AddAttachment function. The wordpress wp_mail function does not allow for this to happen.

The problem with wp_mail() in wp-includes/pluggable.php is at line 483

if ( !empty( $attachments ) ) {
		foreach ( $attachments as $attachment ) {
			try {
				$phpmailer->AddAttachment($attachment);
			} catch ( phpmailerException $e ) {
				continue;
			}
		}
	}

I overcame this problem by copying the function and then replacing the above block of code with

if (!empty($attachments)) {
                foreach ($attachments as $name => $attachment) {
                    try {
                        $phpmailer->AddAttachment($attachment, $name);
                    } catch (phpmailerException $e) {
                        continue;
                    }
                }
            }

The wp_mail function is an important core function and in my opinion is best not to override.

Is it possible to enhance the wp_mail function for this. I have read forums on the internet which has pointed to the issue above. It would be good to offer some of the features which phpmailer is offering.

Thank you very much.

Ryan

Attachments (4)

28407.diff (912 bytes) - added by rittesh.patel 10 years ago.
Check for whether it is associative array or sequential array for attachments
28407.2.diff (2.2 KB) - added by swissspidy 6 years ago.
Refreshed patch with unit tests
28407-alternative.diff (3.3 KB) - added by swissspidy 6 years ago.
Alternative patch that allows changing every argument of addAttachment()
28407.3.diff (2.2 KB) - added by johnjamesjacoby 18 months ago.
Refresh

Download all attachments as: .zip

Change History (35)

#1 @rittesh.patel
10 years ago

You need to check that whether it is associative array or sequential array.

@rittesh.patel
10 years ago

Check for whether it is associative array or sequential array for attachments

#2 @syntaxart
10 years ago

Thank you Rittesh. Will this change be added to the next version of WordPress.

#3 @syntaxart
10 years ago

I have placed your solution into a function and have used the add_action hook 'phpmailer_init'. This works perfectly fine for this. Thank you for your help.

#4 follow-up: @SergeyBiryukov
10 years ago

#28782 was marked as a duplicate.

#5 in reply to: ↑ 4 @p_j_albuquerque
10 years ago

Replying to SergeyBiryukov:

#28782 was marked as a duplicate.

It is not possible to upload multiple file attachment.
It would be possible to change.

if ( !empty( $attachments ) ) {
	foreach ( $attachments as $attachment ) {
		try {
			if(is_array($attachment)){
				list($name, $file) = each($attachment);
				$phpmailer->AddAttachment($file, $name);
			}else{
				$phpmailer->AddAttachment($attachment);
			}
		} catch ( phpmailerException $e ) {
			continue;
		}
	}
}

Exemple:

$attachments[] = array(
	$_FILES[arquivo][name] => $_FILES[arquivo][tmp_name],
);
Last edited 9 years ago by SergeyBiryukov (previous) (diff)

#6 @stephenharris
8 years ago

  • Keywords has-patch needs-unit-tests needs-codex added

#7 @ragulka
8 years ago

The patch by @rittesh.patel looks solid - the best part is that it does not change the $attachments structure, so it's 100% backwards compatible. Would love to see it in the next release :)

#8 @swissspidy
6 years ago

  • Keywords needs-codex removed
  • Milestone changed from Awaiting Review to Future Release

@swissspidy
6 years ago

Refreshed patch with unit tests

@swissspidy
6 years ago

Alternative patch that allows changing every argument of addAttachment()

#9 @swissspidy
6 years ago

  • Keywords has-unit-tests added; needs-unit-tests removed
  • Milestone changed from Future Release to 5.0

I just uploaded 28407.2.diff as a refreshed patch of the original idea, along with unit tests.

While I was at it, I also created 28407-alternative.diff, which would allow developers to override every argument for an attachment. An example from the unit test:

wp_mail( 'user@example.org', 'Subject', 'Hello World', '', array(
        array(
                'path' => DIR_TESTDATA . '/images/canola.jpg',
                'name' => 'myawesomeimage.jpg',
                'encoding' => '8bit',
                'type' => 'image/jpeg',
                'disposition' => 'attachment',
        ),
        array(
                'path' => DIR_TESTDATA . '/images/waffles.jpg',
                'name' => 'foobar.jpg',
                'encoding' => 'base64',
                'type' => 'image/png',
                'disposition' => 'inline',
        ),
) );

I'm not 100% this is a good idea, since it makes it very easy to mess something up. But it would certainly be more future-proof than using an associative array.

#10 @johnbillion
6 years ago

  • Milestone changed from 5.0 to 5.1

#11 @pento
5 years ago

  • Milestone changed from 5.1 to 5.2

#12 @desrosj
5 years ago

  • Keywords 2nd-opinion needs-testing added

The latest patch needs review/testing, and could benefit from a second opinion.

#13 @desrosj
5 years ago

  • Milestone changed from 5.2 to 5.3

#14 @Grandy
5 years ago

The latest patch looks good and should be implemented ASAP. The idea of using an array with even more options seems amazing and should be considered as future improvement.

This ticket was mentioned in Slack in #core by david.baumwald. View the logs.


5 years ago

#16 @davidbaumwald
5 years ago

  • Keywords dev-feedback added

#17 @garrett-eclipse
5 years ago

Hi @swissspidy this was discussed in bugscrub today on Slack here;
https://wordpress.slack.com/archives/C02RQBWTW/p1568264953282100

With concerns of the alternate providing too much control and potential for error would the .2.diff make sense for 5.3 and look into what extended options are potentially malicious and introduce them in a later enhancement?

#18 @davidbaumwald
5 years ago

  • Milestone changed from 5.3 to Future Release

This ticket still needs a decision, and with 5.3 Beta 1 only a few days away, this is being moved to Future Release.

#19 @ocean90
3 years ago

#52979 was marked as a duplicate.

#20 @jodamo5
20 months ago

Could this ticket get kicked off again? I suggest just going with the original simple suggestion of allowing attachments to be renamed.

#21 @ocean90
20 months ago

#56968 was marked as a duplicate.

#22 @smub
20 months ago

PHPmailer does this, but wp_mail doesn't have support for it. The above patch seems to get the job done. It would be super helpful if we can get this merged.

#23 @johnjamesjacoby
18 months ago

  • Keywords needs-refresh added; 2nd-opinion dev-feedback removed
  • Milestone changed from Future Release to 6.2
  • Owner set to johnjamesjacoby
  • Status changed from new to assigned

Self-assigning for review/test; using 28407.2.diff, for 6.2.

@johnjamesjacoby
18 months ago

Refresh

#24 @johnjamesjacoby
18 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 55030:

Mail: allow custom attachment filenames in wp_mail().

Previous to this change, attachment filenames in outgoing emails could only ever be derived from their paths (passed in as a numerically indexed array of $attachments).

This changeset adds support for passing an associative $attachments array, where the key strings will be used as filenames instead.

Includes 2 new unit tests to ensure both array formats continue to work as intended.

Props johnjamesjacoby, ritteshpatel, swissspidy, syntaxart.
Fixes #28407.

#25 @SergeyBiryukov
18 months ago

In 55032:

Coding Standards: Fix WPCS issues in phpunit/tests/pluggable/wpMail.php.

This addresses a few errors along the lines of:

  • Opening parenthesis of a multi-line function call must be the last content on the line
  • Only one argument is allowed per line in a multi-line function call
  • Each array item in a multi-line array declaration must end in a comma
  • Closing parenthesis of a multi-line function call must be on a line by itself

Follow-up to [55030].

See #28407.

This ticket was mentioned in Slack in #core by hellofromtonya. View the logs.


18 months ago

This ticket was mentioned in PR #3856 on WordPress/wordpress-develop by @mukesh27.


17 months ago
#27

  • Keywords needs-refresh removed

Trac ticket: https://core.trac.wordpress.org/ticket/28407

Props to @costdev as he raised it on Slack channel

@mukesh27 commented on PR #3856:


17 months ago
#28

@SergeyBiryukov @JJJ I would like to get your feedback on this.

I have not reopened that ticket at the moment.

#29 @SergeyBiryukov
17 months ago

In 55087:

Tests: Add unique messages to assertions for attachment filenames in wp_mail().

This makes the assertions more helpful, as per the Writing PHP Tests guidelines:

All PHPUnit assertions, as well as all WordPress custom assertions, allow for a $message parameter to be passed. This message will be displayed when the assertion fails and can help immensely when debugging a test. This parameter should always be used if more than one assertion is used in a test method.

Follow-up to [55030], [55032].

Props mukesh27, costdev.
See #28407.

@SergeyBiryukov commented on PR #3856:


17 months ago
#30

Thanks for the PR! Merged in r55087.

#31 @milana_cap
16 months ago

  • Keywords add-to-field-guide added
Note: See TracTickets for help on using tickets.