Send email with PHP containing HTML

Asked

Viewed 978 times

1

I am desperately trying to send an email via PHP, according to the code below. But the code breaks when I enter HTML, example:

$mensagem = "<html> (...) </html>";

I’m gonna need a whole page in there! I’ve tried too:

   // Abre o template...
   $corpoDoEmail = file_get_contents(index.txt');

   //E troca as variáveis
   $corpoDoEmail = str_replace( '%NOME%', $nome,       $corpoDoEmail );

But it doesn’t work, it doesn’t bring any data.

<?php

$para = "[email protected]";
$nome = $_POST['nome'];
$assunto = "Ebah! Temos um novo usuário!";
$mensagem = "<strong>Nome:  </strong>".$nome;
$mensagem .= "<br><br>O usuário: ".$_POST['nome'];
$mensagem .= " quer receber nosso e-mail de Bem Vindo!";

QUERO O CONTEÚDO HTML AQUI

$headers =  "Content-Type:text/html; charset=UTF-8\n";   
$headers .= "From:  Golleo<[email protected]>\n";
$headers .= "X-Sender:  <[email protected]>\n"; 
$headers .= "X-Mailer: PHP  v".phpversion()."\n";   
$headers .= "X-IP: ".$_SERVER['REMOTE_ADDR']."\n";   
$headers .= "Return-Path:  <[email protected]>\n";
$headers .= "MIME-Version: 1.0\n";

mail($para, $assunto, $mensagem, 
$headers);

?>
  • Receive email with no content?

  • I get only what is declared in "$message", but wanted to put an entire html page, so email have a more beautiful layout!

  • Ahhh, you receive the e-mail without formatting

  • Actually, I want to e-mail this full page: http://golleo.com.br/developer/workspace/golleosignupmail/index.html

  • @Gabriel remove the DOCTYPE and the tags html, head, meta, title, script and body. Some email services block messages that contain these tags because they may modify the default behavior of the email service.

  • 1
Show 1 more comment

2 answers

0

Good afternoon, you can use the phpmailer library, the use is easy and well simplified:

require_once("../phpmailer/class.phpmailer.php");
$email = new PHPMailer();
$email->From      = '[email protected]';
$email->FromName  = 'seunome';
$email->Subject   = 'assunto';
$email->msgHTML('<html>Mensagem HTML</html}>');
$email->AddAddress('[email protected]');

$email->send();

This example has HTML messaging.

Phpmailer

0

Try trading for this Header

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

Note: Swap the whole Header.

I noticed an error in your code posted above, forgot to put a quote.

// Abre o template...
$corpoDoEmail = file_get_contents(index.txt');

change to

// Abre o template...
$corpoDoEmail = file_get_contents('index.txt');
  • I’m afraid not yet! Look I want to do something like this: http://golleo.com.br/developer/workspace/golleosignupmail/mail.txt

  • @Gabriel, I edited the answer to a look

Browser other questions tagged

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