I am developing a Joomla!3.0/3.1 component that allows people to book a golf lesson online. When they complete the booking and process paypal payment, I want to send them a receipt/confirmation email.
I can do this fine and send the information in html or plain text format. However I don't like the fact that I now have view (email content) data within my controller code.
Is it possible to have a sort of template view file which can be parsed and sent as the contents instead? or am I stuck with it in my code?
Thanks,
Chris
On admin side create a section for editing your email template with Joomla's default editor
you an create an editor on the backend and open an html file inside that, for changing the styles ,formats content etc for users.
keep your template file as an HTML file on server and email sending time something like below.
function send_mail_account_pages($email_title,$greeting_text,$subject,$email_content,$user_email){
$config = JFactory::getConfig();
$data['fromname'] = $config->get('fromname');
$data['mailfrom'] = $config->get('mailfrom');
$data['sitename'] = $config->get('sitename');
$path = JPATH_BASE."/email_tmpl/";
$fp = fopen($path."email_tmpl.html","r");
$fsize = filesize($path."email_tmpl.html");
$fcontent = fread($fp,$fsize);
fclose($fp);
$date_format = date("M.d, Y");
$fcontent = str_replace("{email_page_title}",$email_title,$fcontent);
$fcontent = str_replace("{date}",$date_format,$fcontent);
$fcontent = str_replace("{greeting_text}",$greeting_text,$fcontent);
$fcontent = str_replace("{email_content}",$email_content,$fcontent);
$fcontent = str_replace("{thanks_text}",THANKS_TEXT,$fcontent);
$return = JUtility::sendMail($data['mailfrom'], $data['fromname'], $user_email, $subject, $fcontent,1);
}
This way you can make admin users to give access to change the template styles make sure admin users do not edit the {place holders}.
Hope its helps..
Add a configuration field to your component of type textarea or editor, and let the user write the email template. You should explain which tokens are available or at least provide a default text, such as
Dear {USERNAME},
today I received {NUM_MESSAGES} messages on your behalf,
and your profile has been viewed {NUM_PROFILEVIEWS} times.
Then simply replace the {TOKENS} in your code with str_replace.
Related
I want the script to send the created html email template email.html to the person who last submitted the form.
Below is the success message I get in the console:
Below is the actual email that is received by the person who submits the form:
The email.html is formatted correctly and appears perfectly when sent manually.
I hope this is reprex enough.
EDIT: Maybe it's easier if I include the code
function sendEmail () {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();
var data = ss.getRange(lr,3);
var email = data.getValue();
var message = HtmlService.createTemplateFromFile('email');
var subject = "Test Subject"
GmailApp.sendEmail(email,subject, message);
}
It's not clear why you are using HtmlService.createTemplateFromFile, but from the image it's clear there at least one error, the script misses two methods:
HtmlService.HtmlTemplate.evaluate() to evaluate the Templated HTML
HtmlService.HtmlOutput.getContent() to get the HTML from the HtmlService.HtmlOutput object returned by the previous method.
Another option that looks to be an error is the use of GmailApp.sendEmail(string,string,string) method, as the third parameter should be a string to be used as the email plain text content. If you want to pass HTML, instead use GmailApp.sendEmail(string,string,string, Object)
Related
Emailing Google Sheet range (with or without formatting) as a HTML table in a Gmail message
Sending an email in HTML and plain with a Gmail Apps Script
Google script inject html into html email
References
https://developers.google.com/apps-script/guides/html
https://developers.google.com/apps-script/guides/html/templates
https://developers.google.com/apps-script/reference/gmail/gmail-app#sendEmail(String,String,String)
I have built a cakePHP3 application and I'm looking for some advice concerning the following situation. An e-mail configuration is setup as follows, to send a mail whenever a new order has been created.
public function orderCreated($order, array $attachments = [])
{
$email = $this
->locale($order->customer->language)
->to($order->customer->email)
->from(Configure::read('Webshop.email'), Configure::read('Webshop.name'))
->subject(__('Confirmation of your order {0}', $order->nr))
->template('orders' . DS . 'order_created')
->set(['order' => $order])
->attachments($attachments);
return $email;
}
Has worked perfectly fine for ages, but I'd like to add some new functionality to this specific e-mail (and others). Admins should be able to override the content of the orders/order_created.ctp template if they want to, so they can define the content of this e-mail theirselves. So they don't have to rely on the same content in the order_created.ctp template I did provide.
Creating a UI for saving their own e-mails is not the issue. But the issue I don't really know how I could provide the overrided content to the cakePHP3 mailer. I tried setting
->template(false)
->message($new_content)
But that didn't help. Message doesn't reach mailbox because the body is empty.
Thanks.
I think I'd go with something like this:
->template('orders' . DS . 'order_created_custom')
->set(['order' => $order, 'content' => $new_content])
And then in the new order_created_custom.ctp you would output the $content. This gives you the option to do some text replacements on the content based on the $order, as well as perhaps having things like a standard salutation or signature.
As per the requirement I am sending a pdf file attachment to some email ids,I thought to include some static text to show in the email and i created one text template and added in the code ,but the text is not showing in the email.
here is the code
I used setTemplateId to show the template...
Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
attach.setContentType('application/pdf');
attach.setFileName(************************);
attach.setInline(false);
attach.Body = body;
Messaging.SingleEmailMessage mail = new
Messaging.SingleEmailMessage();
EmailTemplate et=[Select id from EmailTemplate where
name=:'CSPdfEmailtemplate'];
mail.setUseSignature(false);
mail.setToAddresses(Emailids);
mail.setTemplateId(et.id);//here i am giving the id of template
mail.setBccSender(true);
From the documentation a singleEmailMessage using a template needs to have the id of a contact, user or lead with setTargetObjectId(targetObjectId) so you will need to add that.
Why is there nowhere in the Configuration/System/Mail Sending Settings to specify a user name and password for your smtp server?
To get around this, do you need to make the changes to getMail() outlined in this post:
http://www.magentocommerce.com/boards/viewthread/1073/P30/
I want to do something very simple:
- create an e-mail template
- do not have to make reference to that template in any config files.
- programmatically send an e-mail using the template defined above
- supply values to replace any tags in the template
- supply recipient e-mail addresses
- supply other bits, like a from address
So first step - create a template.
- In Confguration/Transactional Emails I believe I am supposed to see a list of templates. I see nothing. But if I add a new template, I can select from a list of templates.
- Give template a name of "Bob".
- Add a few vars to the template:
myvar1={{var myvar1}}
myvar2={{var myvar2}}
- Save the template; it is given an Id of 1.
Now send the e-mail programmatically from a controller action:
- No need to make change to LINEEND in Mime.php as it is already set to \n in version 1.4.2.0
- Make changes to getMail() in Template.php as specified in this post: http://www.magentocommerce.com/boards/viewthread/1073/P30/
- Write code in the controller action to send the e-mail:
This returns nothing:
$emailTemplate = Mage::getModel('core/email_template')->loadDefault('no matter what goes here emailTemplate is not set');
This does return an email template:
$emailTemplate = Mage::getModel('core/email_template')->loadByCode('Bob');
but the call to send below fails:
$emailTemplate->setSenderEmail('sent#byme.com');
$emailTemplate->setSenderName('Steve');
$emailTemplateVariables = array();
$emailTemplateVariables['myvar1'] = 'TestValue1';
$emailTemplateVariables['myvar2'] = 'TestValue2';
// $processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables); -- this returns nothing
$emailTemplate->send('thisisme#mydomain.com','John', $emailTemplateVariables);
In the system.log I get the warning below, and no e-mail ever arrives.
Warning: stream_socket_enable_crypto() [<a href='streams.crypto'>streams.crypto</a>]: this stream does not support SSL/crypto in C:\Applications\Apache Software Foundation\Apache2.2\htdocs\magento\lib\Zend\Mail\Protocol\Smtp.php on line 206
Should I be using loadByCode? I wish there was some worthwhile documentation (the help for loadByCode is "Load template by code" !!). Should I be using send, sendTransactional? Oh for a bit of quality documentation.
Thanks
I see 2 questions here.
1. How to configure Magento mailing system to use smtp protocol ?
You are having trouble for this because Magento is made to use default host mailing. So it will search for it on the machine where it is installed.
If you want to configure a smtp server, I would recommend using this extension : http://www.magentocommerce.com/magento-connect/ziq2004/extension/460/advanced-smtp--artson.it
I found it simple to use and configure.
2. How to send a mail in your custom module
You can first create your template in Confguration/Transactional Emails, mark down the Id for it will be your identifier
Then, simply use this code to send the mail in your module
<?php
// The Id you just marked...
$templateId = 1;
// Define the sender, here we query Magento default email (in the configuration)
// For customer support email, use : 'trans_email/ident_support/...'
$sender = Array('name' => Mage::getStoreConfig('trans_email/ident_general/name'),
'email' => Mage::getStoreConfig('trans_email/ident_general/email'));
// Set you store
// This information may be taken from the current logged in user
$store = Mage::app()->getStore();
// In this array, you set the variables you use in your template
$vars = Array('my_var' => $my_var,
'another_var' => 12);
// You don't care about this...
$translate = Mage::getSingleton('core/translate');
// Send your email
Mage::getModel('core/email_template')->sendTransactional($templateId,
$sender,
'recipient#gmail.com',
'Recipient Name',
$vars,
$store->getId());
// You don't care as well
$translate->setTranslateInline(true);
?>
Hope this will help you
Regards,
I took out the 'ssl' => 'tls' element in the array in getMail() in Template.php and my e-mail came through.
I'd still appreciate if anyone has an explanation of how the smtp server's username and password should be specified, and an explanation of the differences in the template load methods etc would be most welcome!
If anyone is looking for full sample code of how to send a Magento email based on an existing Magento email template, the following works well. It does not require any XML config. You can load the template by name as well as by ID. In this case I load it by name.
// This is the name that you gave to the template in System -> Transactional Emails
$emailTemplate = Mage::getModel('core/email_template')->loadByCode('My Custom Email Template');
// These variables can be used in the template file by doing {{ var some_custom_variable }}
$emailTemplateVariables = array(
'some_custom_variable' => 'Hello World'
);
$processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);
$emailTemplate->setSenderName('Joe Bloggs');
$emailTemplate->setSenderEmail('test#test.com');
$emailTemplate->setTemplateSubject("Here is your subject");
$emailTemplate->send('recipient#test.com', 'Joanna Bloggs', $emailTemplateVariables);
I'm using the Symfony 1.4 mailer where I build the various bits needed for an email and then send it out using:
$this->getMailer()->composeAndSend($sender, $recipient, $subject, $body);
In the email body, I need to able to take advantage of variables generated in the action, so right now I might have this in my action:
$body = 'Your username is '.$username.' and this is the email body.';
Does anyone know of an elegant way of storing/organising various email bodies, instead of having to code them like this straight into my action? I will have many email templates and will also have them in multiple languages.
I've found an old Askeet tutorial discussing this but it seems somewhat out of date with the new symfony 1.4 integration of SwiftMailer, and SwiftMailer documentation itself isn't very clear on this.
Thank you.
I store the email bodies as a template file and render them via sfPartialView. E.g. inside an action:
$view = new sfPartialView($this->getContext(), $this->getModuleName(), $this->getActionName(), 'confirmation_mail');
$view->setTemplate('_confirmation_mail.php');
// values can be set e.g. by setAttibute
$view->setAttribute('name', $name);
$body = $view->render()
The body templates are located in the module's template folder, but I am sure you can somehow change this and e.g. put all email templates into one folder if you want to.
How about just using the native method availible inside sfAction.
$this->getPartial('partial_name'); which works like the partial helpers for you templates.