Edit body email php with html

1

I needed to edit the body of an email (after completing the form, an email is sent). But I'm not able to integrate my HTML code with PHP.

I will send a printscreen of what is happening.

WhatIwantedwassomethinglikethis- link

CODE:

<?php
// Sanitize data, or initialize if they don't exist.
$clientname = isset( $_POST['ci_name'] ) ? esc_html( trim( $_POST['ci_name'] ) ) : '';
$email      = isset( $_POST['ci_email'] ) ? esc_html( trim( $_POST['ci_email'] ) ) : '';
$arrive     = isset( $_POST['arrive'] ) ? esc_html( trim( $_POST['arrive'] ) ) : '';
$depart     = isset( $_POST['depart'] ) ? esc_html( trim( $_POST['depart'] ) ) : '';
$guests     = isset( $_POST['adults'] ) ? intval( $_POST['adults'] ) : '0';
$children   = isset( $_POST['children'] ) ? intval( $_POST['children'] ) : '0';
$message    = isset( $_POST['ci_comments'] ) ? sanitize_text_field( stripslashes( $_POST['ci_comments'] ) ) : '';
$timeguest     = isset( $_POST['ci_timeguest'] ) ? esc_html(trim($_POST['ci_timeguest'])) : '';
$contactguest     = isset( $_POST['ci_contactguest'] ) ? esc_html(trim($_POST['ci_contactguest'])) : '';
$autorizo = isset( $_POST['autorizo'] ) ? 'Autorizo o uso do email.' : 'Não autorizo o uso do email.';
$camaextra = isset( $_POST['camaextra'] ) ? 'Necessito de uma cama extra.' : 'Não necessito de uma cama extra.';


// Message is optional, so, no check.

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Alright, lets send the email already!
if ( empty( $errorString ) ) {

$mailbody .= '<html><body>';
$mailbody .= '<img src="/wp-content/uploads/2015/06/email.jpg" />';
$mailbody .= '<table rules="all" style="border-color: #666;" cellpadding="10">';

        $mailbody .= __('Nome:', 'ci_theme' ) . ' ' . $clientname . "\n";
        $mailbody .= __('Email:', 'ci_theme' ) . ' ' . $email . "\n";
        $mailbody .= __('Chegada:', 'ci_theme' ) . ' ' . $arrive . "\n";
        $mailbody .= __('Saída:', 'ci_theme' ) . ' ' . $depart . "\n";
        $mailbody .= __('Adultos:', 'ci_theme' ) . ' ' . $guests . "\n";
        $mailbody .= __('Crianças:', 'ci_theme' ) . ' ' . $children . "\n";
        $mailbody .= __('Quarto:', 'ci_theme' ) . ' ' . $room->post_title . "\n";
        $mailbody .= __('Mensagem:', 'ci_theme' ) . ' ' . $message . "\n";
        $mailbody .= __('Hora do Check-in (hh:mm):', 'ci_theme' ) . ' ' . $timeguest . "\n";
        $mailbody .= __('Contacto:', 'ci_theme' ) . ' ' . $contactguest . "\n";
        $mailbody .= __('Autorização:', 'ci_theme' ) . ' ' . $autorizo . "\n";
        $mailbody .= __('Cama Extra:', 'ci_theme' ) . ' ' . $camaextra . "\n";
        $mailbody .= "</table>";
        $mailbody .= "</body></html>";


        mail($to, $mailbody, $headers); 

        // If you want to receive the email using the address of the sender, comment the next $emailSent = ... line
        // and uncomment the one after it.
        // Keep in mind the following comment from the wp_mail() function source:
        /* If we don't have an email from the input headers default to wordpress@$sitename
        * Some hosts will block outgoing mail from this address if it doesn't exist but
        * there's no easy alternative. Defaulting to admin_email might appear to be another
        * option but some hosts may refuse to relay mail from an unknown domain. See
        * http://trac.wordpress.org/ticket/5007.
        */
        //$emailSent = wp_mail(ci_setting('booking_form_email'), get_option('blogname').' - '. __('Booking form', 'ci_theme'), $mailbody);
     $emailSent = wp_mail(ci_setting('booking_form_email'), get_option('blogname').' - '. __('Formulário de Reserva','theme-text-domain', 'ci_theme'), $mailbody, 'From: "'.$clientname.'" <'.$email.'>');
         $emailSent2 = wp_mail( $email, __('Booking Inquiry','theme-text-domain', 'ci_theme'), __('Thank you so much for your interest in Hotel Aveiro Center! We will get back to you within 24 hours to answer your request.','theme-text-domain','ci_theme'),'From: Hotel Aveiro Center <[email protected]>');


    }


}
?>

