通过sendgrid通过电子邮件捕获图像

时间:2016-03-21 21:51:48

标签: swift image email sendgrid

所以我尝试使用SendGrid将捕获的图像作为附件发送给电子邮件。我为电子邮件定义了sendmail函数和attachImage方法。

public extension SendGrid {
    public class Email {
    ......
        public var images: [UIImage]!

        public func attachImage(image: UIImage) {
            if self.images == nil {
                self.images = []
            }
            self.images.append(image)
        }
    }
}

然后调用此函数

var sg = SendGrid(username: "*****", password: "*****")
var email = SendGrid.Email()

func sendMail(){
    screenShot()

    do {
        try
        email.addTo("sean@gmail.com", name: "Sehwan")
        email.setFrom("test@test.com", name: "test")
        email.setSubject("Hello Images")
        email.setHtmlBody("<p>Up Thai, Wolfgang, Benihana, Wa.</p><br><br><p>Nice it works</p>")
        email.attachImage(shareImage)
    } catch {
        print(error)
    }

    if(shareImage != nil){
        do {
            try
            sg.send(email, completionHandler: { (response, data, error) -> Void in
                if let json = NSString(data: data!, encoding: NSUTF8StringEncoding) {
                    print(json)
                }
            })
        } catch {
            print(error)
        }
    }
}

func screenShot(){
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(backgroundImage.frame.size.width, backgroundImage.frame.size.width), false, 0)
    self.view?.drawViewHierarchyInRect(CGRectMake(0, -40, view.bounds.size.width, view.bounds.size.height), afterScreenUpdates: true)
    shareImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
}

一切运作良好 - 截图功能捕获屏幕的一部分,以便我可以共享或保存到手机。除了电子邮件没有图像文件之外,还可以通过电

我不确定如何使用sendGrid将应用程序中捕获的图像作为附件发送。我非常感谢任何建议。

1 个答案:

答案 0 :(得分:0)

他们的API reference描述了如何做到这一点。

相关部分:

  

文件必须小于7MB要附加的文件。

     

文件内容必须是多部分HTTP POST的一部分。

     

Ex:files [file1.jpg] = file1.jpg&amp; files [file2.pdf] = file2.pdf

相关问题