I can not send mail in drupal-7 - drupal-7

I began to learn about drupal, I perform the function send mail but failed. Please could help me:
$params = array(
'subject' => 'hello',
'body' => 'test',);
$from='nguyen.xuan.luan#vinicorp.com.vn';
$to = 'nguyen.xuan.luan#vinicorp.com.vn';
$mail = drupal_mail('exampe', 'notice', $to, language_default(), $params, $from, TRUE);
error message: Unable to send e-mail. Contact the site administrator if the problem persists.
I think that must have information of mail password but I do not know how. Please can help me?

You need to create one hook i.e. hook_mail in your .module file and set message subject and body there. Below is the working hook_mail() implementation for your code -
function exampe_mail($key, &$message, $params) {
switch($key) {
//switching on $key lets you create variations of the email based on the $key parameter
case 'notice':
$message['subject'] = t('Subject');
//the email body is here, inside the $message array
$message['body'][] = 'This is the body of the email';
break;
}
}

Related

Google App Engine Send Grid PHP attachment

I am trying to add attachments as shown on https://github.com/sendgrid/sendgrid-google-php. But its not working by this way. I think I tried every possible solution but cant make this work. Here is my code.
<?php
require 'SendGrid_loader.php';
// Connect to your SendGrid account
$sendgrid = new SendGrid\SendGrid('myusername', 'mypassword');
// Make a message object
$mail = new SendGrid\Mail();
// Mail arrayi
$emails = array("mailadress1#test.com","mailadress2#test.com");
$names = array("name1", "name2");
// Add recipients and other message details
$mail->setTos($emails)->
setFrom('testsender#test.com')->
setFromName('Test Sender')->
setReplyTo('testemail#test.com')->
setSubject('Test')->
addAttachment("test.jpg")->
addCategory("TEST-GONDERIM")->
addUniqueArgument("BASIN", "YEREL-BASIN")->
addSubstitution("%name%", $names)->
setText('TEXT BODY MESSAGE')->
setHtml('<strong>%name% MERHABA,</br>BODY MESSAGE</strong>');
// Use the Web API to send your message
$sendgrid->send($mail);
?>
I tried to put test.jpg file on the same folder with this php file. Also tried to add like gs://bucket_name/test.jpg but not working. Any ideas. Thanks in advance
Solved with using web api v2 Curl version like this
$fileName = 'filename.pdf';
$image_data = file_get_contents('gs://my-bucket/filename.pdf');
sending part
$params = array(
'api_user' => $user,
'api_key' => $pass,
'x-smtpapi' => json_encode($json_string),
'to' => 'email#yourdomain.com',
'subject' => 'your subject',
'html' => 'testing body',
'text' => 'testing body',
'from' => 'yourmail#yourdomain.com',
'files['.$fileName.']' => $image_data
);
$request = $url.'api/mail.send.json';

how to send an email to a user in cakephp 2.x

