WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 18 months ago

#28407 new enhancement

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

Reported by: syntaxart Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.9.1
Component: Mail Keywords: has-patch needs-unit-tests needs-codex
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 (1)

28407.diff (912 bytes) - added by rittesh.patel 3 years ago.
Check for whether it is associative array or sequential array for attachments

Download all attachments as: .zip

Change History (8)

#1 @rittesh.patel
3 years ago

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

@rittesh.patel
3 years ago

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

#2 @syntaxart
3 years ago

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

#3 @syntaxart
3 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
3 years ago

#28782 was marked as a duplicate.

#5 in reply to: ↑ 4 @p_j_albuquerque
3 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 3 years ago by SergeyBiryukov (previous) (diff)

#6 @stephenharris
20 months ago

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

#7 @ragulka
18 months 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 :)

Note: See TracTickets for help on using tickets.