CakePHP HTML email rendered as view - email

I'm trying to send an html email but it is rendered as a view and then everything stops.
My code is very basic
$Email = new CakeEmail();
$Email->template('enquiry', 'default')
->emailFormat('html')
->to('mymail#mail.com')
->from('notme#mail.com');
$Email->send()
Text email works fine.
I followed the trail code through the cake codebase down to _render in CakeView. I think that something is preventing the rendered email template to be returned and echoes it to the screen instead.
I checked output_buffering in php.ini, it's set to 4096.
This is an inherited project, was working on the original server but not on mine.
Do you have any ideas about what else to look for? Thanks

Check Path of templete file app\View\Emails\html\enquiry.ctp
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail();
$email->config('default');
$email->from(array('notme#mail.com' => 'Not Me'));
$email->to('mymail#mail.com');
$email->subject('Test mail');
$email->viewVars($message);
$email->template('enquiry', 'default');
$email->emailFormat('html');
$email->send();
Hope this will help!

Related

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.

My template emails always send as plain text not HTML

I'm new to SendGrid and I'm trying to work out why the emails are always sent as plain text instead of the designed HTML transactional template I've made. If I use the sendgrid "preview/test" feature to send myself the email, it comes through looking exactly how it should, images, HTML etc.
However, when I use the PHP API to send the email, the email is sent but only in plain text. I use this line to tell SendGrid which template to use:
$mail->setTemplateId([my template ID]);
Are there some other things I should be setting via the PHP API before I finally call the following?
$sg->client->mail()->send()->post($mail);
Below is all my SendGrid code:
$from = new SendGrid\Email([website name], [website email address]);
$subject = "test subject";
$to = new SendGrid\Email(null, $email);
$content = "";
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$mail->setTemplateId([my template ID]);
//$mail->setASMGroupId(3057);
$mail->personalization[0]->addSubstitution("%email%", $email);
$mail->personalization[0]->addSubstitution("%hash%", $hash);
$apiKey = [my api key];
$sg = new \SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
I dont put anything in for the $content because the content is dictated in the template.
If anyone has any idea why it's only sending a plain text version of my templated email when sent from PHP, any advice would be much appreciated.
Thank you
EDIT
I thought I might have to set the content-type in the header so I added:
$mail->addHeader("Content-Type", "text/html");
But this just gives me an error. Is there something I'm missing?
To force the email to be treated as HTML, just add an empty HTML body to the message:
$mail->setHtml(" ");
$mail->setText(""); //Sendgrid requires a plain-text body too
You can also use
$content = new \SendGrid\Content("text/html", "<html><body>some text here</body></html>");
$mail = new \SendGrid\Mail($from, $subject, $to, $content);
as shown in the example code:
https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php

I'm sending text/html only emails with zend framework how can I send multipart to include a text only version?

I'm using the code below which sends an html email using a template as the body. How can I include a text only version along with the html version? I'd like to use another template which would be all text to include.
Thanks
$transport = new Zend_Mail_Transport_Smtp('smtp.domain.com', $config);
$HTMLText = $html->render('forgot-password.phtml');
$mail = new Zend_Mail();
$mail->setHeaderEncoding(Zend_Mime::ENCODING_BASE64);
$mail->setBodyHtml($HTMLText);
$mail->setFrom('name#domain.com', 'Name');
$mail->addTo($userDataPost['user_email']);
$mail->setSubject('Your new password');
$mail->send($transport);
How about setBodyText(string) function?

PEAR Mail_mime not showing appropriate version