I will greatly appreciate with all my heart if an expert would help me on how to send an email to a user.
am building a registration system. after a user successfully applies for registration, the admin must approve and at the click of the approve button, an email is send to the user and user details are saved in the approved table.
Here is the approve action in the applicationsController.
public function approve($student_id = null) {
if ($this->request->is('post'))
$application = $this->Application->findById($student_id);
$approved['Approved'] = $application['Application'];
$approved['Approved']['student_id'] = $approved['Approved']['student_id'];
$status = array('Application.status' => 'approved');
unset($application['Application']['id']);
unset($application['Application']['receipts']);
$this->loadModel('Approved');
$this->Approved->create();
if ($this->Approved->save($approved)) {
if ($this->Approved->saveField('status', 'approved')){
$this->Session->setFlash(__('The student has been approved'));
$email=$this->request->data['Application']['email'];
$this->Email->to = $email;
$this->Email->subject = 'Registration request approval';
$this->Email->from = 'ernestmwesha#gmail.com';
$this->Email->template = 'template';
$this->Email->smtpOptions = array(
'port' => '465',
'timeout' => '30',
'host' => 'ssl://smtp.gmail.com',
'username' => 'ernestmwesha#gmail.com',
'password' => 'mweshaernest',
);
$this->Email->delivery = 'smtp';
if($this->Email->send()){
return true;
}
else{
echo $this->Email->smtpError;
}
$this->Application->delete($student_id);
$this->redirect(array('action' => 'index')); }
} else {
$this->Session->setFlash(__('The student could not be approved.'));
}
$this->set('title_for_layout', 'Approved Requests');
}
after clicking the approved button i get the following error:
Notice (8): Undefined index: Application [APP\Controller\ApplicationsController.php, line 120]
You need to specify at least one destination for to, cc or bcc.
Error: An Internal Error Has Occurred.
.....bot the student gets approved and placed in the approved table
Review u2460470's answer to point you in the right direction for generating emails with CakePHP.
Make sure you have a mail server setup to handle the processing of emails. You might already have one setup locally, something like SquirrelMail, or you may prefer to use a managed, hosted provider (like Gmail). You can find examples of configuring CakePHP to send mail through Gmail in the CakeEmail documentation.
I've had great experiences using Postmark to handle transactional emails. There is a nice plugin, maurymmarques/postmark-plugin, you can use to easily setup Postmark for your CakePHP app.
// in your controller
App::uses('CakeEmail', 'Network/Email');
function somrthing () {
$Email = new CakeEmail();
$Email->from(array('me#example.com' => 'My Site'));
$Email->to('you#example.com');
$Email->subject('About');
$Email->send('My message');
}
Have a look CakeEmail in CakePHP 2.x

Debugging CakeEmail, msg sent but not received

