0

I'm writing a C project that needs to validate the emails before they are stored. I will have to cross compile, so using regex.h is not an option.

Is there a surefire way to validate an email by checking if it is a deliverable address without actually having to send an email in C?

Alternatives or suggestions are welcome if there is no way of doing this.

RI98
  • 57
  • 8
  • See https://stackoverflow.com/a/566121/337149 – August Karlstrom Mar 05 '21 at 18:40
  • regex isnt going to check if its deliverable. – lostbard Mar 05 '21 at 18:41
  • 3
    There's a difference between checking that a string has valid syntax to be an email address, and checking whether an email actually exists. Which one do you want to do? If the latter, what happens if you store the email and then it gets deleted? – Kevin Mar 05 '21 at 18:42
  • For Europe, your project is related to https://gdpr-info.eu/ - there are legal issues to take into account – Basile Starynkevitch Mar 05 '21 at 18:58
  • 2
    Don't pretend you can write an algorithm that can correctly distinguish between valid and invalid e-mail syntax. You can't. Not unless you are an expert in all the relevant RFCs. The regexp to verify the syntax of an e-mail address is a few pages long, and totally cryptic. Even getting a DNS name is not as easy as splitting the string at `@`. – HAL9000 Mar 05 '21 at 18:59
  • @Kevin I can't do regex which is why I want to do this. And hence the question. – RI98 Mar 05 '21 at 19:04
  • @Basile Starynkevitch not storing them. just verifying them now, thank you for the mention :) – RI98 Mar 05 '21 at 19:04

2 Answers2

2

You can perform a DNS query of the domain name (the part of the e-mail-address after the @ symbol) to determine whether the domain is valid and has an MX record. To verify the username (the part of the e-mail-address before the @ symbol), you will have to query the destination mail server itself, for example using the VRFY SMTP command. However, for security reasons, some mail servers may not support this command.

The C language itself does not provide any functions with which you can send network packets. However, most platforms provide an API which offers this functionality (e.g. POSIX sockets, Windows sockets). Some platforms also provide an API which performs the DNS query for you. Also, several libraries exist which provide this functionality.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • There is no requirement that an e-mail address involves a MX record. Theoretically, it doesn't even have to involve a DNS domain. Trying to split a string at "the `@`" to get a username/domain pair is naive at best, and a security issue at worst. Sending the username unverified and unquoted to an smtp-server, may trick you into doing other operations on that server. The parents of Little Bobby Tables sends you their regards. – HAL9000 Mar 05 '21 at 19:13
0

There is no such procedure in any language.

ddyer
  • 1,792
  • 19
  • 26
  • Any alternatives? suggestions?? – RI98 Mar 05 '21 at 18:36
  • The standard procedure is to send an email with a verification link. Even that only tells you that the previous email worked, not that the next one will. Another obvious problem if you only verify that an email is valid, is that you don't know that the email is valid for that user. info@google.com is probably valid, but I can't receive email there. – ddyer Mar 06 '21 at 05:27