Sendgrid Unauthenticated senders not allowed error php, zf2 - zend-framework2

I have integrate Sendgrid for my Zend Framework 2 Application using STMP API and i have used Zend Transport for but I get a error
"Caught exception: Cannot receive from specified address : Unauthenticated senders not allowed"
$request = $this->getRequest();
//$form = new Add();
// $product = new Product();
$username = 'XXX';
$password ='XXXX';
if ($request->isXmlHttpRequest()){ // If it's ajax call
$email = $request->getPost('email_add');
$message = $request->getPost('message');
try{
$message = new Message();
$message->addTo('jainudeenf007#gmail.com')
->addFrom('fawazj#digitalglare.com.au')
->setSubject('Greetings and Salutations!')
->setBody("Sorry, I'm going to be late today!");
$transport = new SmtpTransport();
$options = new SmtpOptions(array(
'name' => 'sendgrid.com',
'host' => 'smtp.sendgrid.net',
'port' => 587, // Notice port change for TLS is 587
'connection_class' => 'smtp',
'connection_config' => array(
'auth' => 'login',
'username' => 'XXXXXX',
'password' => 'XXXXXX',
'ssl' => 'tls'
),
));
$transport->setOptions($options);
$transport->send($message);
exit;
}catch (\Exception $ex){
echo 'Caught exception: ', $ex->getMessage(), "\n";
exit;
}
}

Sendgrid has an api and this api is implemented by SlmMail (disclaimer: I am the author of SlmMail). Using that API is easier to use than using the old SMTP protocol.
I am not sure how to exactly configure the SMTP options, but previously we worked with the Google SMTP servers and it required this configuration:
'name' => 'gmail.com',
'host' => 'smtp.gmail.com',
'port' => 587,
'connection_class' => 'login',
'connection_config' => array(
'ssl' => 'tls',
'username' => $username,
'password' => $password,
),
This is slightly different than yours ("class" is "login", there is no "auth" option). Check also the documentation where all SMTP options are specified.

In addition to using the SimMail option, you can try using Zend's mail module as described in our documentation: http://sendgrid.com/docs/Integrate/Frameworks/zend.html
One other option is our Web API, for which we have a PHP library here: https://github.com/sendgrid/sendgrid-php

Try this
connection_class plain should work
use Zend\Mail\Transport\Smtp as SmtpTransport;
use Zend\Mail\Transport\SmtpOptions;
$transport = new SmtpTransport();
$options = new SmtpOptions(array(
'name' => $name,
'host' => $host,
'port' => 587,
'connection_class' => 'plain',
'connection_config' => array(
'username' => $username,
'password' => $password,
),
));
$transport->setOptions($options);
$transport->send($mail);

Related

How to send messages using AWS Pinpoint service using PHP SDK

Hi I am trying to send a message using AWS Pinpoint with the PHP SDK, but I am getting an error. Any ideas why? Here is my code:
$client = new PinpointClient([
'version' => 'latest',
'region' => 'us-east-1',
'credentials' => [
'key' => 'MYIAMAWSKEY',
'secret' => 'MYAWSSECRET',
]
]);
$result = $client->sendMessages([
'ApplicationId' => 'MyAppId',
'MessageRequest' => [
'Addresses' => [
'AnIOSDeviceToken' => [
'BodyOverride' => 'aaa',
'ChannelType' => 'APNS',
'RawContent' => 'bbb',
'Context' => ['ccc' => '222'],
'TitleOverride' => 'ddd',
],
],
],
'MessageConfiguration' => [
'APNSMessage' => [
'Action' => 'OPEN_APP',
'Body' => 'Hello',
'RawContent' => 'World',
'SilentPush' => false,
'Title' => 'Hello World!!!',
],
],
]);
I get the following error:
DeliveryStatus: 'PERMANENT_FAILURE'
StatusCode: 400
StatusMessage: Invalid notification: Notification is malformed
According to the API Docs, 'MessageConfiguration' needs to be inside the 'MessageRequest' field. Please make sure your input parameters fully match the documented API Parameter Syntax.

How configurate the certificate of php 5.6 in SmtpTransport of ZEND 2?

