Google App Engine Send Grid PHP attachment - google-app-engine

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';

Related

Cakephp not sending attachment, only send plain html

I am trying to send an attachment with cakephp email, but only plain html sent and not a attachment.
The following code I am using.
$nmessage ="Hello Test\r\n\r\n";
$email = new CakeEmail();
$email->from(array('abc#example.com' => 'Test'));
$email->filePaths = array('/screenshots/');
$email->attachments =array('Google-Maps-9.22.2.jpg');
$email->to('user4#gmail.com');
$email->subject('Register a visit ');
$email->emailFormat('html');
$email->send($nmessage); // or use a template etc
To send attachments you can do it the following ways, first a string with the full path (notice there is no equals symbol, it's a function of the CakeEmail class).
$email->attachments('/full/file/path/file.jpg');
Secondly is the same but wrapped in an array
$email->attachments(array('/full/file/path/file.png'));
Thirdly an array with keys to rename the file
$Email->attachments(array('photo.png' => '/full/some_hash.png'))
And finally you can use nested arrays
$email->attachments(array(
'photo.png' => array(
'file' => '/full/some_hash.png',
'mimetype' => 'image/png',
'contentId' => 'my-unique-id'
)
));
So in summary, don't use $email->attachments = and make sure to provide the full path.
http://book.cakephp.org/2.0/en/core-utility-libraries/email.html#sending-attachments

I can not send mail in 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;
}
}

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

Render view to variable in CakePHP 1.3 (to generate pdf and download file)

I'm trying to render a view to a variable.
This variable will then be used to generate a pdf.
Then that pdf should be downloaded with the Media view.
Here's my controller code:
$dir = ROOT . '/app/tmp/evaluationpdf/';
$path = $dir . $evaluationid . '.pdf';
$evaluation = $this->SelfEvaluation->find('first', array(
'conditions' => array('SelfEvaluation.id' => $evaluationid),
'contain' => array('Submission' => array('Application'), 'Applicant', 'Member')));
$this->set(compact('evaluation'));
$this->output = '';
$this->layout = false;
$html = $this->render('/elements/self_evaluation_pdf');
$this->_generate_pdf($html, $path);
$this->view = 'Media';
$params = array(
'id' => $evaluationid . '.pdf',
'name' => $evaluationid,
'download' => true,
'extension' => 'pdf',
'path' => $dir,
);
$this->set($params);
The file is created as it should, but the first '$this->render' output is also sent to the browser.
The file is never downloaded.
Any idea on how to fix this?
The simple fix is to just set $this->output to '' after your first render() call.
The more correct way is to use requestAction() instead of render().
In CakePHP 2.x I did the following in order to use a view to generate a barcode label format:
$response = $this->render('/Labels/' . $printer['Printer']['model'] . '/manifest', 'ajax');
$body = $response->body();
$response->body('');
Which gave me the view data as $body. Then I could just redirect or if the request was ajax just set autoRender to false and return ''.
It kind of muddies the MVC waters but it is simple.
You just have to write the code below in self_evaluation_pdf.ctp
header("Content-Disposition: attachment; filename='downloaded.pdf'");
header('Content-type: application/pdf');
The dynamic content in this view will be downloaded as a PDF file on the client side.

CakePHP page with no headers/footers

In a download page for a blob from a database, how would I make it so that no other output is sent? Right now it's sending the header, debug info, and a footer. How do I make it so that none of that is sent, just for that view?
you can create an clear layout (e.g. empty.ctp ) in you layouts folder, only with
<?php echo $content_for_layout ?>
and then in you action where you're getting your blob data use that layout
$this->layout = 'empty.ctp';
and also to disable debugging, in your controllers use
Configure::write('debug',0);
if you're unable to create new layout you could try this.
$this->layout = null;
$this->render("view_name");
If you're using this to download files, you should use the Media view in cakePHP
http://book.cakephp.org/view/1094/Media-Views
$this->view = 'Media';
$params = array(
'id' => 'example.zip',
'name' => 'example',
'download' => true,
'extension' => 'zip', // must be lower case
'path' => APP . 'files' . DS // don't forget terminal 'DS'
);
CakePhp 2.3 users :
use Sending files from the Book
CakePhp 2.x users :
use '$this->viewClass' instead of '$this->view'
copy-paste ready full solution, right in any controller file:
<?php
public function download($file) {
$fsTarget = APP.WEBROOT_DIR.DS.'files'.DS.$file; // files located in 'files' folder under webroot
if (false == file_exists($fsTarget)){
throw new NotFoundException(__('Invalid file'));
}
$pathinfo = pathinfo($fsTarget);
$this->viewClass = 'Media';
$params = array(
'id' => $file,
'name' => $pathinfo['filename'], // without extension
'download' => true,
'extension' => $pathinfo['extension'], // must be lower case
'path' => dirname($fsTarget) . DS // don't forget terminal 'DS'
);
$this->set($params);
}
Hope this helps!

Resources