Email verification to know email really exists on server using php - php

i am trying to make a function where i can check if a email really exists or not i searched various forums but the code only works fine with gmail not others i am checking mx records can i do something to connect to those mx records and ask server if email really does exists or not.
I have added much of the code so that you can understand what do i need to do
Thank's
private function verifyEmail($toemail, $fromemail, $getdetails = false)
{
$result='';
$details=' ';
// Get the domain of the email recipient
$email_arr = explode('#', $toemail);
$domain = array_slice($email_arr, -1);
$domain = $domain[0];
// Trim [ and ] from beginning and end of domain string, respectively
$domain = ltrim($domain, '[');
$domain = rtrim($domain, ']');
if ('IPv6:' == substr($domain, 0, strlen('IPv6:'))) {
$domain = substr($domain, strlen('IPv6') + 1);
}
$mxhosts = array();
// Check if the domain has an IP address assigned to it
if (filter_var($domain, FILTER_VALIDATE_IP)) {
$mx_ip = $domain;
} else {
// If no IP assigned, get the MX records for the host name
getmxrr($domain, $mxhosts, $mxweight);
}
if (!empty($mxhosts)) {
$mx_ip = $mxhosts[array_search(min($mxweight), $mxhosts)];
} else {
// If MX records not found, get the A DNS records for the host
if (filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$record_a = dns_get_record($domain, DNS_A);
// else get the AAAA IPv6 address record
} elseif (filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
$record_a = dns_get_record($domain, DNS_AAAA);
}
if (!empty($record_a)) {
$mx_ip = $record_a[0]['ip'];
} else {
// Exit the program if no MX records are found for the domain host
$result = 'Invalid Email MX ';
$details .= 'No suitable MX records found.';
return ((true == $getdetails) ? array($result, $details) : $result);
}
}
// Open a socket connection with the hostname, smtp port 25
$connect = #fsockopen($mx_ip, 25);
if ($connect) {
// Initiate the Mail Sending SMTP transaction
if (preg_match('/^220/i', $out = fgets($connect, 1024))) {
// Send the HELO command to the SMTP server
fputs($connect, "HELO $mx_ip\r\n");
$out = fgets($connect, 1024);
$details .= $out."\n";
// Send an SMTP Mail command from the sender's email address
fputs($connect, "MAIL FROM: <$fromemail>\r\n");
$from = fgets($connect, 1024);
$details .= $from."\n";
// Send the SCPT command with the recepient's email address
fputs($connect, "RCPT TO: <$toemail>\r\n");
$to = fgets($connect, 1024);
$details .= $to."\n";
// Close the socket connection with QUIT command to the SMTP server
fputs($connect, 'QUIT');
fclose($connect);
// The expected response is 250 if the email is valid
if (!preg_match('/^250/i', $from) || !preg_match('/^250/i', $to)) {
$result = 'Invalid Email Address';
} else {
$result = 'Valid Email Address';
}
}else{echo 'layht';}
} else {
$result = 'Invalid Email Address';
$details .= 'Could not connect to server';
}
if ($getdetails) {
return array($result, $details);
} else {
return $result;
}
}

The only way to know for certain if an email address exists is to send mail to it and get a response from the user.
In the past, it was possible to query the remote server using SMTP and ask it if the address was valid; however because spammers used this to harvest addresses, it no longer works.
In the past it was also possible to send mail to a server and if it wasn't rejected, assume that it was a valid address, however this is no longer reliable either.

Related

PHP IMAP : How to get the correct body?

I am connecting to my inbox via PHP IMAP plugin. Below are my steps
Connection
//The location of the mailbox.
$hostname = '{outlook.office365.com:993/imap/ssl/novalidate-cert}';
//The username / email address that we want to login to.
$username = 'username';
//The password for this email address.
$password = 'password';
I am further opening the inbox connection and looking at anything that is UNSEEN in the inbox
//Attempt to connect using the imap_open function.
$inbox = imap_open($hostname,$username,$password);
$mailboxes = imap_list($inbox, $hostname, '*');
imap_reopen($inbox, $hostname.'INBOX');
$emails = imap_search($inbox, 'UNSEEN');
Further, I am iterating over all the emails and its working well with headers. The issue I am having is with body.
To get the body of the email I am using
//get message body
$message = quoted_printable_decode(imap_fetchbody($inbox, $email_number, 2.1));
if ($message == '') {
$message = quoted_printable_decode(imap_fetchbody($inbox, $email_number, 1));
}
So running a test on 40 emails, 28 emails fetch the body correct and store into the database.
12 emails fetch a body which looks like
PGh0bWwgeG1sbnM6dj0idXJuOnNjaGVtYXMtbWljcm9zb2Z0LWNvbTp2bWwiIHhtbG5zOm89InVy
bjpzY2hlbWFzLW1pY3Jvc29mdC1jb206b2ZmaWNlOm9mZmljZSIgeG1sbnM6dz0idXJuOnNjaGVt
YXMtbWljcm9zb2Z0LWNvbTpvZmZpY2U6d29yZCIgeG1sbnM6bT0iaHR0cDovL3NjaGVtYXMubWlj
cm9zb2Z0LmNvbS9vZmZpY2UvMjAwNC8xMi9vbW1sIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcv
VFIvUkVDLWh0bWw0MCI+DQo8aGVhZD4NCjxtZXRhIGh0dHAtZXF1aXY9IkNvbnRlbnQtVHlwZSIg
Y29udGVudD0idGV4dC9odG1sOyBLWZhbWlseToiQ2FsaWJyaSIsc2Fucy1zZXJp
ZjsNCglmb250LXdlaWdodDpib2xkO30NCmE6bGluaywgc3Bhbi5Nc29IeXBlcmxpbmsNCgl7bXNv
LXN0eWxlLXByaW9
How should I be reading the body? So that I can have my test pass all the emails correctly?
Thank you
Also I found a way to get rid of those characters.
I looked at the encoding and I am parsing based on encoding numbers as follow
$encoding = $structure->encoding;
if($encoding == 3) {
$finalmsg = imap_base64($message);
} else if($encoding == 1) {
$finalmsg = imap_8bit($message);
} else {
$finalmsg = imap_qprint($message);
}
I am still having issues when encoding type = 0. The signature is causing major problem

Open Mailbox with PHP imap_open, DomainFactory mail

I tried to open my mail server with imap_open(). I always get this error:
Warning: imap_open(): Couldn't open stream {sslin.df.eu:993/novalidate-cert}
I tried this code:
{sslin.df.eu:993/novalidate-cert}INBOX
{sslin.df.eu:993}INBOX
{sslin.df.eu:993}
{sslin.df.eu:993/imap/ssl/novalidate-cert}
{sslin.df.eu:993/imap/ssl/novalidate-cert}INBOX
{sslin.df.eu:993/imap/novalidate-cert}INBOX
{sslin.df.eu:993/novalidate-cert}INBOX
my current code:
//The location of the mailbox.
$mailbox = '{sslin.df.eu:993/novalidate-cert}INBOX.';
//The username / email address that we want to login to.
$username = 'example#example.com';
//The password for this email address.
$password = 'xxx';
//Attempt to connect using the imap_open function.
$imapResource = imap_open($mailbox, $username, $password);
//If the imap_open function returns a boolean FALSE value,
//then we failed to connect.
if($imapResource === false){
//If it failed, throw an exception that contains
//the last imap error.
throw new Exception(imap_last_error());
}
//If we get to this point, it means that we have successfully
//connected to our mailbox via IMAP.
//Lets get all emails that were received since a given date.
$search = 'SINCE "' . date("j F Y", strtotime("-7 days")) . '"';
$emails = imap_search($imapResource, $search);
//If the $emails variable is not a boolean FALSE value or
//an empty array.
if(!empty($emails)){
//Loop through the emails.
foreach($emails as $email){
//Fetch an overview of the email.
$overview = imap_fetch_overview($imapResource, $email);
$overview = $overview[0];
//Print out the subject of the email.
echo '<b>' . htmlentities($overview->subject) . '</b><br>';
//Print out the sender's email address / from email address.
echo 'From: ' . $overview->from . '<br><br>';
//Get the body of the email.
$message = imap_fetchbody($imapResource, $email, 1, FT_PEEK);
}
}

fsockopen only working to check port 80 and 8000 (no other ports)

I'm trying to use fsockopen as a port checker to see if a specific port is open on an IP address, but it only seems to be working on Ports 80 and 8000. For any other port, it returns that the port is closed even when it is open. I was wondering how I could possibly fix this?
$server_ip= $_POST['server'];
$port = $_POST['port'];
$server_ip = gethostbyname($server_ip);
$status = array();
if (empty($_POST["server"]) || empty($_POST['port']) || !filter_var($server_ip, FILTER_VALIDATE_IP) )
{
echo "some html code";
}
elseif (!(is_numeric($port)))
{
echo "some other html code";
}
else
{
if($pf = #fsockopen($_POST['server'], $port, $err, $err_string, 1)) {
$status = true;
fclose($pf);
} else {
$status = false;
}
Output code here..
}

Sending udp message from a remote server to my computer via internet