I'm using CakePHP 2.1.1 and trying to send a simple email with form data. This is the code I have in my controller:
App::uses('CakeEmail', 'Network/Email');
try {
$Email = new CakeEmail();
$Email->from(array('support#pcn.com' => 'PC'));
$Email->to('me#gmail.com');
$Email->subject('New Registration');
if ( $Email->send(serialize($formData)) )
$this->set('email', 'Success');
else
$this->set('email', 'Failure without Exception');
}
catch ( Exception $e ) {
$this->set('email', 'Failure with Exception );
}
When I print out the value of 'email' in my view, it is always 'Success', but I never receive the email (ive checked the spam folder). There are no email related issues in the error log. How do I debug this problem and get the email to send properly?

Issue with registration function using email template layout - cakephp

A strange one to be honest. I have got this working completely fine on my local machine but on the production server (CENTOS), the redirect after registering is hitting the email template..
Is there something I am missing here?
function register() {
$this->layout = 'login';
$this->set('title_for_layout', 'Register');
if(!empty($this->data)) {
$this->User->create();
if($this->User->save($this->data)) {
$this->data['User']['group_id'] = 4;
$this->_sendNewUserMail( $this->User->id );
$this->redirect(array('action' => 'approval'));
} else {
$this->Session->setFlash(__('There were errors found in your registration. Please check the highlighted fields', true));
}
}
}
function _sendNewUserMail($id) {
$this->Email->smtpOptions = array(
'port'=>'25',
'timeout'=>'30',
'host' => 'localhost',
'username'=>'username',
'password'=>'password',
);
$this->Email->delivery = 'smtp';
$User = $this->User->read(null,$id);
$this->Email->to = array('someone#blah.com');
$this->Email->subject = 'A new registration has been submitted';
$this->Email->from = 'Me#blah.com';
$this->Email->template = 'default';
$this->Email->sendAs = 'html';
$this->set('User', $User);
$this->Email->send();
}
I am completely stumped with this..
Many thanks for your help in advance!
I would guess php_openssl is not enabled. Also, you should move the smtpOptions and delivery setting into _sendNewUserMail
[From the comments]
It seems that there was an error in the production server (maybe due to a different configuration in the SMTP server) I assume that in your production server you have the debug set to 0. So the errors are not displayed, That's why instead of redirecting, it renders the email template.
You'll need to debug the smtp errors in production. To do that, you could add:
Configure::write('debug', 1);
in the action/controller that sends the email, so you'd be able to see the error. Also, you could check the smtp errors in the $this->Email->smtpError variable.
That way you'll be able to see what's wrong. I don't know much about smtp server's configurations so that might be another S.O. question.
Hope this helps

CakePHP + Facebook

I am trying to implement facebook Connect to my cakephp Application. i am using Nick's Facebook Plugin.
I wanna implement it this way
When a user Visits the Site he should be able to login via Registration on the site or Facebook Connect
Existing users should be able to connect their account to their FB account
People who first time login to the site using FB Connect and dont have an account on the site. should be redirected to a page where they have to enter details to complete the profile.
What i have done -
I have followed the instruction of Nick to implement it and when i click Login - it connects to my app. but i dont understand how to create a username and password associated with the Fb Connect Id. and user it against the FB token.
Apparently I'm doing the same thing a little before you... ;-)
Here's a method for Facebook login I'm using (slightly redacted and annotated):
public function facebook($authorize = null) {
App::import('Lib', 'Facebook.FB');
$Fb = new FB();
$session = $Fb->getSession();
// not logged into Facebook and not a callback either,
// sending user over to Facebook to log in
if (!$session && !$authorize) {
$params = array(
'req_perms' => /* the permissions you require */,
'next' => Router::url(array('action' => 'facebook', 'authorize'), true),
'cancel_url' => Router::url(array('action' => 'login'), true)
);
$this->redirect($Fb->getLoginUrl($params));
}
// user is coming back from Facebook login,
// assume we have a valid Facebook session
$userInfo = $Fb->api('/me');
if (!$userInfo) {
// nope, login failed or something went wrong, aborting
$this->Session->setFlash('Facebook login failed');
$this->redirect(array('action' => 'login'));
}
$user = array(
'User' => array(
'firstname' => $userInfo['first_name'],
'lastname' => $userInfo['last_name'],
'username' => trim(parse_url($userInfo['link'], PHP_URL_PATH), '/'),
'email' => $userInfo['email'],
'email_validated' => $userInfo['verified']
),
'Oauth' => array(
'provider' => 'facebook',
'provider_uid' => $userInfo['id']
)
);
$this->oauthLogin($user);
}
This gives me an array with all the user details I could grab from Facebook and invokes ::oauthLogin, which either logs the user in with the given information or asks the user to fill in missing details and/or creates a new user record in the database. The most important part you get from the Facebook API is the $userInfo['id'] and/or email address, either of which you can use to identify the user in your database. If you're using the AuthComponent, you can "manually" log in the user using $this->Auth->login($user_id), where $user_id is the id of the user in your own database.
private function oauthLogin($data) {
$this->User->create();
// do we already know about these credentials?
$oauth = $this->User->Oauth->find('first', array('conditions' => $data['Oauth']));
if ($oauth) {
// yes we do, let's try to log this user in
if (empty($oauth['User']['id']) || !$this->Auth->login($oauth['User']['id'])) {
$this->Session->setFlash('Login failed');
}
$this->redirect('/');
}
// no we don't, let's see if we know this email address already
if (!empty($data['User']['email'])) {
$user = $this->User->find('first', array('conditions' => array('email' => $data['User']['email'])));
if ($user) {
// yes we do! let's store all data in the session
// and ask the user to associate his accounts
$data['User'] = array_merge($data['User'], $user['User']);
$data['Oauth']['user_id'] = $user['User']['id'];
$this->Session->write('Oauth.associate_accounts', $data);
$this->redirect(array('action' => 'oauth_associate_accounts'));
}
}
// no, this is a new user, let's ask him to register
$this->Session->write('Oauth.register', $data);
$this->redirect(array('action' => 'oauth_register'));
}
Look no further. Here is an excellent article that'll guide you all the way through (minus any readymade plugins):
Integrating Facebook Connect with CakePHP's Auth component
Simply follow the approach described in there.
Cheers,
m^e

Resources