SendGrid SMTP API for Applying Template - php

I'm trying to apply the sendgrid templates by building the SendGrid X-SMTPAPI headers in my index.php.
This is an abstract of my index.php code:
// Start Sendgrid
$sendgrid = new SendGrid('userid', 'pwd');
$header = new Smtpapi\Header();
$filter = array(
'templates' => array(
'settings' => array(
'enabled' => 1,
'template_id' => 'f2c99ace-87af-2618-8390-c19e2ad2805f'
)
)
);
$header->setFilters($filter);
$emailwelcome = new SendGrid\Email();
$emailwelcome->addTo($email)->
setFrom('hello#world.com')->
setFromName('Welcome to World')->
setSubject('Your Invitation')->
setText('Hello World!')->
setHtml('<strong>Hello World!</strong>');
$sendgrid->send($emailwelcome);
To apply the templates, as copied from the SendGrid documentation:
"Enabling a Template
To use a Template Engine template when you send, enable the templates filter and set the template_id to one of your Template Engine templates.
Example
{
"filters": {
"templates": {
"settings": {
"enable": 1,
"template_id": "5997fcf6-2b9f-484d-acd5-7e9a99f0dc1f"
}
}
}
}
You can use this JSON in the X-SMTPAPI header of an SMTP message, or in the x-smtpapi parameter of a mail.send API call."
Question is... where is the X-SMTPAPI header of my SMTP message? In my composer.json or index.php or in the vendor files?
Thanks for your help.

Okay, so apparently. All you need to do is add these 2 lines of code in the index.php:
addFilter('templates', 'enable', 1)->
addFilter('templates', 'template_id', 'f2c99ace-87af-3618-8390-c19e2ad2805f');
Weeeeeeeee~

Related

Codeigniter 4 Email sending with HTML as message

can you help me figure out a way to use the email class of CI4 to send an email with an HTML as the message?
in Codeigniter 3 it is simple and goes like this:
$email->message($this->load->view('html_page_to_send_from_view_folder',$data,true));
but in Codeigniter 4 I tried doing this:
$email->setMessage(echo view('html_page_to_send_from_view_folder',$data,true));
It gives out an error:
syntax error, unexpected echo(T_ECHO), expecting ')'
I also tried putting the view in a variable like so:
$echo_page = echo view('html_page_to_send_from_view_folder',$data,true);
$email->setMessage($echo_page);
but it just throws the same error, I was searching all over the internet and the documentation but couldn't find a way to do this. Help please.
I tried this but got no error, and also got no email :'(
$echo_page = view('html_page_to_send_from_view_folder',$data,true);
$email->setMessage($echo_page);
According to this, if you want to use some template as your message body, you should do something like this:
// Using a custom template
$template = view("email-template", []);
$email->setMessage($template);
CodeIgniter 4 documentation states:
setMessage($body)
Parameters: $body (string) – E-mail message body
Returns: CodeIgniter\Email\Email instance (method chaining)
Return type: CodeIgniter\Email\Email
Sets the e-mail message body:
$email->setMessage('This is my message');
Okay I got it now I made it work by adding this code $email->setNewLine("\r\n"); at the end just after the setMessage:
$email->setMessage($my_message);
$email->setNewLine("\r\n");
and also I set the SMTP port 587 instead of 465:
$config['SMTPPort']= 587;
ALSO, for the setMessage, I did it like this:
$my_message = view('html_page_to_send_from_view_folder',["id" => $data['id']]);
$email->setMessage($my_message);
really weird man....
A. Firstly,
Instead of:❌
$echo_page = echo view('html_page_to_send_from_view_folder',$data,true);
Use this:✅
$echo_page = view('html_page_to_send_from_view_folder',$data);
Notice the luck of an echo statement and not passing true as the third argument of the view(...) hepler function.
B. Secondly, to submit HTML-based emails, ensure that you've set the mailType property to html. This can be achieved by using the setMailType() method on the Email instance. I.e:
$email->setMailType('html');
Alternatively, you could set the "mail type" by passing an array of preference values to the email initialize() method. I.e:
public function sendMail(): bool
{
$email = \Config\Services::email();
$email->initialize([
'SMTPHost' => 'smtp.mailtrap.io',
'SMTPPort' => 2525,
'SMTPUser' => '479d7c109ae335',
'SMTPPass' => '0u6f9d18ef3256',
'SMTPCrypto' => 'tls',
'protocol' => 'smtp',
'mailType' => 'html',
'mailPath' => '/usr/sbin/sendmail',
'SMTPAuth' => true,
'fromEmail' => 'from#example.com',
'fromName' => 'DEMO Company Name',
'subject' => 'First Email Test',
]);
$email->setTo('to#example.com');
$email->setMessage(view('blog_view'));
$response = $email->send();
$response ? log_message("error", "Email has been sent") : log_message("error", $email->printDebugger());
return $response;
}

How can i send docusign envelop to multiple recipient?