I am trying to send a udp message from a php webpage to my computer and it works if I send it from the localhost, but if I send it from a remote server I will never receive the message. Please advise how to set the ip address, I have public and private ip addresses. I tried $server_ip set to both the public and private ip address of my computer but it doesn't work.
<?php
$server_ip = '127.0.0.1';
$server_port = 2009;
$beat_period = 5;
$message = 'Hello world! ';
print "Sending heartbeat to IP $server_ip, port $server_port\n";
print "press Ctrl-C to stop\n";
if ($socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) {
while (1) {
socket_sendto($socket, $message, strlen($message), 0, $server_ip, $server_port);
print "Time: " . date("%r") . "\n";
sleep($beat_period);
}
} else {
print("can't create socket\n");
}
?>
Please advise ...

redirecting to thank you page after sending a message via contact form - php

I am having problem with contact form which is working fine and displays a message "sent ok" after sending it. BUT when I changed code so it will display "a thank you page" after sending a message instead of simple "sent ok" page goes blank and message never arrives at my email box. Why that can be? the only code that was changes was:
// #SEND MAIL
if($m->Send()) {
die('_sent_ok_');
} else {
die($m->ErrorInfo);
}
na
// #SEND MAIL
if($m->Send()) {
header ("Location: http://xyz.ie/thankyouurl.php");
} else {
header ("Location: http://xyz.ie/errorurl.php");
}
exit();
Here is current version of code.
<?php
#ini_set('display_errors', 1);
#ini_set('track_errors', 0);
#date_default_timezone_set('Europe/Bucharest'); // Used only to avoid annoying warnings.
if($_REQUEST['action'] = 'email_send') {
$array['name'] = isset($_REQUEST['name']) ? strip_tags(trim($_REQUEST['name'])) : '';
$array['email'] = isset($_REQUEST['email']) ? ckmail($_REQUEST['email']) : '';
$array['subject'] = isset($_REQUEST['subject']) ? strip_tags(trim($_REQUEST['subject'])) : '-';
$array['message'] = isset($_REQUEST['message']) ? (trim(strip_tags($_REQUEST['message'], '<b><a><strong>'))) : '';
// Visitor IP:
$ip = ip();
// DATE
$date = date('l, d F Y , H:i:s');
// BEGIN
require('config.inc.php');
require('phpmailer/5.1/class.phpmailer.php');
$m = new PHPMailer();
$m->IsSMTP();
$m->SMTPDebug = false; // enables SMTP debug information (for testing) [default: 2]
$m->SMTPAuth = true; // enable SMTP authentication
$m->Host = $config['smtp_host']; // sets the SMTP server
$m->Port = $config['smtp_port']; // set the SMTP port for the GMAIL server
$m->Username = $config['smtp_user']; // SMTP account username
$m->Password = $config['smtp_pass']; // SMTP account password
$m->SingleTo = true;
$m->CharSet = "UTF-8";
$m->Subject = ($array['subject'] == '-') ? $config['subject'] : $array['subject'];
$m->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$m->AddAddress($config['send_to'], 'Contact Form');
$m->AddReplyTo($array['email'], $array['name']);
$m->SetFrom($config['smtp_user'], 'Contact Form');
$m->MsgHTML("
<b>Date:</b> {$date} <br>
<b>Name:</b> {$array['name']}<br>
<b>Email:</b> {$array['email']}<br>
<b>Subject:</b> {$array['subject']}<br>
<b>Message:</b> {$array['message']}<br>
---------------------------------------------------<br>
IP: {$ip}
");
if($config['smtp_ssl'] === true)
$m->SMTPSecure = 'ssl'; // sets the prefix to the server
// #SEND MAIL
if($m->Send()) {
header ("Location: http://xyz.ie/thankyouurl.php");
} else {
header ("Location: http://xyz.ie/errorurl.php");
}
exit();
function ip() {
if (getenv('HTTP_CLIENT_IP')) { $ip = getenv('HTTP_CLIENT_IP'); }
elseif (getenv('HTTP_X_FORWARDED_FOR')) { $ip = getenv('HTTP_X_FORWARDED_FOR'); }
elseif (getenv('HTTP_X_FORWARDED')) { $ip = getenv('HTTP_X_FORWARDED'); }
elseif (getenv('HTTP_FORWARDED_FOR')) { $ip = getenv('HTTP_FORWARDED_FOR'); }
elseif (getenv('HTTP_FORWARDED')) { $ip = getenv('HTTP_FORWARDED'); }
else { $ip = $_SERVER['REMOTE_ADDR']; }
return $ip;
}?>
Your problem could be because you are missing your close "}" of your first "if".
if($_REQUEST['action'] = 'email_send') {
I think should be before:
function ip() {
And I think you don't need the exit line.
It turned out that ckmail below were coasing a problem. So I changed it on strip_tags. And It worked out.
$array['email'] = isset($_REQUEST['email']) ? ckmail($_REQUEST['email']) : '';

Categories