如何发送带有wp_mail且电子邮件正文上带有图像的电子邮件

时间:2021-09-01 作者:tiago calado

我可以使用wp\\U mail发送电子邮件,但是身体上的任何图像都不会显示在收到的电子邮件上。有没有办法在邮件中发送图像

这是用于发送电子邮件的代码:

$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);
此代码可以工作并发送电子邮件,但是如果我将图像标记与图像的src放在一起,图像将不会显示,也尝试将图像标记发送到body标记内,但结果仍然相同。

任何人

1 个回复
最合适的回答,由SO网友:tiago calado 整理而成

对于那些有相同问题的人,这是我的inteire代码,我必须将img标记放在主题正文标记内,而且一旦我使用$\\u POST,链接就不起作用了,我就使用了一个单词;[图像M]”;替换为img标签和上传的链接。

<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);
}
为了使wordpress wp\\u mail()正常工作,我在函数中使用了这个。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();
    }
}

相关推荐

Cron not sending wp-mail()

我对cron计划功能有问题,它不想用wp_mail() 作用This is my code:它被放置在函数中。php<?php add_filter(\'cron_schedules\', \'add_review_invites_schedule\'); function add_review_invites_schedule($schedules){ $schedules[\'send_review_invites_interva