I have some trouble with PEAR when I'm using the Mail_mime class to send out HTML/text mail with embedded images.
What I need script to do, is to provide an email with both a text and HTML version of the content. The content will be somewhat different.
The text version will contain some text, and an image attachment.
The HTML version will have a layout with some links and an embedded image. This image is the same as the attached image in the text version.
What I've got so far, is a script that send a plain text version and an HTML version. The text version is in fact not the text version which I'm telling it to send, but a stripped down version of the HTML email.
After some investigation, I found out that the plain text version actually gets sent in the email, but the email clients only show the stripped HTML version for some strange reason. It also seems like it's the addHTMLImage() method that breaks it. Without the embedded image the
What my code looks like, at the moment:
<?php
require 'Mail.php';
require 'Mail/mime.php';
$to = 'your#email.com';
$additional_headers = array(
'Subject' => 'Email subject',
'From' => 'my#domain.com'
);
$text_body = <<<TEXT
This is the plain text version.
TEXT;
$html_body = <<<HTML
<p>This is the HTML version</p>
<p><img src="image.jpg" alt="" /></p>
HTML;
$mime = new Mail_mime();
$mime->setTxtBody($text_body);
$mime->setHTMLBody($html_body);
$mime->addHTMLImage(file_get_contents('default.jpg'), 'image/jpeg', 'image.jpg', FALSE);
$body = $mime->get();
$headers = $mime->headers($additional_headers);
$mailer =& Mail::factory('smtp', array(
'host' => 'my.mailserver.net',
'port' => 0,
'auth' => TRUE,
'username' => 'myusername',
'password' => 'mypassword'
));
$res = $mailer->send($to, $headers, $body);
if (PEAR::isError($res)) {
echo 'Couldn\'t send message: '.$res->getMessage();
}
?>
As far as I know, there doesn't seem to be anyone else with this problem. Is there something wrong with my code, or with my PEAR installation?
Pass in the end of line character to the constructor of the Mail_mime class.
Windows in "\n\r" - or the other way around, can't remember
We ran into the same issue today and have done extensive research in to it. What we've uncovered is that Mozilla Thunderbird chokes on ABNF strings that don't follow the Content-Type capitalization pattern. In all other email clients we've tested, the text alternative worked as expected.
So, you're problem is likely your client. Hope this helps point you in the right direction.

How to rewrite or set the Return-Path in cakePHP Email Component?

I'm using the cakePHP email component for sending mails from my application. Now the return-path has something like www#domain.tld
How can I set or rewrite the Return-Path value in emails when using the cakePHP Component?
I know how to do it when sending mails via 'mail' in PHP but the cakePHP email component seems to missing such a feature... or am I missing something? :)
In CakePHP 2 (where the Email Component is largely replaced by the CakeEmail class), you can do this configuration inside /app/Config/email.php:
class EmailConfig {
public $email = array(
...
// The next line attempts to create a 'Return-path' header
'returnPath' => 'myaddress#mydomain.com',
// But in some sendmail configurations (esp. on cPanel)
// you have to pass the -f parameter to sendmail, like this
'additionalParameters' => '-fmyaddress#mydomain.com',
...
);
}
Or if you need to do it just for a single email, something like this should work...
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail('MyConfig');
$email->from(...)
->to(...)
->subject(...)
->returnPath('myaddress#mydomain.com')
// Haven't tested this next line, but may possibly work?
->config(array('additionalParameters' => '-fmyaddress#mydomain.com'))
->send();
There's an attribute called EmailComponent::return that is the return path for error messages. Note that this is different than the replyTo attribute.
$this->Email->return = 'name#example.com';
http://book.cakephp.org/1.3/en/The-Manual/Core-Components/Email.html
A co-worker and I were working on this same issue, we found that editing the following line in php.ini gave us our fix:
from:
sendmail_path = /usr/sbin/sendmail -t -i
to:
sendmail_path = /usr/sbin/sendmail -t -i -f youremail#address
when testing be sure to send your emails to a valid domain. this caught us for a few minutes.
To change the return path in CakePHP Email component I do like this:
...
$return_path_email = 'return#email.com';
...
$this->Email->additionalParams = '-f'.$return_path_email;
and it works like charm ;)
Digging into the cake manual when you were looking at how to use the rest of the component you should have seen something like the following. This is what set the Return-Path.
$this->Email->return = 'name#tld.com';