This is my html template
Dear ##name##(##email##),
Thank you for contacting us.
I want to replace ##name## and ##email## with the receiver's name and email of the person who gets it which will be provided in the array. How do I do it?
This is what I've got so far
$to_email = array('a#example.com', 'b#example.com', 'c#example.com');
$to_name = array('apple', 'ball', 'cat');
$Email = new CakeEmail();
$Email->from($from);
$Email->to($to_email );
$Email->subject($subject);
$Email->emailFormat('html');
$Email->viewVars(array('data' => $body));
$Email->template('bulk');
$Email->send();
You should start with creating template for your email, which will be including your current content (I use name example_template.ctp in my samples below):
Dear <?php echo $name; ?> <?php echo $email; ?>,
Thank you for contacting us.
Then you have to modify way of setting up your viewVars() and template():
$Email->viewVars(array('email' => $email, 'name' => $name));
$Email->template('example_template');
There is also required to change way of sending emails to loop over emails instead of sending all recipients in one field. So combine your input arrays into one, e.g.:
$emails = array(
'a#example.com' => 'apple',
'b#example.com' => 'ball',
'c#example.com' => 'cat'
);
Then just foreach over your array and send mails:
$Email = new CakeEmail();
foreach ($emails as $email => $name) {
$Email->from($from);
$Email->to($email);
$Email->subject($subject);
$Email->emailFormat('html');
$Email->viewVars(array('email' => $email, 'name' => $name));
$Email->template('example_template');
$Email->send();
$Email->reset(); // for cleaning up CakeEmail object
}
Related
I'm trying to create an email with php in Dynamics 365 by using the AlexaCRM php-crm toolkit after someone fills in the form on our website.
The email also appears in Dynamics, but the from and to fields are empty. The email itself and the subject are stored in Dynamics.
Does anyone had the same problem or does anyone knows what I'm doing wrong?
This is the code I'm using.
<?php
require_once '../vendor/autoload.php';
use AlexaCRM\CRMToolkit\Client as OrganizationService;
use AlexaCRM\CRMToolkit\Settings;
use AlexaCRM\CRMToolkit\Client;
use AlexaCRM\CRMToolkit\Entity\MetadataCollection;
use AlexaCRM\CRMToolkit\Entity\EntityReference;
$options = [
'serverUrl' => 'xxx.dynamics.com',
'username' => 'xxx#xxx.com',
'password' => 'xxxxxx',
'authMode' => 'OnlineFederation',
];
$serviceSettings = new Settings( $options );
$service = new OrganizationService( $serviceSettings );
$email = $service->entity( 'email' );
$email->subject = 'TEST SUBJECT';
$email->description = 'TEST EMAIL';
$email->sender = 'Sender Name';
$email->from = 'test#gmail.com';
$email->to = 'Our Company';
$email->torecipients = 'test#ourcompany.com';
$emailId = $email->create();
?>
Thanks for your help.
I created activityparties, but it still doesn"t write the email correctly to Dynamics.
I don't know how to combine the two parties.
This is my code:
$to = $service->entity( 'activityparty' );
$to->partyid = new EntityReference( 'systemuser', ''.$guid_stassen.'');
$from = $service->entity( 'activityparty' );
$from->partyid = new EntityReference( 'contact', ''.$guid.'');
$email = $service->entity( 'email' );
$email->subject = 'TEST SUBJECT';
$email->description = 'TEST EMAIL';
$email->from = ''.$from.'';
$email->to = ''.$to.'';
$emailId = $email->create();
rather than
$email->from = ''.$from.'';
$email->to = ''.$to.'';
you will have to use email_activity_parties as an array. I am not familair with PHP but something like below
$email->email_activity_parties=[
{
"partyid_systemuser#odata.bind" : "/systemusers(CED2E02D-188E-4AA8-B6E2-D746E9B370C1)",
"participationtypemask" : 1
},
{
"addressused":"vvyas#cloudfronts.com",
"participationtypemask" : 2
}
];
This happens because To and From field is not text field rather it is Party list field.
you cannot directly add/put email address, you will need to create object with it's type and email address.
Take a look at this blog, it has all the information you need.
Below sample for To and from, but here these are users in system.
"email_activity_parties" : [
{
"partyid_systemuser#odata.bind" : "/systemusers(CED2E02D-188E-4AA8-B6E2-D746E9B370C1)",
"participationtypemask" : 1 ///From Email
},
{
"partyid_account#odata.bind" : "/accounts(69C38067-EDB7-E811-A961-000D3A363C81)",
"participationtypemask" : 2 ///To Email
}]
Creating Email with unresolved emails (To field of email is not record in MS CRM).
"email_activity_parties" : [
{
"partyid_systemuser#odata.bind" : "/systemusers(CED2E02D-188E-4AA8-B6E2-D746E9B370C1)",
"participationtypemask" : 1 ///From Email
},
{
"addressused":"vvyas#cloudfronts.com",
"participationtypemask" : 2 ///To Email
}
]
I an setting up a website to send SMS automatically by API key Nexmo. But whene I add my variables into the Nexmo code, this one not work. How can I add my variables please?
I added my php variables to the Nexmo SMS default code but no result, but whene I trayed there code the stuff work fine
my file phone.php , with $row["phone_number"]=212981416807 at this exemple
$text1 = "Hello";
$text2 = " this is my company";
$MyNexmoID_Account = "3896321";
$MyNexmoAPI_Key = "yhg784frds78jkim";
$to = $row["phone_number"];
$from = "my company";
$text = "$text1 $text2";
// Code to Send SMS with Code Recharge ------
require_once "vendor/autoload.php";
//composer require nexmo/client;
$basic = new \Nexmo\Client\Credentials\Basic('$MyNexmoID_Account', '$MyNexmoAPI_Key');
$client = new \Nexmo\Client($basic);
$message = $client->message()->send([
'to' => '$to',
'from' => '$from',
'text' => '$text'
]);
if ($message && $client && $basic){echo " Recharge Code Sent Correctlly.";}else{echo "Failed! Recharge Code Not Sent.";}
// End Code to Send SMS with Code Recharge -----
This is default Nexmo code:
require_once "vendor/autoload.php";
//composer require nexmo/client;
$basic = new \Nexmo\Client\Credentials\Basic('3896321', 'yhg784frds78jkim');
$client = new \Nexmo\Client($basic);
$message = $client->message()->send([
'to' => '212981416807',
'from' => 'Nexmo',
'text' => 'Hello Nexmo'
]);
You don't need to quote variables. Try this:
$message = $client->message()->send([
'to' => $to,
'from' => $from,
'text' => $text
]);
Not sure if you have corrected this but do check your country code, I had a similar error with variable (also do listen to Michael Heap), where the hard code for the US phone number was 19091234567 and my database number was 9091234567. Unlike Twilio, Nexmo needs the country code. Hope it helps.
I am trying to send email to multiple recipient address in cake php 3.
my codes are :
$this->loadModel('AsIndividualDetails');
$EmailDetails = $this-> AsIndividualDetails->find('all',['fields'=>'email']);
$EmailDetails = $EmailDetails->toArray();
foreach ($EmailDetails as $key => $a) {
$this->loadModel('DomainEmailDetails');
$DomainEmailDetails = $this-> DomainEmailDetails->find('all')->first();
$DomainEmailDetails = $DomainEmailDetails->toArray();
$host = 'ssl://'.$DomainEmailDetails['host_name'];
$username = $DomainEmailDetails['user_name'];
$password = $DomainEmailDetails['user_password'];
$port = $DomainEmailDetails['port'];
$email_to = $a['email'];
$senderName = 'abc';
$email_id ='xyz110#gmail.com';
Email::configTransport('WebMail', [
'className' => 'Smtp',
'host' => $host,
'port' => $port,
'timeout' => 30,
'username' => $username,
'password' => $password,
'client' => null,
'tls' => null,
]);
////////// SEND MAIL
$email = new Email('WebMail');
$email ->template('default','default')
->emailFormat('both')
->from([$username => $senderName])
->to($email_to)
->replyTo($email_id)
->subject('Client Message');
$response = $email->send('My msg');
if($response){
echo 'success';
}else{
echo 'failed';
}
}
When I run this script just only one mail send successfully and after that an error has come :
Cannot modify an existing config "WebMail"
How to solve this error and send mail to all recipient mail address.
If you really need set the config inside a loop, you could delete it before rewrite the config:
use Cake\Mailer\Email;
Email::dropTransport($key);
See Class Email API for more info
Make your email configuration outside of the loop. You don't want to try and establish the configuration every time you send the emails - just one time. Then send all the emails based on that one configuration.
Does someone know how do I get the id of an email by name set on backend? I have an email created in backend in transactional emails that I want to send it programatically, but the id of it may differ depending on the instance that I'm on (local, live, stage), and I can only provide the same name for it.
I have this:
Mage::getModel('core/email_template')->sendTransactional(
$templateId,
$sender,
$recepientEmail,
$recepientName,
$vars,
$store);
And I need to find out $templateId and I only know that I saved the mail with name "Tests".
You can get email template:
$templateName = “Test”;
$emailTemplate = Mage::getModel('core/email_template')->loadByCode($templateName);
Get id:
$templateId = $emailTemplate->getId();
And then send email on your way:
Mage::getModel('core/email_template')->sendTransactional(
$templateId,
$sender,
$recepientEmail,
$recepientName,
$vars,
$store
);
or use "my" method:
$vars = array('key' => 'value');
$storeId = Mage::app()->getStore()->getStoreId();
$recipientEmail = 'some#email.com';
$recipientName = 'Some Name';
$emailTemplate->setSenderEmail(Mage::getStoreConfig('trans_email/ident_general/email', $storeId));
$emailTemplate->setSenderName(Mage::getStoreConfig('trans_email/ident_general/name', $storeId));
$emailTemplate->send($recipientEmail, $recipientName, $vars);
please check the codes below. any way this is my first answer on stackoverflow.
$templateName = "Tests";
$templateID = Mage::getModel('core/email_template')->loadByCode($templateName)->getId();
i have written this code for sending emails
Route::post('contact', function(){
$inputs = Input::all();
$rules = array(
'email' => 'required|email',
'name' => 'required|min:2',
'message' => 'required',
'recaptcha_response_field' => 'required|recaptcha',
);
$validator = Validator::make($inputs, $rules);
if($validator->passes()){
$fromEmail = Input::get('email');
$fromName = Input::get('name');
$subject = Input::get('subject');
$data = array('message' => Input::get('message'));
$toEmail = 'info#danielchikaka.com';
$toName = 'Daniel Chikaka';
Mail::send('emails.contact', $data, function($message) use ($toEmail, $toName, $fromEmail, $fromName, $subject){
$message->to($toEmail, $toName)->from($fromEmail, $fromName)->subject($subject);
});
return Redirect::to('/');
}
return Redirect::to('/#contact')->withInput()->withErrors($validator);
});
and my view emails.contact is
<html>
<body>
<p><b>Email From:</b> {{$fromName}} of {{$fromEmail}}</p>
<p><b>Subject:</b> {{$subject}}</p>
<b> Message:</b> <br>
</html> {{$data}}
</body>
but whenever i send email all i get is :
Email From:{{$fromName}} of {{$fromEmail}}
Subject: {{$subject}}
Message:
{{$data}
why my view does not pick up the data from Mail Class?
Make sure your view has the blade extension:
views/emails/contact.blade.php
Your $data array consists of only the variable message. So, in your blade template only {{$message}} will work. Ensure that you are passing all the variables required in your view into the $data array.
$data = array(
'message' => Input::get('message'),
'fromName' => Input::get('name'),
'fromEmail'=> Input::get('email')
);
Now, in your contact.blade.php you can use them as normal variables {{$formName}}, {{$fromEmail}} etc;
Also, since you are using blade templating, ensure that your view file is having proper extension i.e contact.blade.php
i have found a solution to my problem:
$fromEmail = Input::get('email');
$fromName = Input::get('name');
$subject = Input::get('subject');
$data = array('content' => Input::get('message'));
$toEmail = 'info#danielchikaka.com';
$toName = 'Daniel Chikaka';
Mail::send('emails.contact', $data, function($message) use ($toEmail, $toName, $fromEmail, $fromName, $subject){
$message->to($toEmail, $toName)->from($fromEmail, $fromName)->subject($subject);
});
and in the view has to be:
<html>
<body>
{{$content}}
</html>
</body>
Important:
Never use {{$message}} in contact view file, according to Taylor Otwell, that is reserved in email view files
Hope this will help someone one day!