Send emails with built-in HTML/CSS Cakephp

Asked

Viewed 583 times

1

Good afternoon!

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

I’m working on a project using the cakephp framework.
In the documentation, it seems to me that it has the possibility to send emails with the css built in, the same way 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 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 sent messages arrive blank, 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.

  • 1º the CSS you want to apply can be inline or do you want a style sheet? 2º Email must go through the server?

  • @Ricardohenrique css is inline (actually not even inline yet, I wanted to resolve these html tags that are exposed in the email,) and the email does not need to go through the server.

  • I’ve worked with something like this, I think I have a solution so wait till I post.

  • @Ricardohenrique Okay, I’m on hold.

2 answers

1

1º I used Mandriljs which is a service that sends me 12 Thousand emails for free to you (this is not spam). works as a field medium to not need a server.

Access link (you need to create an account): Mandrills

2º Create a common form and (put as method POST just not to fill the URL but nothing will be sent to a server) in the send 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 recover the fields filled by the form user:

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: You do not need to enter your email passwords anywhere, you should only register with MandrilJS (without entering password of your emails) and generating a key, key that will be inserted in the json in the specific location (it is generated in the settings).

OBS2: It is necessary jQuery.

OBS3: You can insert CSS into tags

OBS4: Both Emails have to be validated by the Recipient for obvious missteps he will receive the emails, Remente why Mandrijs sends weekly email sent and read statistics.

  • Comment here any questions in the process of creating your account.

  • Thanks for the answer Ricardo, it’s a solution. But I need it to be sent back-end, preferably natively with cake. In this case I posted is a simple contact form, but there are other excerpts that send email in the back-end... I’m still searching for possible solutions, maybe I use your answer to elaborate a Curl and send the email! Thank you for your reply!

  • Okay, I hope you find a solution, if you find pole here.

0

Good night, 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 that’s what solved it. Second thing I noticed, in my /app/Views/Emails/html folder, I didn’t have the "contact" template, so my html emails were always empty and the text emails weren’t. I believe they were the only modifications I made.

Thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.