Edit php email body with html

Asked

Viewed 1,419 times

1

He needed to edit the body of an email (after completing the form, an email is sent). But I can’t integrate my HTML code with PHP.

I’ll send you a printscreen of what’s happening.corpo de email a formatar

What I wanted was something like this - https://css-tricks.com/wp-content/csstricks-uploads/before-and-after.png

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]>');


    }


}
?>

I’m putting the code right?

/ 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 managed to remove - Wordpress - . getting only the Sender Name.

changing the code

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

for:

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

Only the question of accented and cedile characters is missing

  • All right my friend? We at Sopt obviously want to help you, but the way you described it makes it a little difficult. Edit your question and try to elaborate more on what you’re trying to do, what errors are returned, how it should work and what you’re getting, but not something like, "can help me solve/do this," without us knowing what "this" is. The more details the better. Thanks in advance.

  • Good afternoon, thanks for the tips. I’m new at this. What he basically wanted was to edit the body of the email sent after completing the form on the site. I’ve put that code above demonstrated, but I’m not getting the intended.

  • Just one question before I post my answer: is the printscreen relative to what you’re getting in your email? Using what? Gmail? Yahoo? Some e-mail of its own?

  • Yes the printscreen is relative to what I am receiving in my email. I am using my own email. ([email protected]) And I’m using roudcube Webmail to read the emails

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 is related to the address of the image you placed in the email. In this case the address is relative to your page and not to the server where it actually is.

Just think of it 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? Then 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 so:

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

Note: there are still factors to be validated at this url/domain, for example, whether https or not.

2 - Mail functions

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

Remember to set the $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 believe 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 believe that will solve your problem in this case, the lack of header with Content-type text/html.

I hope I’ve helped.

Any questions leave a comment below.

  • Not working :( this happening http://i.imgur.com/l3W0B3k.png? 1

  • I edited my question, but the problem was that you did not follow the tutorial. You created a table and did not put rows or columns. When doing something of the kind, always try to pay attention to what is being done on both sides, as it was merely lack of attention.

  • Thank you, I’ll test it. Just one more thing. That’s right?: código $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" />';

  • Perfect @Patrickmaciel. Thank you very much. It’s working. Right now I only have a problem with the text. Words with accents and "Ç" do not appear right. and the sender of the email appears " Wordpress <Ricardo Barbosa>" You can remove the "Wordpress". Thank you very much

  • @Ricardorafaelrodrigues Just change the charset in the header to utf-8. I edited the answer.

Browser other questions tagged

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