Question

I am trying to to use mail api from sendgrid.com 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 & how can I send image ? I am using below code for now

-(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];
}

Thanks in advance.

Update

I made some changes in code & now I can send the email successfully as below

-(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);
    }];
}

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

-(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<AFMultipartFormData> 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);
    }];
} 

Can you anybody tell me whats going wrong while uploading the image ?

Thanks

Was it helpful?

Solution

Just modified your code a little. It looks like there was an issue with the parameters being sent and the URL path.

Also since you are already using AFNetworking to make your POST request, you can follow their docs and example on how to send a photo over here: http://cocoadocs.org/docsets/AFNetworking/2.0.1/

NSDictionary *parameters = @{@"api_user": @"username",
                                 @"api_key": @"sdsfddf23423",
                                 @"Test SendGrid":@"test",
                                 @"from":@"noreply@gmail.com",
                                 @"to":@"test@gmail.com",
                                 @"text":@"Test SendGrid from iOS app"};


    NSURL *url = [NSURL URLWithString:@"https://sendgrid.com/api/"];
    AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL: url];
    NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"mail.send.json"  parameters:parameters];
    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: %@", response);
     } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"%@",error);
     }];
    [operation start];

Update**

Created a Sendgrid-ios library to make it easier to send an email and photo attachment.

//create Email Object
gridmail *msg = [gridmail user:@"username" andPass:@"password"];

//set parameters
msg.to = @"foo@bar.com";
msg.subject = @"subject goes here";
msg.from = @"me@bar.com";
msg.text = @"hello world";   
msg.html = @"<h1>hello world!</h1>";


//Image attachment
[msg attachImage:self.photo];

//Send email through Web API Transport
[msg sendWithWeb];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top