Am I putting the code well?

/ To send HTML mail, the Content-type header must be set
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

            // Alright, lets send the email already!
            if ( empty( $errorString ) ) {

            $mailbody .= '<html><body>';
    $mailbody .= '<img src="http://invisual.pt/hotelaveirocenter/wp-content/uploads/2015/06/email.jpg'.$_SERVER['invisual.pt'].'http://invisual.pt/hotelaveirocenter/wp-content/uploads/2015/06/email.jpg" />';
    $mailbody .= '<table rules="all" style="border-color: #666;" cellpadding="10">';

                $mailbody = __('Nome:', 'ci_theme' ) . ' ' . $clientname . "\n";
                $mailbody .= __('Email:', 'ci_theme' ) . ' ' . $email . "\n";
                $mailbody .= __('Chegada:', 'ci_theme' ) . ' ' . $arrive . "\n";
                $mailbody .= __('Saída:', 'ci_theme' ) . ' ' . $depart . "\n";
                $mailbody .= __('Adultos:', 'ci_theme' ) . ' ' . $guests . "\n";
                $mailbody .= __('Crianças:', 'ci_theme' ) . ' ' . $children . "\n";
                $mailbody .= __('Quarto:', 'ci_theme' ) . ' ' . $room->post_title . "\n";
                $mailbody .= __('Mensagem:', 'ci_theme' ) . ' ' . $message . "\n";
                $mailbody .= __('Hora do Check-in (hh:mm):', 'ci_theme' ) . ' ' . $timeguest . "\n";
                $mailbody .= __('Contacto:', 'ci_theme' ) . ' ' . $contactguest . "\n";
                $mailbody .= __('Autorização:', 'ci_theme' ) . ' ' . $autorizo . "\n";
                $mailbody .= __('Cama Extra:', 'ci_theme' ) . ' ' . $camaextra . "\n";
                $mailbody .= "</table>";
                $mailbody .= "</body></html>";


    mail(
        $to,
        $mailbody,
        $headers
    ); 

    // NOVO header adicionado no wp_mail
    $headersWp  = 'MIME-Version: 1.0' . "\r\n";
    $headersWp .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headersWp .= "From: $clientname" . "\r\n";

     $emailSent = wp_mail(
        ci_setting('booking_form_email'),
        get_option('blogname').' - '. __('Formulário de Reserva','theme-text-domain','ci_theme'),
        $mailbody,
        $headersWp);          


        $emailSent2 = wp_mail(
        $email,
        __('Booking Inquiry','theme-text-domain','ci_theme'),
        __('Thank you so much for your interest in Hotel Aveiro Center! We will get back to you within 24 hours to answer your request.','theme-text-domain','ci_theme'),
        'From: Hotel Aveiro Center <[email protected]>');


            }


        }

I was able to remove - WordPress -. leaving only the sender's name.

changing the code

$headersWp .= "From: $clientname" . "\r\n";

To:

$headersWp .= "From: $clientname, <$email>" . "\r\n";

The question of characters with accents and cedillas is just missing

    
asked by anonymous 16.06.2015 / 14:01

1 answer

1

