0
votes

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';
3
Can you add your code for sending Email?FYI cakephp add subject in _createHeader method of Email component for smtp. - while1
@Shrey Gupta there is a lot of code in the Email component, which part do you want to see....or just the part in my controller where I am sending an email? - JD Isaacks
Your code only for sending email, which uses email component. - while1
@Shrey Gupta I have added the code. - JD Isaacks

3 Answers

1
votes

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.

0
votes

I have used already the email component with smtp (using gmail) and it works fine it sets the subject and everything correctly.

I used it this way:

    $this->Email->reset();
    $this->Email->smtpOptions = $options;
    $this->Email->delivery = 'smtp';
    $this->Email->from = $from;
    $this->Email->to = $to;
    $this->Email->subject = $tittle;
    if ( $this->Email->send($message) ) {
        return true;
    } else {
        return false;
    }

and $options is

    $options = array(
        'port'=>'465',
        'timeout'=>'30',
        'host'=>'ssl://smtp.gmail.com',
        'username'=>'[email protected]',
        'password'=>'yourPassword'
    );

Also the _smtp function uses createHeader that uses the subject.

What may be wrong that explains this beahviour. You may have some encoding problem, are you using normal characters in your subject? some of the functions in cake die without escaping them or something (i usually have spanish text so i know first handed). You may insert a var_dump where it creates the header to see how it is doing it...

Hope this helps you, if not post a comment :)

0
votes

I was also facing the same issue after investigating, I found that the php mbstring is not installed on the server. After installing the PHP Mbstring the problem is solved and I am getting the subject in the emails.

As Cakephp used this library for the string processing so it needs this library.

 yum install php-mbstring