Note that there are some explanatory texts on larger screens.

plurals
  1. POSend email from iOS app using SendGrid
    primarykey
    data
    text
    <p>I am trying to to use mail api from <a href="http://sendgrid.com/docs/API_Reference/Web_API/mail.html" rel="nofollow">sendgrid.com</a> but everytime it finishes with failure block. Also I don't understand how to send the image as an attachment in the email. Can anybody tell me whats wrong in below code &amp; how can I send image ? I am using below code for now</p> <pre><code>-(void)sendEmail { NSMutableDictionary *params = [[NSMutableDictionary alloc]init]; [params setValue:@"username" forKey:@"api_user"]; [params setValue:@"sdsfddf23423" forKey:@"api_key"]; [params setValue:@"test@gmail.com" forKey:@"to"]; [params setValue:@"test user" forKey:@"toname"]; [params setValue:@"Test SendGrid" forKey:@"subject"]; [params setValue:@"Test SendGrid from iOS app" forKey:@"text"]; [params setValue:@"noreply@gmail.com" forKey:@"from"]; NSURL *url = [NSURL URLWithString:@"https://sendgrid.com/api"]; AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL: url]; NSMutableURLRequest *request = [client requestWithMethod:POST path:@"/mail.send.json" parameters:params]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil]; DLog(@"Get latest product info response : %@", response); NSLog(@"Success"); } failure: ^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Failure"); }]; [operation start]; } </code></pre> <p>Thanks in advance.</p> <p>Update </p> <p>I made some changes in code &amp; now I can send the email successfully as below</p> <pre><code>-(void)sendEmailWithoutImage { NSDictionary *parameters = @{@"api_user": @"username", @"api_key": @"sdsfddf23423", @"subject":@"Test SendGrid", @"from":@"noreply@gmail.com", @"to":@"test@gmail.com", @"text":@"Test SendGrid from iOS app"}; [[MyAPIClient sharedAPIClient] POST:@"mail.send.json" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) { NSLog(@"Success::responseObject : %@", responseObject); } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"Error::Mail response : %@", error); }]; } </code></pre> <p>But when I try to send the image as attachment then it result in 400 bad request. So I think there is some error in my file uploading block. Here is my code</p> <pre><code>-(void)sendEmailWithImage { NSDictionary *parameters = @{@"api_user": @"username", @"api_key": @"sdsfddf23423", @"subject":@"Test SendGrid", @"from":@"noreply@gmail.com", @"to":@"test@gmail.com", @"text":@"Test SendGrid from iOS app"}; [[MyAPIClient sharedAPIClient] POST:@"mail.send.json" parameters:parameters constructingBodyWithBlock:^(id&lt;AFMultipartFormData&gt; formData) { UIImage *image = [UIImage imageNamed:@"redWine.png"]; NSData *imageToUpload = UIImagePNGRepresentation(image); [formData appendPartWithFileData:imageToUpload name:@"files" fileName:[NSString stringWithFormat:@"%@",@"abc.png"] mimeType:@"image/png"]; } success:^(NSURLSessionDataTask *task, id responseObject) { NSLog(@"Success::responseObject : %@", responseObject); } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"Error::Mail response : %@", error); }]; } </code></pre> <p>Can you anybody tell me whats going wrong while uploading the image ?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload