I have meet a big problem when I was doing developing about Apple Push Notification Service.
I wrote a PHP script(apns.php) which will use a PEM(ck.pem) file as security certificate and build a SSL connection to APNS when I post a HTTP request to it, but it always failed when it tried to connect the service port ( ssl://gateway.sandbox.push.apple.com:2195). The PHP script is deployed on a Linux Hosting of GoDaddy (it should support SSL).
I have already registered to be a member of iOS developer program,and I have registered the apple push notification service for my application. And I have generated a certificate file and a key file (cert.p12 and key.p12) from Keychain Access and translate them into PEM files(cert.pem and key.pem) and join them into one(ck.pem) and put it in the same directory of PHP script .
I want to know if there is something wrong I did ! Would you refer to the attachment of the files that you may need?
Thanks very much!
The HTTP request is something like below.
http://www.insidertracker.net/apns/apns.php?message=&badge=2&sound=received5.caf
The response message of my request is below:
Warning: stream_socket_client() [function.stream-socket-client]:
unable to connect to ssl://gateway.sandbox.push.apple.com:2195
(Connection refused) in /home/content/40/6967940/html/apns/apns.php on
line 25 Failed to connect 111 Connection refused
The PHP script:
<?php
$deviceToken = '0535dda1 6fd04e87 ed0a8194 d418a6c1 99eec462 8a871891 d062018d c6af4f99';
$pass = 'Php1234'; // Passphrase for the private key (ck.pem file)
// Get the parameters from http get or from command line
$message = $_GET['message'] or $message = $argv[1] or $message = 'You have an important message from InsiderTracker';
$badge = (int)$_GET['badge'] or $badge = (int)$argv[2];
$sound = $_GET['sound'] or $sound = $argv[3];
// Construct the notification payload
$body = array();
$body['aps'] = array('alert' => $message);
if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;
/* End of Configurable Items */
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
// assume the private key passphase was removed.
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
// connect to apns
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
// $fp = fsockopen('ssl://gateway.sandbox.push.apple.com:2195', 2195, $err, $errstr, 30);
if (!$fp) {
print "Failed to connect $err $errstr\n";
return;
}
else {
print "Connection OK\n<br/>";
}
// send message
$payload = json_encode($body);
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack ("n",strlen($payload)) . $payload;
print "Sending message :" . $payload . "\n";
fwrite($fp, $msg);
fclose($fp);
?>
It's my understanding that GoDaddy prohibits outgoing connections on exotic ports (anything other than 80 a perhaps a few others). You're probably out of luck trying to roll your own push service hosted on GoDaddy. I have had luck doing what you're attempting with bluehost.
For general advice on developing a push service in PHP, these two articles were invaluable: One Two
I also got this error. Following things you have to remember:
Path of ios push cert file should be correct. Check on online portals using device token and push cert.
Your server is enabled for 2195 port. Check on terminal by using telnet command.
Check ssl supported by your php version if using ssl://gateway.sandbox.push.apple.com and tls if using tls://gateway.sandbox.push.apple.com
The data you are sending in the push or token etc should be equal or less than the specified bytes mentioned in the apple doc. link is mentioned below.
You can also use api get reference for sending push Development server: api.development.push.apple.com:443 Production server: api.push.apple.com:443
https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html#//apple_ref/doc/uid/TP40008194-CH11-SW1
Related
This is my first post so please be gentle.
I've been trying to send automatic SMTP emails via PHPMailer using my webhost as the server. Below is the script I am using.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require '../PHPMailer/src/Exception.php';
require '../PHPMailer/src/PHPMailer.php';
require '../PHPMailer/src/SMTP.php';
//Load Composer's autoloader
//require 'vendor/autoload.php';
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 4; //SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Mailer = "smtp";
$mail->Host = 'surrey.redbackinternet.net';
$mail->SMTPAuth = true;
$mail->Username = 'noreply#mydomain.com';
$mail->Password = 'password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
//Recipients
$mail->setFrom('noreply#mydomain.com', 'xxx');
$mail->addAddress('contact#mydomain.com');
//Content
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
Previously, I was using Gmail as the server, which worked fine, but now I want to change this. When I run the script with the new details I get authentication error - "SMTP ERROR: Password command failed: 535 Incorrect authentication data". The full PHPMailer script log is shown in the image below.
PHPMailer log
I know the username and passwords used are correct and have seen this error seems to be returned whenever a connection cannot be established. I have a hunch it's related to my email DNS settings, though I could be completely wrong here. When I check my DNS and MX settings using dnschecker.org all seems ok as far as I can naively tell. But using hardenize.com I see my domain fails their mail server and email TLS checks. The information provided is lacking but it does say "A network error occurred while we were trying to communicate with a server. Error message: java.net.SocketTimeoutException: Read timed out". I should add here that my site uses Cloudflare as a CDN.
Any ideas on what the issue is or how to resolve it?
Thanks in advance.
I am currently trying to connect to a gmail inbox using Perl and
Net::IMAP::Client
with the following code
use strict;
use warnings;
use Net::IMAP::Client;
my $user = '[address]#gmail.com';
my $pwd = '[password]';
my $imap = Net::IMAP::Client->new(
server => 'imap.gmail.com',
user => $user,
pass => $pwd,
ssl => 1, # (use SSL? default no)
ssl_verify_peer => 0, # (use ca to verify server, default yes)
port => 993 # (but defaults are sane)
) or die "Could not connect to IMAP server: $_";
$imap->login or
die('Login failed: ' . $imap->last_error)
But the $imap variable is undef and I am getting this error:
Use of uninitialized value $_ in concatenation (.) or string at testIMAP.pl line 9.
Could not connect to IMAP server: at testIMAP.pl line 9.
I have successfully connected to the mailbox using Outlook, but as I'm not getting an error message I'm not sure where to look. Does anyone know what I'm doing wrong here?
A big thanks to zdim for help troubleshooting.
Firstly, zdim pointed out that I had the incorrect error variable. $_ should have been $!
This revealed the error message "Network is unreachable", however I was able to pint and telnet to 'imap.gmail.com' successfully.
The solution to this was found here
Perl IO::Socket::SSL: connect: Network is unreachable .
Changing the use statement in the Net::IMAP::Client module to the following worked:
use IO::Socket::SSL 'inet4';
After this, the connection was established, but the login would fail with
Login failed: [ALERT] Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure)
This is due to Gmail's security features. I received an email that allowed me to confirm that the connection was not malicious, and then subsequent logins were successful.
For others, there may be a few solutions to this last one. You may need to issue an 'app password' If two-step authentication is activated, or you might need to toggle on 'allow less secure apps'
I've try to send mail using perl with attachment I've tried with Email::Stuff and MIME::Lite,during run time i got some error as Authentication failed or server not connected can anybody help me?
corresponding code is:
use MIME::Lite;
use Net::SMTP;
### Adjust sender, recipient and your SMTP mailhost
my $from_address = 'atme04#gmail.com';
my $to_address = 'thiyagu040#gmail.com';
my $mail_host = 'smtp.gmail.com';
### Adjust subject and body message
my $subject = 'A message with 2 parts ...';
my $message_body = "Here's the attachment file(s) you wanted";
my $your_file_zip = 'my.zip';
$msg = MIME::Lite->new (
From => $from_address,
To => $to_address,
Subject => $subject,
Type =>'multipart/mixed'
) or die "Error creating multipart container: $!\n";
MIME::Lite->send('smtp', 'smtp.gmail.com' ,
Port =>465 ,
Timeout=>320 ,
Debug => 1 ,
Hello => $mail_host,
User => $from_address,
Password => 'Thiyagu.04' );
#$mime_msg->send() or die "Error sending message: $!\n";
#MIME::Lite->send('smtp',$mail_host,AuthUser=> $from_address, AuthPass=>"apssword");
$msg->send();
error message is;
SMTP Failed to connect to mail server: A connection attempt failed because the connected party did not properly respond aft
er a period of time, or established connection failed because connected host has failed to respond.
at mail.pl line 54.
thanks in advance
The error message seems pretty clear to me. The problem is with your connection to the mail server - so changing the module that you're using is unlikely to achieve anything useful. I don't know enough about Gmail's server settings to comment on what the problem is, but this page has some suggestions that you might follow. Specifically, you could check that the Gmail account has 'allow authentication' turned on and that your mail client (the Perl program) is using SSL for the connection.
Also, this might be easier if you used an email that was specifically designed for use with Gmail. Email::Send::SMTP::Gmail looks tailor-made for your requirements.
I have a problem with push notification in my app. I just started to work with push notifications so I dont know what's the reason of my problem, and i cant find the solution in google.
So, I used this tutorial http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12 as the example of programming of push notifications. When I run "php simplepush.php" on my terminal everything is fine and I get the notification on my device. But when I load that script on the server and trying to run it from there there is no actions performed. only 30 sec of waiting and message "Failed to connect: 110 Connection timed out"
Here os the code of my script
<?php
Put your device token here (without spaces):
$deviceToken = '***************';
// Put your private key's passphrase here:
$passphrase = '**************';
// Put your alert message here:
$message = 'My first push notification!';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
Yes, as NewObjective clarified, 2195 & 2196 ports have to be open. To open these ports you might ask Bluehost technical support team (ticket/chat), but you have to have dedicated IP address before that.
But there is another issue. If you try to send push notification message right after dedicated IP address is set and both ports are open you might receive the same "Connection timed out" error. You can test that problem by logging in to your server via SSH and running "telnet gateway.sandbox.push.apple.com 2195" command. If you receive "Connection timed out" error - you can try to contact Bluehost support team, but they can't solve this problem.
If you try to telnet any other server with 2195 opened port you will also receive that error. I had about 10 chats with Bluehost support team, opened 10 or so tickets, but they really couldn't help me. But two weeks later everything began to work. I think it was because turning dedicated IP on takes some time, but I'm not sure. Anyway, it very interesting that Bluehost support team always answers that the problem is on the Apple side, even if you tell them that any other server with 2195 opened port isn't available too)
i'm using Zend_Mail to send out an email from Aruba PEC.
$config = array('auth'=>'login',
'ssl' => 'ssl',
'port' => 465,
'username'=>'user#pec.it',
'password'=>'pass');
$tr = new Zend_Mail_Transport_Smtp('62.149.152.91',$config);
Zend_Mail::setDefaultTransport($tr);
$mail = new Zend_Mail();
$mail->setBodyHtml('Mail test');
$mail->setFrom('user#pec.it');
$mail->addTo('marco#email.it');
$mail->setSubject('Test');
$mail->send();
but i receive this error: " Could not open socket ".
Now if i try to exceute this script on my local pc it works correctly, but wen i try to exceute it on server in my local area network i receive this error.
than i i try with another ip address (no certifaction account) it works correctly.
(so i think that is a problem of configuration server??)
thanks
You need to have OpenSSL installed on the PC. Is your server Windows? It is a bit of a pain if you are.