Sending email using phpmailer

Asked

Viewed 175 times

0

I am trying to send an email with several variables passed via post. I cannot send the content of the email with all variables. What would be the correct syntax? Below is the code:



$name = $_POST['nome'];
$zip = $_POST['cep'];
$adress = $_POST ['endereco'];
$number = $_POST['numero'];
$comp = $_POST['complemento'];
$neigh = $_POST['bairro'];
$city = $_POST['cidade'];
$state = $_POST['estado'];
$iemail = $_POST['email'];
$phone = $_POST['telefone'];
$product = $_POST['produto'];
$model = $_POST['modelo'];
$brand = $_POST['marca'];
$report = $_POST['laudo'];
$date = $_POST['data'];

require 'phpmailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.meudominio.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = '[email protected]';// SMTP username
$mail->Password = 'minhasenha';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('[email protected]', 'Contato');
$mail->addAddress($iemail);     // Add a recipient
$mail->addReplyTo('[email protected]', 'Contato');

$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Sua Ordem de Serviço Grupo SAB';
$mail->Body    = '
    <table id="recibo">
        <h1>Ordem de serviço</h1>
        <p>Cliente:'.$name'</p>
        <p>CEP:'.$zip'</p>
        <p>Endereço:'.$adress'</p>
        <p>Numero:'.$number'</p>
        <p>Complemento:'.$comp'</p>
        <p>Bairro:'.$neigh'</p>
        <p>Cidade:'.$city'</p>
        <p>Estado:'.$state'</p>
        <p>Produto:'.$produtc'</p>
        <p>Modelo:'.$model'</p>
        <p>Marca:'.$brand'</p>
        <p>Laudo:'.$report'</p>
    </table>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';

?>

  • When you say you can’t send with ALL variables, it means some work?

  • No. All variables work correctly. However, at the moment of constructing the body of the email, in <Prev>$mail->Body = ''<code>I am trying to include all variables by closing the single quotes, concatenating the variable and opening the single quote and following this procedure until the end of the body. But then I can’t send the email and results in error

  • Reverse. Use Quotes to open the string and single quotes for tags. Plus which error you get?

  • You’re concatenating in the wrong way.

  • This error: Parse error: syntax error, Unexpected T_CONSTANT_ENCAPSED_STRING in /home/Storage/9/d0/82/brastempconsuleletr1/public_html/admin/enviarods.php on line 83

1 answer

0


I believe your problem is the lack of the operator . to concatenate the values of the variables with the rest of the string. Note, for example, that in the code line

<p>Cliente:'.$name'</p>

there is only the concatenation operator before the variable, but not after, which generates a syntax error, because the PHP interpreter will hold the part '</p> as part of the string, generating the error. To correct, simply insert the concatenation operator again after the variable:

<p>Cliente:'.$name.'</p>

Or, if you prefer more organized writing, by making use of the function sprintf for the formatting of string.

Moreover, it would be good practice for you to adopt in future questions already put in the scope of the question the error message you are getting in your application. This will make life easier for those who help you. Maybe you want to read Manual on how NOT to ask questions.

  • When making the correction, placing the second point, generated the following error:

  • Parse error: syntax error, Unexpected $end in /home/Storage/9/d0/82/brastempconsuleletr1/public_html/admin/enviar.php on line 105

  • And which is line 105?

  • It is the end of the code, the closing tag ? >, just after the email confirmation if

  • There is the closing key "}" of the last code If, before the closing tag?

  • Ah, there was the key even, now it worked. Thank you very much Anderson!

Show 1 more comment

Browser other questions tagged

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