Using phpMailer the config is that (it is working):
$mail->SMTPOptions = array (
'ssl' => array(
'verify_peer' => true,
'verify_depth' => 3,
'allow_self_signed' => true,
'peer_name' => 'smtp.rei.unicamp.br',
'cafile' => '/var/www/apache/conf/certs/cadeia-completa-globalsign.pem',
)
My old configuration of Smtp are (it is working in php versions before 5.6):
$options = new SmtpOptions(array(
'name' => $name,
'host' => $host,
'port' => 587,
'connection_class' => 'plain',
'connection_config' => array(
'username' => 'test',
'password' => 'test123',
'ssl' => 'tls',
),
Anyone knows how I put the first configuration on the SmtpOptions of Zend 2?
Thank you!!

ZF2 mail error Too many RSET commands

I have to send a lot of emails distributed over the whole day with ZF2 and I'm using Zend\Mail\Transport\Smtp. I have the mails in a queue table and once a minute I want to send them. Everything works fine until 5th email, where I receive an error : Zend\Mail\Protocol\Exception\RuntimeException
4.7.0 Too many RSET commands; closing connection
I guess this is coming from the smtp server. But why does ZF2-Smtp reset each message? I don't know where to start debugging the problem and if you need more informations just let me know. Thx for any help...
What I'm using is:
$transport = new SmtpTransport();
$options = new SmtpOptions(array(
'host' => $config['mail_options']['server'],
'port' => $config['mail_options']['port'],
'connection_class' => 'login',
'connection_config' => array(
'username' => $config['mail_options']['smtp_user'],
'password' => $config['mail_options']['smtp_password'],
'ssl' => $config['mail_options']['ssl'],
),
));
foreach ($queuedMails as $queuedMail) {
$message = new Message();
$message->addTo($data['to'])
->addFrom($config['mail_options']['from'], $config['mail_options']['from_name'])
->setSubject($data['subject'])
->setBody(utf8_decode($data['body']))
->setEncoding('utf-8');
$transport->setOptions($options);
$transport->send($message);enter code here
}
Should I place the $transport into the foreach?

Local mysql settings in ZF2

I want add in my ZF2 application at begining of every mysql connection this mysql query/setting
SET time_zone = \'+00:00\';
for some reasons i couldn't do it on mysql conifiguration file.
So where i can do it?
(now i create dbadapter by config file:
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
etc...
)
Try this in your config file (for example in config/autoload/global.php)
return array(
'db' => array(
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET time_zone = \'+00:00\''
)
)

Zend Framework 2: How to properly replace Figlet with reCaptcha on zfcUser

I am trying to replace Figlet with reCaptcha on a zfcUser registration form. Partial instruction on how to accomplish this can be found on https://github.com/ZF-Commons/ZfcUser#changing-registration-captcha-element but no complete instruction exists.
Checking the README.md file has a two-step instruction on how to accomplish this but still the CAPTCHA uses Figlet when rendered on the form.
Has anyone successfully implemented this? I really need a hand on this one.
Thanks in advance.
EDIT: Here is a proven working solution I developed:
1. Add to composer.json
// Add the lines below under the "require" element:
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": ">2.2.0rc1",
"zendframework/zendservice-recaptcha": "2.*"
}
2. Goto to your project's ZF2 installation directory and execute this command:
php composer.phar update
3. Replace or Create config/autoload/database.global.php with:
<?php
$config = array(
'dbdriver' => 'pdo',
'dbhost' => 'localhost',
'dbport' => '3306',
'dbname' => 'CHANGEME',
'dbuser' => 'CHANGEME',
'dbpass' => 'CHANGEME',
);
return array(
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
'db' => array(
'driver' => 'pdo',
'dsn' => 'mysql:dbname='.$config['dbname'].';host='.$config['dbhost'],
'username' => $config['dbuser'],
'password' => $config['dbpass'],
),
);
4: Execute this on your mySQL server:
CREATE TABLE `user`
(
`user_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(255) DEFAULT NULL UNIQUE,
`email` VARCHAR(255) DEFAULT NULL UNIQUE,
`display_name` VARCHAR(50) DEFAULT NULL,
`password` VARCHAR(128) NOT NULL,
`state` SMALLINT UNSIGNED
) ENGINE=InnoDB CHARSET="utf8";
5. Create/Replace config/autoload/recaptcha.global.php with:
<?php
define('RECAPTCHA_PRIVATE_KEY','CHANGEME');
define('RECAPTCHA_PUBLIC_KEY','CHANGEME');
return array(
'zfcuser' => array(
'form_captcha_options' => array(
'class' => 'Zend\Captcha\ReCaptcha',
'options' => array(
'privkey' => RECAPTCHA_PRIVATE_KEY,
'pubkey' => RECAPTCHA_PUBLIC_KEY,
),
),
),
'di'=> array(
'instance'=>array(
'alias'=>array(
'recaptcha_element' => 'Zend\Form\Element\Captcha',
),
'ZfcUser\Form\Register' => array(
'parameters' => array(
'captcha_element'=>'recaptcha_element',
),
),
),
),
);
6. Create/Replace config/autoload/zfcuser.global.php with:
<?php
$settings = array(
'enable_registration' => true,
'enable_username' => true,
'auth_adapters' => array( 100 => 'ZfcUser\Authentication\Adapter\Db' ),
'enable_display_name' => false,
'auth_identity_fields' => array( 'email' ),
'use_registration_form_captcha' => true,
'user_login_widget_view_template' => 'zfc-user/user/login.phtml',
);
return array(
'zfcuser' => $settings,
'service_manager' => array(
'aliases' => array(
'zfcuser_zend_db_adapter' => (isset($settings['zend_db_adapter'])) ? $settings['zend_db_adapter']: 'Zend\Db\Adapter\Adapter',
),
),
);
7. Navigate to http://yourdomain.com/user
8. Enjoy! :)
This is how I did it, it might not be the best or correct way but it worked for me:
Add the recaptcha service to your composer.json file:
"require": {
"Zendframework/zendservice-recaptcha": "2.*"
}
Run composer to get the service. Then you need to specify the ReCaptcha config.
I created a separate config file to store the ReCaptcha keys:
//zfcuser.local.php
return array(
'zfcuser' => array(
'form_captcha_options' => array(
'options' => array(
'privkey' => RECAPTCHA_PRIVATE_KEY,
'pubkey' => RECAPTCHA_PUBLIC_KEY,
),
),
),
);
Then ZfcUser captcha config looks like so, telling it to use the ReCaptcha service:
//zfcuser.global.php
'form_captcha_options' => array(
'class' => 'Zend\Captcha\ReCaptcha',
'options' => array(
'wordLen' => 6,
'expiration' => 300,
'timeout' => 300,
),
),
Edit:
You don't need the recaptcha.global.php. You can call the config file whatever you like aslong as it ends with .global.php or .local.php. You usually name things .local.php when you don't want them in version control.
In this case I named the file zfcuser.local.php because all it does is store the ReCaptcha keys and I didn't want them in version control.
All the config files get merged in to one array when the application is started. So basically, ignore the ZfcUser documentation. Or maybe someone else can explain how to get it working that way.
The third block of code is the zfcuser.global.php.

Resources