i wan't to send the same mail to multiple recipients retreived from database, using zend framework 2. With my solution i can only send email to the first row on my database, and i do not no what's the problem exactly.
This is my action in the indexController:
public function eventdetailsAction() {
$id = (int) $this->params()->fromRoute('id', 0);
$this->layout()->setVariable('lang', $this->params()->fromRoute('lang', 'en_US'));
$this->layout()->setVariable('action', $this->params()->fromRoute('action', 'index'));
$request = $this->getRequest();
$aPost = $request->getPost();
if (isset($aPost['invitUser'])) {
$user = new Container('user');
$db = $this->getServiceLocator()->get('db1');
if (!$user->offsetExists('id')) {
$idconnected = '0';
} else {
$user = new Container('user');
$db = $this->getServiceLocator()->get('db1');
if (!$user->offsetExists('id')) {
$idconnected = '0';
} else {
$idconnected = $user->offsetGet('id');
$mail = $db->query("SELECT * FROM user")->execute()->current();
print_r($mail);
exit();
$message = new Message();
foreach ($mail as $recip) {
$message->addTo($recip)
->addFrom('xxxxx#gmail.com')
->setSubject('Invitation for the event : Event Latino');
}
// Setup SMTP transport using LOGIN authentication
$transport = new SmtpTransport();
$options = new SmtpOptions(array(
'host' => 'smtp.gmail.com',
'connection_class' => 'login',
'connection_config' => array(
'ssl' => 'tls',
'username' => 'xxxxx#gmail.com',
'password' => 'xxxxxx'
),
'port' => 587,
));
$html = new MimePart('<b>Invitation for the event: Latin Night, orgonized by Mr. Jony Cornillon. Date : 06/04/2015</b>');
$html->type = "text/html";
$body = new MimeMessage();
$body->addPart($html);
//$body->setParts(array($html));
$message->setBody($body);
$transport->setOptions($options);
$transport->send($message);
}
}
}
}
And also when i put printr($mail); exit(); to show the result, only the first line will be displayed on the page. Any help please.
In the line below you are executing the query, and then obtaining the current() record from the result set the execute() call returned. That will always give you the first record in the result set.
$mail = $db->query("SELECT * FROM user")->execute()->current();
Change that to
$mail = $db->query("SELECT * FROM user")->execute();
and you should be able to iterate over the result set as expected.
i find another solution and i can succefully diplay all emails from database with print_r. However now when i put the resultset on the message->addTo(), an error will diplay. This is my new code
$sql = "SELECT * FROM user";
$statement = $db->query($sql);
$res = $statement->execute();
if ($res instanceof ResultInterface && $res->isQueryResult()) {
$resultSet = new ResultSet;
$resultSet->initialize($res);
$message = new Message();
foreach ($resultSet as $row) {
echo $row->email . PHP_EOL;
$message->addTo($row->email)
->addTo('xxxxx#hotmail.com', 'eee#web.com')
->addFrom('xxxxx#gmail.com')
->setSubject('Invitation for the event : Event Latino');
}
Any help please. Thanks.
Related
I want to prevent double post on double click on the submit button.
I have this code to save the data:
public function reply($id = null){
date_default_timezone_set('Europe/Amsterdam');
date_default_timezone_set('UTC');
$username = $this->Auth->user('username');
$authorRang = $this->Auth->user('rang');
$posts = TableRegistry::get('posts');
$threads = TableRegistry::get('threads');
$query = $threads->find('all');
$last_post_date = date('m/d/Y h:i:s a', time());
$replies = $posts->newEntity();
if ($this->request->is('post')) {
//$token = $this->request->getParam('_csrfToken');
$reply = $posts->patchEntity($replies, $this->request->data);
$reply['thread_id'] = $id;
$reply['author'] = $username;
$reply['created'] = $last_post_date;
$reply['authorRang'] = $authorRang;
$query->update()
->set(['last_post_date' => $last_post_date])
->where(['id' => $id])
->execute();
if ($posts->save($reply)) {
$this->Flash->success(__('Your reply has been saved.'));
//return $this->redirect(['action' => 'view/'.$id.'']);
}else{
$this->Flash->error(__('Unable to add your reply.'));
}
}
$this->set('reply', $replies);
}
How can I prevent it?
I have tried many things but it doesn't work. I tried to unset the entity but it does not work. It still saves the data multiple times.
Thanks
I am using zend-framework2 and google-app-engine server to import all gmail contact list. I am getting authorization code from gmail after login. When hitting curl using this authorization code to get access token, it gives blank array as response.
Also this app is a billed app in google-app-engine. and already set
extension = "curl.so"
in php.ini file. and the credentials which i have given are correct.
// Import Gmail Contacts
$client_id='xxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com';
$client_secret='xxxxxxxxxxxxxx-xxxxxxxxx';
$redirect_uri='http://xxxxxxxxxxxxxxx.appspot.com/u/invite/gmailCallback';
$max_results = 1000;
if(isset($_GET["code"]) && $_GET["code"] != '') {
$auth_code = $_GET["code"];
$fields=array(
'code'=> urlencode($auth_code),
'client_id'=> urlencode($client_id),
'client_secret'=> urlencode($client_secret),
'redirect_uri'=> urlencode($redirect_uri),
'grant_type'=> urlencode('authorization_code')
);
$post = '';
foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token');
curl_setopt($curl,CURLOPT_POST,5);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,FALSE);
$result = curl_exec($curl);
//$info = curl_getinfo($curl);
//echo "<pre>"; print_r($info); die;
curl_close($curl);
$response = json_decode($result);
//echo "<pre>"; var_dump($response); die;
if(isset($response->access_token) && $response->access_token != '') {
$accesstoken = $response->access_token;
$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&oauth_token='.$accesstoken;
//$url = 'https://www.google.com/m8/feeds/contacts/default/full?oauth_token='.$accesstoken;
$xmlresponse = $this->curl_file_get_contents($url);
//At times you get Authorization error from Google.
if((strlen(stristr($xmlresponse,'Authorization required'))>0) && (strlen(stristr($xmlresponse,'Error '))>0)) {
//$returnArr['error_msg'] = "Something went wrong to import contacts!!";
echo '<h2>Something went wrong to import contacts !!</h2>';die;
} else {
$xml = new SimpleXMLElement($xmlresponse);
$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
$result = $xml->xpath('//gd:email');
//echo "<pre>"; print_r($result); die;
foreach ($result as $title) {
$emailsArr[] = (string) $title->attributes()->address;
}
}
}
}
Please help.
From previous experience (and this answer) I don't think cURL is enabled on App Engine as C extensions aren't supported.
You'll have to use URL Fetch instead e.g.
$data = ['data' => 'this', 'data2' => 'that'];
$data = http_build_query($data);
$context = [
'http' => [
'method' => 'GET',
'header' => "custom-header: custom-value\r\n" .
"custom-header-two: custom-value-2\r\n",
'content' => $data
]
];
$context = stream_context_create($context);
$result = file_get_contents('http://app.com/path?query=update', false, $context);
App engine supports cURL, but it seems to be buggy. I have the same problem with OAuth authentication via Google. The POST cURL call just returns FALSE without any error message. Here is the official bug report: https://code.google.com/p/googleappengine/issues/detail?id=11985
What you can do now, is to switch to url fetch functions. To make a POST request you can use following function:
protected function _call($url, array $params, $method = 'GET', array $headers = [])
{
$query = http_build_query($params);
$headers_str = '';
if ($headers) {
foreach ($headers as $name => $value) {
$headers_str .= $name . ': ' . $value . "\r\n";
}
}
$headers_str .= "Content-type: "."application/x-www-form-urlencoded"."\r\n";
$options =
array('http'=>
array(
'method' => $method,
'header' => $headers_str,
'content' => $query,
'ignore_errors' => true,
)
);
if ($method === 'POST') {
$options['http']['content'] = $query;
} else if ($query) {
$url .= '?' . $query;
}
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
// check for zipped response and decode it
if (array_search('Content-Encoding: gzip', $http_response_header)) {
$response = gzinflate(substr($response, 10, -8));
}
return $response;
}
Or you can try to use this cURL wrapper library https://github.com/azayarni/purl, which was implemented before GAE provided cURL support. Hope it is helpful:)
i wan't to send the same mail to multiple recipients retreived from database, using zend framework 2. With my solution i can only display all emails from database but i can't send mails to them. I do not no what's the problem exactly. This is my action in the indexController:
public function eventdetailsAction() {
$id = (int) $this->params()->fromRoute('id', 0);
$this->layout()->setVariable('lang', $this->params()->fromRoute('lang', 'en_US'));
$this->layout()->setVariable('action', $this->params()->fromRoute('action', 'index'));
$request = $this->getRequest();
$aPost = $request->getPost();
if (isset($aPost['invitUser'])) {
$user = new Container('user');
$db = $this->getServiceLocator()->get('db1');
if (!$user->offsetExists('id')) {
$idconnected = '0';
} else {
$user = new Container('user');
$db = $this->getServiceLocator()->get('db1');
if (!$user->offsetExists('id')) {
$idconnected = '0';
} else {
$idconnected = $user->offsetGet('id');
$sql = "SELECT * FROM user";
$statement = $db->query($sql);
$res = $statement->execute();
if ($res instanceof ResultInterface && $res->isQueryResult()) {
$resultSet = new ResultSet;
$resultSet->initialize($res);
$message = new Message();
foreach ($resultSet as $row) {
echo $row->email . PHP_EOL;
$message->addTo($row->email)
->addTo('xxxxx#hotmail.com', 'eee#web.com')
->addFrom('xxxxx#gmail.com')
->setSubject('Invitation for the event : Event Latino');
}
// Setup SMTP transport using LOGIN authentication
$transport = new SmtpTransport();
$options = new SmtpOptions(array(
'host' => 'smtp.gmail.com',
'connection_class' => 'login',
'connection_config' => array(
'ssl' => 'tls',
'username' => 'xxxxx#gmail.com',
'password' => 'xxxxxx'
),
'port' => 587,
));
$html = new MimePart('Invitation for the event: Latin Night, orgonized by Mr. Jony Cornillon. Date : 06/04/2015');
$html->type = "text/html";
$body = new MimeMessage();
$body->addPart($html);
//$body->setParts(array($html));
$message->setBody($body);
$transport->setOptions($options);
$transport->send($message);
}
}
}
}
And this is the error displayed each time:
5.1.2 We weren't able to find the recipient domain. Please check for any
5.1.2 spelling errors, and make sure you didn't enter any spaces, periods,
5.1.2 or other punctuation after the recipient's email address. cj9sm2642949wjc.42 - gsmtp
Any help please. Thanks.
I am currently doing a reset password of which is sending the following link to a user in an email
http://localhost/realestateagencyadministration/users/reset/859be257b603ce278c42dca470be642a/48/
Then the user goes to the form and the controller does the following
public function reset($resetkey = null, $id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
$result =$this->User->find('first', array('conditions' => array('User.id' => $id, 'User.resetKey' => "'" . $resetkey . "'")));
if ($result) {
$message = __('<b>Please check your reset link</b>');
$this->Session->setFlash($message);
}
if ($this->request->is('post')) {
if ($this->User->save()) {
$message = __('Your password has been reset');
$this->Session->setFlash($message);
} else {
$message = __('Something has gone wrong. Please try later or <b>sign up again</b>');
$this->Session->setFlash($message);
}
}
else {
$this->request->data = $this->User->findByIdAndResetkey( $id, $resetkey );
$log = $this->User->getDataSource()->getLog(false, false);
debug($log);
}
}
The problem is when I submit it gives an error like so
Error: The requested address
'/realestateagencyadministration/users/reset/48' was not found on this
server.
How should I handle this?
The answer that I have found be it correct or not and if anyone has suggestions on how it should be altered do comment.
I took skywalker's suggestion and switched around the params so that id is first.
public function reset($id = null,$resetkey = null) {
and changed my save code
if ($this->request->is(array('post', 'put'))) {
$data = $this->User->findById($id);
$data['User']['password'] = $this->request->data['User']['password'];
$data['User']['resetkey'] = '';
if ($this->User->save($data)) {
$message = __('Your password has been changed, try logging in with your new password.');
$this->Session->setFlash($message);
} else {
$message = __('Something has gone wrong. Please try again later.');
$this->Session->setFlash($message);
}
}
I know it isn't clean and if anyone knows a better way it would be gratefully appreciated.
Try this:
[1] Remove trailing slash (/):
http://localhost/realestateagencyadministration
/users/reset/859be257b603ce278c42dca470be642a/48
[2] If still no success, add router statement (app/Config/routes.php), :
Router::connect('/users/reset/:resetkey/:id',
array('controller' => 'users', 'action' => 'reset'),
array('pass' => array('resetkey', 'id'))); // <-- must be in action argument order
I am creating a site in Drupal 7. I am using user registration through admin approval. After admin approval I want to send user password in user mail, but going username and only "Your password" static text.
Anybody can help me.
Thanks in advance
Check out this post and more precisely the comment #16:
/**
* Implements hook_token_info().
* */
function modulename_token_info() {
$info['tokens']['user']['password'] = array(
'name' => t('Password'),
'description' => t('The password by the user'),
);
return $info;
}
/**
* Implements hook_tokens().
* */
function modulename_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
$url_options = array('absolute' => TRUE);
if (isset($options['language'])) {
$url_options['language'] = $options['language'];
}
if ($type == 'user' && !empty($data['user'])) {
$account = $data['user'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'password':
$replacements[$original] = $account->password;
break;
}
}
}
return $replacements;
}
After that, in your /admin/config/people/accounts you'll find the token for the new user's password [user:password]