Sunday, January 16, 2022

[FIXED] When sending SMTP email in CakePHP no subject is set

Issue

After I have switched my CakePHP application to use SMTP for emailing (using the Email component) All of the emails sent out now have no subject.

They always had a subject before and everything else works now just no subject. I contacted support for the smtp server I am using (SendGrid) and they assured me that no subject headers are being included in the emails.

CakePHP uses the _mail() function by default or the _smtp() function when using smpt.

I looked through the code and I can see where the _mail function uses the subject, however I do not see _smtp or _smtpSend using $this->subject anywhere. Am I missing something?

What do I need to do to get the subject to work?


Update adding code:

This is how I am sending an email from my controller:

$this->Email->to = $data['Order']['user_email'];
$this->Email->subject = 'Your Order Has Shipped';
$this->Email->template = 'order_shipped';
$this->Email->layout = 'sussex';
$this->Email->sendAs = 'html';

$this->Email->send();

The Email component is the standard Cake 1.3 Email component only I added this code to the beginning of the send() function:

$this->smtpOptions = array(
  'port'=>'587', 
  'timeout'=>'30',
  'host' => 'smtp.sendgrid.net',
  'username'=>'my_user',
  'password'=>'*******',
  'client' => 'www.example.com'
);
$this->delivery = 'smtp';

Solution

In above code which you have mentioned you are using in send method of email components, has to use before call to $this->__createHeader();.Since for smtp delivery, subject is set in __createHeader method it is necessary to put $this->delivery = 'smtp'; before __createHeader call.



Answered By - while1

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.