-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed #34: Added support for attachments #35
base: master
Are you sure you want to change the base?
Conversation
faishal
commented
Feb 23, 2018
- Created multipart email for mails with attachments.
- Added param in wp-cli commad.
- Updated readme with example command.
Signed-off-by: Faishal Saiyed <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd also want review from @joehoyle on this.
inc/class-ses.php
Outdated
* | ||
* @return string | ||
*/ | ||
private function get_raw_message( $to, $subject, $message, $headers = array(), $attachments = array() ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be protected
rather than private
inc/class-ses.php
Outdated
$reply_to = array_merge( (array) $reply_to, explode( ',', $content ) ); | ||
break; | ||
default: | ||
$raw_message_header .= $name . ': ' . $content . "\n"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should $content
be newline-escaped to avoid header overflow issues?
inc/class-ses.php
Outdated
$raw_message .= 'MIME-Version: 1.0' . "\n"; | ||
$raw_message .= 'Content-type: Multipart/Mixed; boundary="' . $boundary . '"' . "\n"; | ||
$raw_message .= "\n--{$boundary}\n"; | ||
$raw_message .= 'Content-type: Multipart/Alternative; boundary="alt-' . $boundary . '"' . "\n"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these should be Content-Type
with the actual type lowercase?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked into RFC for that, In starting it showing Content-Type
but in the example, it was showing Content-type
, so I tested both and working fine.
Ref: https://www.w3.org/Protocols/rfc1341/rfc1341.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, headers are theoretically case-insensitive (likewise in HTTP), but in practice, it's best to use the exact case.
inc/class-ses.php
Outdated
$custom_from = sprintf( '%s <%s>', apply_filters( 'wp_mail_from_name', get_bloginfo( 'name' ) ), apply_filters( 'wp_mail_from', $from_email ) ); | ||
} | ||
|
||
$boundary = uniqid( rand(), true ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wp_rand()
would be better than uniqid( rand() )
I think, and I'd probably add a prefix as well:
$boundary = 'aws-ses-wp-mail-' . wp_rand();
inc/class-ses.php
Outdated
|
||
$raw_message .= 'MIME-Version: 1.0' . "\n"; | ||
$raw_message .= 'Content-type: Multipart/Mixed; boundary="' . $boundary . '"' . "\n"; | ||
$raw_message .= "\n--{$boundary}\n"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd probably use sprintf()
for these instead, I think it's a bit more readable.
inc/class-ses.php
Outdated
$raw_message .= "\n--alt-{$boundary}--\n"; | ||
|
||
foreach ( $attachments as $attachment ) { | ||
if ( ! @is_file( $attachment ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a better way to check this and avoid the error suppression operator?
inc/class-ses.php
Outdated
} | ||
|
||
$raw_message .= "\n--{$boundary}\n"; | ||
$raw_message .= 'Content-Type: ' . $file_type['type'] . '; name="' . $filename . '"' . "\n"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd use sprintf()
for this.
Signed-off-by: Faishal Saiyed <[email protected]>
I thing the biggest problem here is that working with attachments means different filters and way of sending the message, which is going to be annoying to work with. As a developer I can't rely on We could also consider only ever sending raw, so there's only one method to deal with. I don't see much value in keeping around SendMessage if we have to have a fully reliable SendRawMessage implementation. Having a branch is undoubtedly going to cause some bugs. However, SendMessage is presumably easier to implement and less error prone, so we need to weigh up the benefit of that versus the pretty rarely used sending of attachments. If we push ahead I think |
@joehoyle That makes sense, After reading your comment I dig into what we can do to make it simple. How about using I looked into the We can also create a custom class to do that but I feel like phpMailer is providing all encoding supports. |
Interesting, I think using PHPMailer might be handy. In that case, do we even need to hook |
Do you mean removing In that case it will not require to build the mail, we just have to send it via aws ses api.
If we don't want to repeat what
|
Cool yeah that's what I was getting at. I think this approach is certainly interesting. I'm not 100% whether to proceed - what would be your suggested route? |
@joehoyle We can set milestone |
} | ||
$raw_message .= sprintf( "\n--alt-%s--\n", $boundary ); | ||
|
||
foreach ( $attachments as $attachment ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@faishal - wp_mail
$attachments
param also accepts a single string file location as a valid param. Currently, passing a single string results in attachments not being sent here.
We could follow cores approach and cast as follows (before entering the loop here)
if ( ! is_array( $attachments ) ) {
$attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );
}
Is this already in use? I noticed we have a 2.0.0-alpha tag with this feature. |