Let's validate the errors before proceeding with the possible solution:

1 - Absolute paths (addresses / urls)

$mailbody .= '<img src="/wp-content/uploads/2015/06/email.jpg" />';

The first problem concerns the address of the image you have placed in the email. In this case the address is relative to your page and not to the server where it actually lies.

Just think this way: a gmail user, if you type /wp-content/uploads/2015/06/email.jpg after the address https://www.gmail.com will be able to load your image? No, right? So the path should be relative to your server, for example:

http://www.seusite.com.br/wp-content/uploads/2015/06/email.jpg

In PHP you can do this:

$mailbody .= '<img src="http://'.$_SERVER['SERVER_NAME'].'/wp-content/uploads/2015/06/email.jpg" />';

Note: there are still factors to validate in this url / domain, such as whether it is https or not.

2 - Mail functions

I noticed that you are apparently sending 3 emails, but only in the first you are using the $headers and in the demails.

Remember to set $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; , for example:

// O trecho abaixo se manteve igual da sua pergunta
mail(
    $to,
    $mailbody,
    $headers
); 

// NOVO header adicionado no wp_mail
$headersWp  = 'MIME-Version: 1.0' . "\r\n";
$headersWp .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headersWp .= "From: $clientname" . "\r\n";

 $emailSent = wp_mail(
    ci_setting('booking_form_email'),
    get_option('blogname').' - '. __('Formulário de Reserva','theme-text-domain','ci_theme'),
    $mailbody,
    $headersWp);

// O trecho abaixo se manteve igual da sua pergunta
 $emailSent2 = wp_mail(
    $email,
    __('Booking Inquiry','theme-text-domain','ci_theme'),
    __('Thank you so much for your interest in Hotel Aveiro Center! We will get back to you within 24 hours to answer your request.','theme-text-domain','ci_theme'),
    'From: Hotel Aveiro Center <[email protected]>');

3 - HTML Code

Based on the tutorial you said you followed, I think you forgot to put the rows and columns ( tr , td ).

So, follow the code below:

$mailbody .= '<html><body>';
    $mailbody .= '<img src="/wp-content/uploads/2015/06/email.jpg" />';
    $mailbody .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
        $mailbody .= "<tr style='background: #eee;'><td><strong>Nome:</strong> </td><td>" . $clientname . "</td></tr>";
        $mailbody .= "<tr><td><strong>Email:</strong> </td><td>" . $email . "</td></tr>";
        $mailbody .= "<tr><td><strong>Chegada:</strong> </td><td>" .  . $arrive . "</td></tr>";
        $mailbody .= "<tr><td><strong>Saída:</strong> </td><td>" . $depart . "</td></tr>";
        $mailbody .= "<tr><td><strong>Adultos:</strong> </td><td>" . $guests . "</td></tr>";
        $mailbody .= "<tr><td><strong>Crianças:</strong> </td><td>" . $children . "</td></tr>";
        $mailbody .= "<tr><td><strong>Quarto:</strong> </td><td>" . $room->post_title . "</td></tr>";
        $mailbody .= "<tr><td><strong>Mensagem:</strong> </td><td>" . $message . "</td></tr>";
        $mailbody .= "<tr><td><strong>Hora:</strong> </td><td>", 'ci_theme' ) . ' ' . $timeguest . "</td></tr>";
        $mailbody .= "<tr><td><strong>Contacto:</strong> </td><td>" . $contactguest . "</td></tr>";
        $mailbody .= "<tr><td><strong>Autorização:</strong> </td><td>" . $autorizo . "</td></tr>";
        $mailbody .= "<tr><td><strong>Cama:</strong> </td><td>" . $camaextra . "</td></tr>";
    $mailbody .= "</table>";
$mailbody .= "</body></html>";

I think this will solve your problem, in this case missing the header with Content-type text / html .

I hope I have helped.

Any questions leave a comment below.

    
16.06.2015 / 16:56