I have one question regarding docusign api. I am using docusign api for electronic-sign. In my requirement the recipient view does not show on the docusign website and only show on my website where user can do e-sign for the document. Basically i done this. My question start now. How can i add two recipient on the document. I try it with single recipient but not know how to use multiple recipient. Here in two recipient(person) one is me and other is my client who purchase product from me. When i send this envelop to my client. It will come first to me and then after my sign will go to my client side and wait for its sign. How can i do this priorities also?
require_once './docusign-php-client/src/DocuSign_Client.php';
require_once './docusign-php-client/src/service/DocuSign_RequestSignatureService.php';
require_once './docusign-php-client/src/service/DocuSign_ViewsService.php';
$clientConfig = array(
// Enter your Integrator Key, Email, and Password
'integrator_key' => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 'email' => "xxxxxxxxxxxxxxxxxxxxxx", 'password' => "xxxxxxxxxxxxxxxxxx",
// API version (v2 is latest) and environment (i.e. demo, www, etc)
'version' => 'v2', 'environment' => 'demo'
);
// Instantiate client and call the Login API
$client = new DocuSign_Client($clientConfig);
// create service object and configure envelope settings, document(s), and recipient(s)
$service = new DocuSign_RequestSignatureService($client);
$emailSubject = "Please sign this document.";
$emailBlurb = "This is a document from Developer who test this docusign app. I would like to work with this.";
// add one signHere tab for the recipient located 100 pixels right and
// 150 pixels down from the top left corner of document's first page
$tabs1 = array( "signHereTabs" => array(
array( "documentId" => "2",
"pageNumber" => "1",
"xPosition" => "450",
"yPosition" => "233" )));
// $tabs2 = array( "signHereTabs" => array(
// array( "documentId" => "2",
// "pageNumber" => "1",
// "xPosition" => "130",
// "yPosition" => "233" )));
$signed_document_id = time();
// add a recipient and document to the envelope
$recipients = array( new DocuSign_Recipient( "1", "1", "I am", "my-email#my-email.in", $signed_document_id, 'signers', $tabs1) );
$documents = array( new DocuSign_Document("TEST.PDF", "1", file_get_contents("BCD.pdf")) , new DocuSign_Document("TEST.PDF", "2", file_get_contents("ABC.pdf")) );
// "sent" to send immediately, "created" to save as draft in your account
$status = 'sent';
//*** Create and send the envelope with embedded recipient
$response = $service->signature->createEnvelopeFromDocument( $emailSubject, $emailBlurb, $status, $documents, $recipients, array() );
/**************************************************/
/* Step- 3 Embadded sign iframe */
/**************************************************/
$service = new DocuSign_ViewsService($client);
$envelopeId = $response->envelopeId;
$returnUrl = "https://demo.docusign.com;
$authMethod = "email";
$response = $service->views->getRecipientView( $returnUrl,
$envelopeId,
"i am",
"my-email#my-email.in",
$signed_document_id,
$authMethod );
$sign_url = $response->url;
Yes, you can have two different people sign a document via DocuSign, where both are signing within your application.
This is called "embedded signing."
Create the envelope (signing request) with both signers. You must supply a clientUserId field for both recipients.
The result of envelope create is an envelope_id. Use it with the getRecipientView method to obtain the url for the first signer.
Redirect the signer to the url to sign. After signing, the signer's browser is re-directed back to your application. You can use an iframe but it is not recommended if there is any chance that people will be signing via a mobile/tablet device.
Later on, the other signer will log into your application. At that time, you can repeat the previous steps to enable the second signer to sign.
Remember that when you enable someone to sign from within your application, your app is responsible for authenticating the signer.
More information: see the recipe for embedded signing.

Mandrill inbound emails through Laravel / PHP

