#28407 closed enhancement (fixed)
You are unable to override the attachment name in wp_mail() when adding attachments
Reported by: | syntaxart | Owned by: | johnjamesjacoby |
---|---|---|---|
Milestone: | 6.2 | Priority: | normal |
Severity: | normal | Version: | 3.9.1 |
Component: | 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)
Change History (35)
#3
@
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.
#5
in reply to:
↑ 4
@
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], );
#7
@
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
@
7 years ago
- Keywords needs-codex removed
- Milestone changed from Awaiting Review to Future Release
#9
@
7 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.
#12
@
6 years ago
- Keywords 2nd-opinion needs-testing added
The latest patch needs review/testing, and could benefit from a second opinion.
#14
@
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
#17
@
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
@
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
.
#20
@
2 years ago
Could this ticket get kicked off again? I suggest just going with the original simple suggestion of allowing attachments to be renamed.
#22
@
23 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
@
21 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.
This ticket was mentioned in Slack in #core by hellofromtonya. View the logs.
21 months ago
This ticket was mentioned in PR #3856 on WordPress/wordpress-develop by @mukesh27.
21 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:
21 months ago
#28
@SergeyBiryukov @JJJ I would like to get your feedback on this.
I have not reopened that ticket at the moment.
@SergeyBiryukov commented on PR #3856:
21 months ago
#30
Thanks for the PR! Merged in r55087.
You need to check that whether it is associative array or sequential array.