新答案:
要将文件作为电子邮件附件直接上传到SendGrid,您应该使用Web API v3并按照docs中所述创建请求.
首先,你需要add authentication header你的要求.
其次,您需要将数据转换为JSON格式.如果我们在谈论文件,则需要使用Base64对文件数据进行编码,如body parameters section所述:
JSON PARAMETER: attachements/content
TYPE: string
REQUIRED: Yes
The Base64 encoded content of the attachment.
另外,看看disposition和content_id参数:它们将帮助您以邮件方式设置文件外观.
老回答:
上传两个参数和文件的标准方式是使用multipart messages的POST请求.我已经更改了代码来创建这种格式的数据:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage* image = info[UIImagePickerControllerEditedImage];
NSDictionary* params = @{
@"toEmailAddress" : @"blabla@blabla.com",
@"subject" : @"Some Subject",
@"message" : @"Some message...",
@"fullName" : @"Mister Bla Bla",
};
[picker dismissViewControllerAnimated:YES completion:^{
[self sendMailWithParams:params image:image];
}];
}
static NSStringEncoding const kEncoding = NSUTF8StringEncoding;
- (void)sendMailWithParams:(NSDictionary*)params image:(UIImage*)image {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
request.URL = [NSURL URLWithString:@"http://www.someURL.com/sendgrid.php"];
request.HTTPMethod = @"POST";
NSString *boundary = [NSUUID UUID].UUIDString;
// define POST request as multipart
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
// prepare boundary
NSString *middleBoundary = [NSString stringWithFormat:@"--%@\r\n", boundary];
NSData *middleBoundaryData = [middleBoundary dataUsingEncoding:kEncoding];
NSMutableData* body = [NSMutableData data];
// append params
[params enumerateKeysAndObjectsUsingBlock:^(NSString* key, NSString* value, BOOL* stop) {
[body appendData:middleBoundaryData];
NSData* fieldData = [self dataForKey:key value:value];
[body appendData:fieldData];
}];
// append image
if (image) {
[body appendData:middleBoundaryData];
NSData* imageData = [self dataForImage:image imageName:@"photo.png"];
[body appendData:imageData];
}
// add last boundary
NSString* lastBoundary = [NSString stringWithFormat:@"--%@--\r\n", boundary];
NSData* lastBoundaryData = [lastBoundary dataUsingEncoding:kEncoding];
[body appendData:lastBoundaryData];
// set body to request
request.HTTPBody = body;
// add length of body
NSString *length = [NSString stringWithFormat:@"%llu", (uint64_t)body.length];
[request setValue:length forHTTPHeaderField:@"Content-Length"];
// send request
NSURLSession* session = [NSURLSession sharedSession];
NSURLSessionDataTask* task = [session dataTaskWithRequest:request
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
// handle as you want
}];
[task resume];
}
- (NSData*)dataForImage:(UIImage*)image imageName:(NSString*)imageName {
NSString* fieldDescription = [NSString stringWithFormat:
@"Content-Disposition: form-data; name=\"image\"; filename=\"%@\"\r\n"
@"Content-Type: image/png\r\n\r\n", imageName];
NSMutableData *data = [NSMutableData data];
[data appendData:[fieldDescription dataUsingEncoding:kEncoding]];
NSData* imageData = UIImagePNGRepresentation(image);
[data appendData:imageData];
NSString* newLine = @"\r\n";
NSData* newLineData = [newLine dataUsingEncoding:kEncoding];
[data appendData:newLineData];
return data;
}
- (NSData*)dataForKey:(NSString*)key value:(NSString*)value {
NSString* fieldDescription = [NSString stringWithFormat:
@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n"
@"%@\r\n", key, value];
return [fieldDescription dataUsingEncoding:kEncoding];
}
您可以使用$_POST和$_FILES变量来访问PHP脚本中加载的数据.
如果您想了解有关多部分消息的更多信息,请查看文档here.