1

I can send emails using wp_mail, however any image on the body will not be display on the email received. Is there any way of sending an image within the message

this is the code used to send emails:

$to = '[email protected]';
$headers = array('Content-Type: text/html; charset=UTF-8','From: [email protected]');
$subject = "Olá $util, Benvindo(a) ao nosso site!";
$body = '
          <h3>Olá </h3></br>
          <p>Obrigado por se registar no site</p>
          <p>Saudações,</p>
          <p>my website</p>';
wp_mail($to, $subject, $body, $headers);

this code works and send the email, however if I put an image tag with the src of my image, the image will not be displayed, had also try to send the image tag inside the body tag and still the same result.

Anyone!

3
  • did you modify the code for the question? The code in the question has a PHP syntax error on the first line
    – Tom J Nowell
    Commented Sep 1, 2021 at 18:52
  • yes I did, only the email names. had already correct that on the $to variable. Commented Sep 1, 2021 at 19:18
  • can you modify it to show what you tried when you used img tags?
    – Tom J Nowell
    Commented Sep 1, 2021 at 20:14

1 Answer 1

0

for those with the same issue, here is my inteire code working, I had to put the img tag inside the subject body tag, also once I was using $_POST the link just didnt work, had made a work around using a word "[imagem]" to replace with img tag and the link uploaded.

<div class="container">
  <div class="email_maker">
    <h2>Enviar vários emails</h2>
    <form action="?page=editor_redes" method="post" enctype="multipart/form-data">
        <label for="emails_assunto">Assunto:</label>
        <input type="text" name="emails_assunto" required><br>
        <label for="emails_msg">corpo de email:</label>
        <textarea name="emails_msg" rows="20" cols="80" required></textarea><br>
        <label for="emails_image">Banner publicitário: use no texto acima [imagem] e aqui apenas links de imagens online</label>
        <input type="text" name="emails_image"/><br>
        <div class="emails_conf">
          <input type="file" name="emails_file" required/>
          <label for="emails_file">Destinatários:</label>
        </div><br><br>
        <input type="submit" name="emails_submit" value="enviar" />
    </form>
  </div>
  <div class="email_preview">

  </div>
</div>
<style media="screen">
  .container{display:flex;}
  .email_maker{width:50%;}
  .email_maker h2{color:orange;margin-top:40px;margin-bottom:30px;}
  .email_maker form{display:grid;}
  .email_maker label[for=emails_file]{float:right;padding-right:10px;padding-top: 7px;}
  .email_maker input[type=file]{float:right; color:gray;}
  .email_maker input[type=submit]{background:lightyellow;border-radius: 7px;}
</style>
if(isset($_FILES['emails_file'])){
  $path_to_wp = $_FILES['emails_file']['tmp_name'];
  $file = fopen($path_to_wp,"r");
  if (strpos($_POST['emails_msg'], '[imagem]')) {
    $ms = explode('[imagem]',$_POST['emails_msg']);
    $msg = $ms[0];
    $msg .= "<a href='https://ittca.github.io'><img src='".$_POST['emails_image']."'></a>";
    $msg .= $ms[1];
  } else {
    $msg = $_POST['emails_msg'];
  }
  $body = "<html><head></head><body>";
  $body .= $msg;
  $body .= "</body></html>";
  $a = 1;
  while(!feof($file)){
    $to = fgets($file);
    if($to != ""){
      $headers = array('Content-Type: text/html; charset=UTF-8','From: [email protected]');
      $enviado = wp_mail($to, $_POST['emails_assunto'], $body, $headers);
      if($enviado){
        echo '<p>'.$a.' [<a style="color:green;">OK</a>] '.$to.'</p>';
        $a+=1;
      } else {
        echo '<p>'.$a.' [<a style="color:red;"> erro </a>] '.$to.'</p>';
        $a+=1;
      }
    }
  }
  fclose($file);
}

and to make the wordpress wp_mail() work correctly I have this on my functions.php.

if (! function_exists('email_sender')){
    add_action('phpmailer_init','email_sender');
    function email_sender($mail){
        $mail->SetFrom('[email protected]', 'Tiago');
        $mail->Host = 'smtp-mail.outlook.com';
        $mail->Port = 587;
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = 'STARTTLS';
        $mail->Username = '[email protected]';
        $mail->Password = '1234567';
        $mail->IsSMTP();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.