Zend Framework hostname Email Validator fails on yahoo.gr - php

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.

Related

Getting Plesk Mailbox information with PHP?

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.

Debugging SMTP email failures - GSuite

I am working on an eCommerce site that sends a number of emails to the customer when they complete their order using G Suite SMTP relay service. But a large number of these emails are failing. There does not seems to be any pattern to it either - sometimes all emails will send, some times just one or two, and sometimes none.
I am getting the following error: 421, "4.7.0", Try again later, closing connection.
Looking here: https://support.google.com/a/answer/3726730?hl=en doesn't really help me debug this or figure out why some emails fail.
I am using the phpmailer class (https://sourceforge.net/projects/phpmailer/)
The issue seems to occur when the first handshake fails:
function Hello($host="") {
$this->error = null; # so no confusion is caused
if(!$this->connected()) {
$this->error = array(
"error" => "Called Hello() without being connected");
return false;
}
# if a hostname for the HELO was not specified determine
# a suitable one to send
if(empty($host)) {
# we need to determine some sort of appopiate default
# to send to the server
$host = "localhost";
}
// Send extended hello first (RFC 2821)
//If this fails then the second attempt will always fail
if(!$this->SendHello("EHLO", $host))
{
//when this fails it generates the try again later error
if(!$this->SendHello("HELO", $host))
return false;
}
return true;
}
So what is the best approach for debugging this?
The error message is pretty explicit. You call a 3rd party web service, which returns an error code which says the server you are calling is at capacity, try later. Is this a free service which allows you to upgrade to a paid plan? Usually whee you see his kind of thing.

piwik send email doesn't work?

my application use piwik for statistics. when I config the mail section. something wrong happen. the config is like this:
[mail]
transport = "smtp"
port = "587"
host = "smtp.qq.com"
type = "Login"
username = "username#qq.com"
password = "*********"
encryption = "tls"
and when transfer a mail the error information is like this:
An error occurred while sending 'PDF Email Report -
1.2016-11-15.2.zh-cn.pdf' to *******#qq.com. Error was 'mail from address must be same as authorization user'
and I google with this info , but nothing I found work fine.
I guess there was somewhere I can put the address from as I do before. But I don't find the place.
Could someone tell something about this question, thanks a lot.
I have handle this question. The solution is add a line in config/config.ini.php [General] code section.
noreply_email_address = username#qq.com
I don't know how it work. but obviously, it work.
Hope this solution can help someone !

PHP. How to check if a e-mail domain exists?

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).

Verifying POST data sent from same server WITHOUT using a token using PHP?

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';

Categories