Send emails with HTML / CSS embedded CakePhp

1

Good afternoon!

I'm trying to do something "simple", I need to send a stylized email, I do not want to just send text, I need to insert html and css tags. Before working with cakePHP I used PHPmailler , where I could send emails with HTML and CSS in line.

I'm working on a project that uses the cakePHP framework. In the documentation, it seems to me that it has the possibility of sending emails with the embedded css, in the same way that I did, but all the emails sent go with the HTML tags printed as if they were simple texts.

$email = new CakeEmail('smtp');
$email->template('contato');    
$email->emailFormat('html');        
$email->to('****************');
$email->subject('Contato - Fale Conosco');
$email->viewVars(array('contato' => $this->request->data));
$email->send();

This is the code I'm using to send the emails and the view is:

<html>
<body>
    <p><strong>Contato Recebido</strong></p>

    <p>Data: <strong><?php echo date("d/m/Y"); ?></strong></p>
    <p>Nome: <strong><?php echo $contato['Contato']['nome']?></strong></p>
    <p>Telefone: <strong><?php echo $contato['Contato']['telefone']?></strong></p>
    <p>E-mail: <strong><?php echo $contato['Contato']['email']?></strong></p>
    <p>Assunto: <strong><?php echo $contato['Contato']['assunto']?></strong></p>
    <p>Mensagem: <strong><?php echo $contato['Contato']['mensagem']?></strong></p>
</body>

However, when I leave the parameter "$ email-> emailFormat ('html'); the messages sent arrive in white, when I remove this parameter leaving the default (text) the messages arrive with the html on the screen as if it were plain text and if I put the parameter as "both" nothing arrives.

    
asked by anonymous 25.05.2015 / 22:26

2 answers

1

I used MandrilJS, which is a service that sends me 12 thousand emails free of charge to you (this is not spam). it works as a field medium so you do not need a server.

Link to access (you need to create an account): MandrilJS

2º Create a common form and (put as method POST just not to fill the URL but nothing will be sent to a server) on the submit button do it this way:

<button onClick="sendMail();return false;">Enviar</button>

3rd Create a Javascript with a function of the same name as the button that will retrieve the fields populated by the user of the form:

function sendMail(){
    var nome = document.getElementById("nome").value;
    var email = document.getElementById("email").value;
    var assunto = document.getElementById("assunto").value;
    var desc = document.getElementById("desc").value;
    var body = '<strong>Nome: </strong>'+nome+'<br />'+
               '<strong>Email: </strong>'+email+'<br />'+
               '<strong>Assunto: </strong>'+assunto+'<br />'+
               '<strong>Descição: </strong>'+desc;

    $.ajax({
        type:"POST",
        url:"https://mandrillapp.com/api/1.0/messages/send.json",
        data:{
                'key':'sua chave aqui',
                'message':{
                    'from_email':'email que irá enviar',
                    'to':[
                        {
                            'email':'email remetente',
                            'name':'Seu Nome (ou nick)',
                            'type':'to'
                        }
                    ],
                    'subject':'Assunto',
                    'html':body
                }
            }
    });
}

OBS1: It is not necessary to enter your email passwords anywhere, you only have to register with MandrilJS (without entering your email password) and generate a key, this key will be inserted in the json at the specific location (it is generated in the settings).

OBS2: jQuery is required.

OBS3: You can insert CSS into tags

OBS4: Both Emails have to be valid the Recipient for obvious reasons he will receive the emails, Remente why MandriJS sends weekly statistics of mailings and reads. >     

26.05.2015 / 14:43
0

Good evening, I was able to find a solution to my problem.

First thing I had to do was remove the opening HTML tags, but I'm not sure if that's what it was. Second thing I noticed, in my folder / app / Views / Emails / html, I did not have the "contact" template, so my html emails were always empty and text emails were not. I think they were the only changes I made.

Thanks for the help!

    
26.05.2015 / 23:22