I was wondering if someone could help me with a problem I have been having some trouble researching related to Laravel and inbound email processing through Mandrill.
Basically I wish to be able to receive emails through Mandrill and store them within my Laravel database. Now i'm not sure if i'm reading through the documentation with the wrong kind of eyes, but Mandrill says it deals with inbound email as well as outbound, however i'm starting to think that Mandrill deals with inbound email details as opposed to the actual inbound email, such as if the message is sent etc.
I've created a new Mandrill account, created an API key, created an inbound domain and corresponding subdomain of my site (e.g. inboundmail.myproject.co.uk), set the MX record and the MX record is showing as valid. From there i have set up a route (e.g. queries#inboundmail.myproject.co.uk), and a corresponding webhook (myproject.co.uk/inboundmail.php) and within this webhook tried a variety of the examples given in the API (https://mandrillapp.com/api/docs/inbound.php.html), such as adding a new route, checking the route and attempting to add a new domain. All of them worked and produced the correct results, so my authentication with Mandrill is not in question, but my real question is is there a specific webhook for dealing with accepting incoming mail messages?
I cant help but feel like an absolute idiot asking this question as i'm sure the answer is either staring me in the face or just not possible through Mandrill.
Thanks in advance.
Thanks to duellsy and debest for their help, in the end i found a script and expanded on it to add the mail to my own database and style / display it accordingly. Hope this helps someone who may have the same trouble:
<?php
require 'mandrill.php';
define('API_KEY', 'Your API Key');
define('TO_EMAIL', 'user#example.com');
define('TO_NAME', 'Foo Bar');
if(!isset($_POST['mandrill_events'])) {
echo 'A mandrill error occurred: Invalid mandrill_events';
exit;
}
$mail = array_pop(json_decode($_POST['mandrill_events']));
$attachments = array();
foreach ($mail->msg->attachments as $attachment) {
$attachments[] = array(
'type' => $attachment->type,
'name' => $attachment->name,
'content' => $attachment->content,
);
}
$headers = array();
// Support only Reply-to header
if(isset($mail->msg->headers->{'Reply-to'})) {
$headers[] = array('Reply-to' => $mail->msg->headers->{'Reply-to'});
}
try {
$mandrill = new Mandrill(API_KEY);
$message = array(
'html' => $mail->msg->html,
'text' => $mail->msg->text,
'subject' => $mail->msg->subject,
'from_email' => $mail->msg->from_email,
'from_name' => $mail->msg->from_name,
'to' => array(
array(
'email' => TO_EMAIL,
'name' => TO_NAME,
)
),
'attachments' => $attachments,
'headers' => $headers,
);
$async = false;
$result = $mandrill->messages->send($message, $async);
print_r($result);
} catch(Mandrill_Error $e) {
// Mandrill errors are thrown as exceptions
echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
// A mandrill error occurred: Mandrill_PaymentRequired - This feature is only available for accounts with a positive balance.
throw $e;
}
?>
Like using webhooks from other mail parsing services, you'll need to make use of
file_get_contents("php://input")
This will give you the raw data from the webhook, which you can then json_decode and work with the results.

How to apply template (tpl) file for email message body

I've this mail function in my custom module
function mymodule_mail($key, &$message, $params) {
switch ($key) {
case 'notification':
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
$message['subject'] = $params['subject'];
$message['body'] = t('<table style="border:2px solid black;"><tr><td>MESSAGE BODY </td><td><b>'.$params['msg'].'</b></td></tr></table>');
break;
}
}
Here you can clearly see that for message body i'm using some html tags.
Below code invoke the mail function, which is written in my block.
$params = array(
'subject' => 'email subject',
'msg' => 'message body',
);
drupal_mail('mymodule', 'notification', 'email address', language_default(), $params);
I want to know, is there any easy way to apply a template (.tpl.php) file for my message body so that i can put my all css styling within that tpl file.
Any suggestion would be greatly appreciated.
You'll need to set up a theme call for it
function mymodule_theme() {
$path = drupal_get_path('module', 'mymodule') . '/templates';
return array(
'mymodule_mail_template' => array(
'template' => 'your-template-file', //note that there isn't an extension on here, it assumes .tpl.php
'arguments' => array('message' => ''), //the '' is a default value
'path' => $path,
),
);
}
Now that you have that, you can change the way you're assigning the body
$message['body'] = theme('mymodule_mail_template', array('message' => $params['msg']);
The key message needs to match the argument you supplied in mymodule_theme(), which it does.
Now you can just create your-template-file.tpl.php in the module's templates/ folder (you'll have to make that) and you can use the variable $message in your template to do whatever you'd like. The variable name matches your theme argument name.
After your module is set up properly, make sure to flush the cache. I can't tell you how long it took me to realize that the first time I started working with Drupal, and how much time I wasted trying to fix non-existent bugs.
The HTML Mail module does just that :-)

How to make NuSOAP works with Symfony 1.4?

I'm trying to make my webservice in symfony with NuSOAP. I also made a client for test purpose.
I managed to make it work in my /web/ directory, but i can't access my symfony methods from there.
So i created a new module in my frontend app, and i copied the content of my nuSOAP server file into indexSuccess.php.
When i try to consume it, i get no error but also no results, and what's really strange is $proxy->response returning my homepage.
Here's my indexSuccess.php
require_once ("../lib/soap/nusoap.php");
$server = new soap_server();
$namespace = "Webservices";
$server->wsdl = new wsdl();
$server->wsdl->schemaTargetNamespace = $namespace;
$server->configureWSDL("Webservices", "Webservices");
function getDemandes($partnerCode)
{
$demandesArray = array();
$demandeArray[] = array( 'id' => 5, 'poid_id' => 25, 'demande_type' => "Male" );
$demandeArray[] = array( 'id' => 8,'poid_id' => 21, 'demande_type' => "Female");
return $demandeArray;
}
$server->register(
'getDemandes',
array('partnerCode' => 'xsd:string'),
array('getDemandesResponse'=>'tns:ArrayOfDemandesDatas'),
$namespace,
false,
'rpc',
'encoded',
'Return requests'
);
$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
$server->service($POST_DATA);
exit();
After further research I get the error Response not of type text/xml: text/html; charset=utf-8 whitch is not surprising because i have my default layout in the $request -> response, even if i disable it with $this->layout(false);
Maybe you should choose an option that is better integrated in Symfony. There is the ckWebservicePlugin for example. This plugin is tightly integrated in Symfony and would be a far better choice than using NuSOAP.

Categories