How to add subject in tranctional email template for magento 2 version - magento2

I create below but not idea how to add custom subject in this code
$transport = $this->transportBuilder->setTemplateIdentifier($emailTemplateId)
->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId])
->setTemplateVars($templateParams)
->setFrom(['name' => $senderEmailName, 'email' => $senderEmail])
->addTo($email)
->getTransport();
$transport->sendMessage();
For Magento 1 we use add line:-
->setTemplateSubject($mailSubject)

As it has already been answered, you do set it within the template via
<!--#subject [...] #-->
You can read it out then within the template by accessing the following template variable:
{{var this.getSubject()}}
If you for example use the trans directive to create the subject, this will return the translated subject.
Setting the subject for the message then is transparently done when the TransportBuilder prepares the message (\Magento\Framework\Mail\Template\TransportBuilder::prepareMessage).
So technically, as long as you make use of the email-templates (which is recommended, see email_templates.xml files for reference) this is how the subject is handled.
In your code this happens already within the call to
->getTransport();
If the subject is empty, set it in the template first.
References:
Magento 2: How to declare a custom email template for your module (Stackexchange)

You can set the subject in the email template header
<!--#subject This is subject #-->

Related

Salesforce send Email by Apex

I'm making by a requirement a code able to send an E-mail to an specific list of E-mails, due the fact that I must to include the attachments of the record I decided to use an apex class instead an e-mail alert. This object (A custom object ) must populate some fields in an email template with some of the record´s fields. I implemented the following code
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(lista);
mail.setTemplateId('00X21000000QR22');
//mail.setWhatId(idMinuta);
mail.setTargetObjectId('005d0000005NMIx');
mail.setSaveAsActivity(false);
List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
for (ContentVersion document: documents)
{
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(document.Title);
efa.setBody(document.VersionData);
fileAttachments.add(efa);
}
mail.setFileAttachments(fileAttachments);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
I understood that to make the fields merge it´s necesary to use the WhatId method. In the related code, I have commented it because It generates an error (INVALID_ID_FIELD, WhatId is not available for sending emails to UserIds.)
My question is, if is it possible to do this with a custom object. I´m a little confuse with salesforce documentation beacuse it looks like the method supports a custom object, or maybe If I am forggeting something to include in the code.
If i keep the WhatID line commented, effectively the email is sent with the attachments and the Template but it is not populated.
I really need this kind of solution because the org have in this object at least 20 email templates, for me will be easier just to pass the Id of the template instead of makig a code with 20 different html codes for each situation
Thanks a lot
Please publish this question at Salesforce StackExcahnge https://salesforce.stackexchange.com/

override the content of cakePHP3 emails

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.

How can I send email Headers using Amazon SES?

I am using a Wrapper from http://sourceforge.net/projects/php-aws-ses/
I am not able to send Email Headers with
From: Name <email#example.com>
Is there any way we can send headers using amazon ses. Any other PHP Wrapper you recommend which allows us to do that?
Change the FROM Variable to something like this
$m->setFrom(" Name <info#gitgrow.com>");
For the new API SDK (v2) you can use:
$client->sendMail( array(
'source' => 'Name <a#b.c>',
....
)
);
Basically if you want to include the name, the source must have the name followed by the email id and importantly, the email id must be enclosed in < and >

Sending e-mail programmatically in Magento is failing

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

Email body in Symfony 1.4 mailer?

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.