I'd like to know how can I check if I have a valid e-mail, including the domain.
For example,
e-mails like:
email1#gmail.com, email2#hotmail.com should return true,
but e-mails like:
email3#gmal.com, email4#hotmai.com should return false.
How can we do this?
I can check if a e-mail is a valid one with filter_var($email, FILTER_VALIDATE_EMAIL), but this don't guarantee that we can send the e-mail (email3#gmal.com is a example). How can we guarantee that we have a valid domain?
You could create a whitelist of acceptable domains but that would no doubt leave out many valid services.
You could try to connect to the domain on the appropriate port after doing an MX dig but what if the service is down for an hour for maintenance?
The best way to determine if an email is valid is by sending a verification code and have the user click on/enter the code after receiving the email.
You could use CURL to check if domain exist, this option is very simple but works very well. Is fast to implement.
Check this response for well tested solution:
- https://stackoverflow.com/a/12547629/1146492
To get domain email you can use something like this:
<?php
function getDomainFromEmail($email)
{
// Get the data after the # sign
$domain = substr(strrchr($email, "#"), 1);
return $domain;
}
// Example
$email = 'the_username_here#yahoo.com';
$domain = getDomainFromEmail($email);
echo $domain; // yahoo.com
?>
And than use CURL for fast check.
One possible solution is to attempt to connect to the domain and see if it's successful or not:
if(fopen("http://$domain"))
{
echo "success, valid domain";
} else {
echo "failure, not valid domain";
}
Where $domain is the portion of the email address following the # symbol. (This can be obtained using regular expressions).
Related
I am using zend framework to send emails. I have an Hostname::ALLOW_DNS validator. It fails when trying to send email to yahoo.gr. I get this error:
An error occurred
An error occurred during execution; please try again later.
Additional information:
Zend\Mail\Exception\InvalidArgumentException
File:
/var/www/file/project/vendor/zendframework/zendframework/library/Zend/Mail/Address.php:41
Message:
'yahoo.gr ' is not a valid hostname for the email address
The email is smth#yahoo.gr. Any suggestions?
From your error, the only problem I can see is the whitespace 'yahoo.gr ' which will/can cause failure of validation.
Fix Suggestion 1:
You should start using trim() on your GET/POST "email" value.
Fix Suggestion 2:
Which is by the way in the documentation:
Validating only the local part
If you need Zend\Validator\EmailAddress to check only the local part of an email address, and want to disable validation of the hostname, you can set the domain option to FALSE. This forces Zend\Validator\EmailAddress not to validate the hostname part of the email address.
$validator = new Zend\Validator\EmailAddress();
$validator->setOptions(array('domain' => FALSE));
Fix Suggestion 3:
Which is by the way in the documentation as well:
Validating different types of hostnames
The hostname part of an email address is validated against Zend\Validator\Hostname. By default only DNS hostnames of the form domain.com are accepted, though if you wish you can accept IP addresses and Local hostnames too.
To do this you need to instantiate Zend\Validator\EmailAddress passing a parameter to indicate the type of hostnames you want to accept. More details are included in Zend\Validator\Hostname, though an example of how to accept both DNS and Local hostnames appears below:
$validator = new Zend\Validator\EmailAddress(
Zend\Validator\Hostname::ALLOW_DNS |
Zend\Validator\Hostname::ALLOW_LOCAL);
if ($validator->isValid($email)) {
// email appears to be valid
} else {
// email is invalid; print the reasons
foreach ($validator->getMessages() as $message) {
echo "$message\n";
}
}
Well, no more suggestions. I wish you good luck!
The End.
I have set some mail quota's on email addresses that I manage through Plesk.
However, users are complaining that they are not being informed when their inbox is full or nearly full.
So my idea was to send an email to them when their inbox is about 90% full, so I was wondering if I can retrieve mail account information using PHP?
If not, is there another way of doing this? (I'm not too familiar with console commands)
In this answer I skip the Plesk API altogether, and I assume you either store the properties of the mailboxes in a database, hardcode it, or actually use the Plesk API to retrieve it.
Here is how I retrieve the space used by a mailbox:
function getSpaceUsedByMailBox($username,$password)
{
// open mailbox
$mailBox = imap_open('{localhost:110/pop3/novalidate-cert}INBOX',$username,$password);
// test if successful
$errors = imap_errors();
if ($errors === FALSE)
{
// get info
$info = imap_mailboxmsginfo($mailBox);
// give feedback
echo "Mailbox of $username contains ".$info->Nmsgs.
' messages and is '.$info->Size.' bytes big.';
// flush notices
imap_errors();
imap_alerts();
// close mailbox
imap_close($mailBox);
// return info
return $info;
}
// change this to proper error handling
echo 'ERROR: '.print_r($errors);
// return nothing
return NULL;
}
This is just to give you an idea. You have to adapt it to your coding style.
I have a form to get some urls from users.
Eg: web address, facebook address, twitter address etc. Then I need to check user may or may not enter the protocol as part of the address. If protocol is not with submitted url I need to add protocol to it.
In this case it is http://. As well as sometimes users may type the protocol incorrectly.
Eg. htttp:/, http//, htp:// etc..
With this issue I need to know is there any way to remove the protocol completely which users has entered and add new protocol with that URL to insert to the database.
I wrote some code to detect submitted URL has a protocol.. Its working little I am expecting but not 100%.
$url = 'www.example.com';
$checkProtocol = strpos($url, '://');
if (false === $checkProtocol ) {
$url = 'http://' . $url;
echo 'This is new URL : ' . $url;
} else {
echo 'Invalid';
}
Just assume $url have htp://www.example.com its become an invalid url.. not assigning the protocol.
Hope someone help me out.
Thank You.
If you want to handle all cases, you might just want to assume that the URL is missing the http:// or has it malformed and just add it in all cases after cleaning the string:
$url = 'htp://www.example.com';
$fixed_url = 'http://' . preg_replace('#^.*://#', '', $url);
Hello i am using this function to get IP Address of different systems..but everytime it returns the same value: 117.239.82.182
function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
EDIT: (The answer changed radically after some clarifications in the comments)
You could edit the User-Agent setting of the user's browsers. To see how to change the setting in various browsers follow this link. Then you should modify your PHP script to read User-Agent of the browser.
In PHP,$_SERVER['HTTP_USER_AGENT'] returns the browser's User-Agent setting. Eg. you can define as User-Agent something like Company/System/1.02 Bla bla bla. Then when you receive that same string you can assume it is coming from a known host.
Attention that the User-Agent can be easily spoofed. So this method is not secure. The secure solution would be to implement a VPN solution.
117.239.82.182 is an external IP address. If all the systems that connect to the PHP server are behind the same external IP address, all of them will be notet as the same IP address.
Your script doesn't take the local IP. Don't think it's even possible. The IP you are seeing, is the IP of the firewall of your company.
For reasons beyond the scope of this post, I would like to verify in PHP if post data was sent from the same server, but I would like to avoid using a token. If completely necessary, I can use one, but I would be very helpful if I didn't have to.
Is this possible? If not, why?
Thanks!
Yes, you can check the remote address for the IP address of the request sender using $_SERVER['REMOTE_ADDR']. Do it like this:
if( $_SERVER['REMOTE_ADDR'] == $your_Server_IP_Address)
echo 'From same server';
else
echo 'from different server';