简体   繁体   中英

API response accessing names of From and To mail

I'm able to fetch messages form gmail api. In the response i'm getting From and To mails as like below

在此处输入图片说明 .

{
    "name": "From",
    "value": Mail Delivery Subsystem **Symbol lessthan**mailer-daemon@googlemail.com**Symbol greaterthan**
}

{
    "name": "To",
    "value": Rahul, kumar **Symbol greaterthan**rahulkumar1234@gmail.com**Symbol lessthan**
}

I can able to access value as like below by looping.

 if element.name == "From" {
   print("From ::: \(String(describing: element.value!))")
 }

OutPut is :

From ::: Mail Delivery Subsystem <mailer-daemon@googlemail.com>

Instead of sender name and mail. I want to get only mail and sender name individullay. how can i devide the value into two parts/strings one for name and another for mail.

Some times there is no sender name as well as lessthan and greaterthan symbols, in that time i'm getting only mail.

@vadian I've specified below of all the formats in the response. sometimes i'm not getting lessthan and greater than. And also i want to save name and email as a two seperate strings. if there is no name in the response then there is no need to grab it as a string only mail is enough to grab as a string. 在此处输入图片说明

2 options i can think of,

1st: Quick and Dirty

Split the string using the '<' operator, get the second part, which will be bit after '<' then remove the '>' from that result to clean it up. This could be dangerous if it's possible for a user to have a '<' or '>' in their name so wouldn't rely on this approach.

2nd: RegEx

Use regular expressions to grab the email from the value

var value = "Some Guy <email@example.com>"

// no idea how robust this pattern is I just grabbed it from google so feel free to change
var pattern = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
let regex = try NSRegularExpression(pattern: pattern)
let results = regex.matches(in: value, range: NSRange(value.startIndex..., in: value))

// Generates a string array of emails
var emails = results.map {
    String(value[Range($0.range, in: value)!])
}

you'll probably only ever have one email in the From field but this will allow you to grab multiple emails if there are in the To field.

First of all \\(String(describing: element.value!) is horrible. element.value! is supposed to be a string anyway and wrapping in String(describing: and again in String Interpolation is twice redundant.

Don't abuse String(describing for types which conform to ExpressibleByStringLiteral anyway.

Just write print("From ::: ", element.value!)


This function returns a tuple for name and email . It checks if there is a < character.

If there is none, it returns an empty string as name and the input string as email .
If there is one, it returns the substring after < until second last character as email and the rest trimmed by double quote , space and < as name .

func extractNameAndMail(in string: String) -> (name:String, email:String) {
    if let rangeOfLessThan = string.range(of: "<") {
        let email = string[rangeOfLessThan.upperBound...].dropLast()
        let name = string[...rangeOfLessThan.lowerBound].trimmingCharacters(in: CharacterSet(charactersIn: "\" <"))
        return (name: String(name), email: String(email))
    } else {
        return (name:"", email:string)
    }
}

let string = "\"Some Guy\" <mailer-daemon@googlemail.com>"
let extractedString = extractNameAndMail(in: string)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM