PHP Script To Check If An Email Is Valid Without Sending Confirmation - php

I've found a couple online services that do this, and I found this post at stackoverflow about it:
How to check if an email address exists without sending an email?
The problem is that the PHP script linked to there requires you to populate a list of nameservers and domains, and thus (I think) only works if you are validating emails on a known domain. I want something that will work for any email (at least work with a high probability). I found a script that does it that I can buy for $40, but I'd rather find the same thing as open source.
Thanks for any advice,
Jonah

This:
$emailValidation = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")#(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/;
if(preg_match($emailValidation, $testEmail)) {
echo "valid email.";
} else {
echo "invalid email.";
}
Is a well used email validation regex, which is PHP compatible.
Just check email addresses against it and you're done.
But note that without a confirmation postback you will never know that an email is 100% valid.

Related

PHPMailer do not send if email is invalid

I'm using PHPMailer to send emails via contact form.
When I enter an invalid email address in the input field, the email is still sent. I want to perform both client-side and server-side validations.
Does PHPMailer have a built-in validation system to check for invalid email address? If the email entered is invalid I want to return an error and not send the email.
The easiest and most correct way to validate email addresses is to use filter_var. Rather than relying on a patched PHPMailer, you could write a function to validate them before you send them to PHPMailer.
function validateEmailAddr($addr) {
// note: do not attempt to write any regex to validate email addresses;
// it will invariably be wrong
return filter_var($addr, FILTER_VALIDATE_EMAIL);
}
You said that you could enter something like 'sdjfygsdfisdf' and still get the email sent to you.
That's odd. Because adding any email address ('to', 'cc', 'bcc', 'replyto') in PHPMailer will go through the addOrEnqueueAnAddress() function, which does include validation checks. Adding a 'from' address uses different code, but also does validation checks.
The most obvious thing here is that you're not actually doing any error checking to trap for those errors.
Depending on whether you've got PHPMailer using exceptions or not, you might just be getting a false value returned from functions like setFrom() when you give it a bad address. If you ignore that value and carry on anyway, then yes, the email will still be sent.
So you need to add some error handling. Check for function call returning false.
However my preferred suggestion would be to switch to using exceptions for your error handler -- PHPMailer can do this just by setting a flag. This will make error handling easier, as you won't need to check for false on every single function call; just wrap the whole thing in a try catch block, and do your error handling in one go at the end.

MODX not sending emails

I'm using modMail class to send custom emails. I have followed the guidelines on MODX site and used the following code which I placed in a snippet:
$message = $modx->getChunk('myEmailTemplate');
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY,$message);
$modx->mail->set(modMail::MAIL_FROM,'me#example.org');
$modx->mail->set(modMail::MAIL_FROM_NAME,'Johnny Tester');
$modx->mail->set(modMail::MAIL_SUBJECT,'Check out my new email template!');
$modx->mail->address('to','user#example.com');
$modx->mail->address('reply-to','me#xexample.org');
$modx->mail->setHTML(true);
if (!$modx->mail->send()) {
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
}
$modx->mail->reset();
The snippet has been modified to contain message from custom chunk as well as email addresses have been replaced with the correct ones. The snippet sent email once and never again. I have no idea what causes such behavior which prevents it from sending emails.
I have read that using the reset function $modx->mail->reset(); resets email fields and allows the email to be sent again yet I have a feeling that it causes problem here.
The snippet is called uncached on the page [[!email]]
Does anyone have an idea why the emails are not being sent, even though it worked once?
if there is an error in your chunk or in processing your chunk, modx is never going to get to thepoint where it logs an error. try something like:
if (!$modx->mail->send()) {
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
}else{
$modx->log(modX::LOG_LEVEL_ERROR,'This mail was sent: '.$message);
}
to see if it logs something. but otherwise what you have there is exactly correct - try to take the $message variable out and send just a string. if it sent mail once, then something else must be wrong. I'd start looking at mail server logs, headers, spam [gmail??] etc.

Codeigniter Mail not working correctly

I am sending an email using the following code. But it shows up in my email as from "me#gmail.com" also when I press reply on the email it wants to send it to "me#gmail.com"
Don't know if I've done something wrong or?
$this->email->from($this->input->post('email'), $this->input->post('thename'));
$this->email->reply_to($this->input->post('email'), $this->input->post('thename'));
$this->email->to('me#gmail.com');
$this->email->subject('New Feedback');
$this->email->message($this->input->post('message'));
$this->email->send();
I set up a simple email test in CodeIgniter v 2.1.2 as follows:
$this->email->from('malcom#awdoffice.com','Malcom');
$this->email->reply_to('awd#awdoffice.com','AWD');
$this->email->to('marc#awdoffice.com');
$this->email->subject('Subject Mailer-Test');
$this->email->message('Lorem est email.');
$this->email->send();
$rc_email = $this->email->print_debugger();
I suggest that you go through the following debugging procedure.
(1) Hard code all email addresses and names in the from and reply_to setters. Test to see if the problem still exist.
(2) If step (1) fixes the problem, then something may be wrong with your input variables so try printing out the variables (append to email body text).
(3) Print out the text returned from print_debugger
(4) I tested the email in Mozilla Thunderbird and the from, reply_to and to fields worked as expected. What email client are you using? what email server (if testing locally)?
Please keep us posted with your progress.
I figured it out. I had uploaded the site to my "real" server and forgot to change the baseurl of the site.. I still had it as localhost. So whenever I sent an email it was using my localhost and the from email I had setup in phpmailer.
I know quite a silly mistake, but being up for 20+ hours can do that to someone.
Thanks for all the help.

php regex too strict

Just wondering why this is too strict, I can send very simplified emails to say tim#yahoo.com or two#google.com
but if I make the email any longer (sacagawea#gmail.com) it does not get sent.
Instead it echos back my error message:Invalid Email Address Supplied
// Create a function to check email
function checkEmail($email)
{
// Add some regex
return preg_match('/^\S+#[\w\d.-]{2,}\.[\w]{2,6}$/iU', $email) ? TRUE : FALSE;
}
If you have access to php 5.2 or above, you should use the filter functions :
function checkEmail($email){
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}
Or validate it, "the right way".
This part
#[\w\d.-]{2,}
is gobbling up
#gmail.com
leaving nothing for this part
[\w\d.-]{2,}
to match.
Better to reuse something already proven, see for example http://www.regular-expressions.info/email.html
I usually don't fret myself too much for checking email validity. I just need to check there is a value in front of "#" and at the back. That's all. The rest of the "checking" job, the MTAs will do that for me. If its invalid email, i will get a response from MTA. If its able to be sent out, that means the email is most probably valid.
please try
'/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*#[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/'
it also fits for subdomain email adresses as email#sub.domain.tl

Allow user#example or user#localhost in email validation?

I'm working on an email validation check and we need to decided whether to allow user#localhost and user#example (notice no .anything) to be validated as a valid email address. This is for an open source project that has a number of use cases on both the web at large and intranets.
RFC 2822 (Internet Message Format Standard) allows it but RFC 2821 (SMTP Standard) says it should fail.
Thoughts?
It depends on your application. If you think that several of your users will have an email #localhost, and you don't mind. Then go for it.
Make it a configurable option, so people can decide for themselves. I'd default it to failure, personally, as I've yet to run into a case - intranet or public internet - where I've had someone use a valid user#localhost type address.
I would disable it. Very few organizations use internal domains, and those that do generally use "acme.localhost" or "intranet.com" or something else of the like. There is some sort of configuration going on in the DNS that they use to make it work.
Regardless, internal email is nearly dead anyway: with the advent of instant messaging, Twitter, and SMS along with the increasing availability of external email for every member of a company, it is almost entirely likely that you will never get a TLD-less domain in an email.
For the folks that do require it, they can always tweak the regex themselves, as they were savvy enough to set up a custom hostname to handle internal email.
Well, if you have DNS working for internally you could always just do a DNS lookup.
But if this is going to fail with SMTP, then I would suggest making sure you don't include it.
I have seen email addresses of the form user#localhost, typically when looking at archives of a mailing list and the administrator hosted and posted from the same machine. So it can definitely occur - and I admit it broke my parsing routine! So now I am a little more flexible to email addresses.
Looking at this it looks like you've we need two quick checks as detailed:
<?php
function valid_email($email) {
// First, we check that there's one # symbol, and that the lengths are right
if (!ereg("^[^#]{1,64}#[^#]{1,255}$", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of # symbols.
return false;
}
// take a given email address and split it into the username and domain.
list($userName, $mailDomain) = split("#", $email);
if (checkdnsrr($mailDomain, "MX")) {
// this is a valid email domain!
return true;
}
else {
// this email domain doesn't exist!
return false;
}
}
?>
(source 1, source 2)

Categories