如何在浏览器中添加打开的电子邮件,最简单的方式,php,mailto

How to add open email in browser, in the simplest manner, php, mailto

我正在发送一封电子邮件,其中包含在 wordpress 的 woocomerce 中购买结束时触发的 webhook 中的以下行 mail($to, $subject, $message, $headers); 电子邮件成功触发,但我希望电子邮件有一个 link 在浏览器中打开,以便在 html 中针对特定的 css 打印设计进行格式化。有没有办法自动生成一个 html 文件,我可以为使用 php 和 wordpress 发送的每封电子邮件托管,然后我将分别将 link 添加到那些 html 文件对于发出的电子邮件。我不知道其他解决方案,但也愿意接受其他解决方案。

这就是我现在在 php 文件中构建电子邮件的方式。

$to = $email_user;
    $subject_decoded = 'You received a gift card for stuff';
    $subject = '=?UTF-8?B?' . base64_encode( $subject_decoded ) . '?=';
    $subject1 = 'test';
    $message = '<table border="0" cellspacing="0" cellpadding="0" width="100%" style="width: 100%; border-collapse: collapse;"><tbody><tr><td style="padding: 40px 40px 20px; background-color: #f9f9f9;" align="center"><table border="0" cellspacing="0" cellpadding="0" width="600" style="border-collapse: collapse;"><tbody>';
    $message .= '<tr><td align="center" valign="bottom" style="padding: 0 0 20px;">';
    $message .= '<img src="https://dev.website.com/wp-content/uploads/2019/05/RCDH-ComLogo.png" alt="place" width="600" height="87" style="vertical-align: bottom;" />';
    $message .= '</td></tr>';
    $message .= '<tr><td align="center" style="padding: 10px 40px 20px; background-color: #ffffff; color: #676767; font-family: Helvetica, Arial, sans-serif;">';
    $message .= '<h2 style="font-family: Garamond, serif; font-size: 28px; font-weight: 600; color: #444444;">' . (!empty($gift_card_data['recipient_name']) ? $gift_card_data['recipient_name'] : 'Whoa') . ', you&rsquo;ve got ' . $gift_card_data['gift-card-amount'] . ' to spend at place; | Deane&nbsp;House!</h2>';
    $message .= '<p style="color: #676767;">' . (!empty($gift_card_data['sender']) ? $gift_card_data['sender'] : 'Someone') . ' sent you a gift card' . (!empty($gift_card_data['message']) ? ' with the following message:' : '.') . '</p>';
    if( !empty($gift_card_data['message']) ) {
      $message .= '<p style="color: #676767;"><i><br />' . nl2br($gift_card_data['message']) . '<br /><br /></i></p>';
    }
    $message .= '<img src=" ' . $gift_card_image . '"/>';
    //$message .= '<img src="https://www.barcodesinc.com/generator/image.php?code=' . $response->result[3] . '&style=68&type=C39&width=300&height=50&xres=1&font=4" alt="" />';
    // barcode generator website: https://www.barcodesinc.com/generator/index.php
    $message .= '<p style="color: 676767; font-size: 1.25em;"><b>Card Number:</b> ' . $response->result[3] . '<br /> <b>PIN:</b> ' . $response_reference[1] . '<br /> <b>Card Amount:</b> ' . $response->result[4] . '<br /> <b>Reference Number:</b> ' . $response_reference[0] . '</p>';
    $message .= '</td></tr>';
    $message .= '<tr><td align="center" style="padding: 20px 0 0;">';
    $message .= '<p style="color: #676767;"><b>We look forward to you dining with us!</b></p>';  
    $message .= '</td></tr>';
    $message .= '</tbody></table></td></tr></tbody></table>';
    $headers = "From: Gift Cards <noreply@website.com>\r\n";
    $headers .= "Reply-To: noreply@website\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    mail($to, $subject, $message, $headers);

提前致谢。

这是一个想法:

  • 创建一个包含两个字段的数据库 table:
    • id(非增量自动生成)
    • 正文($message 内容的文本)
  • 创建接受 id 的端点,例如html-email/{id}
  • 使用哈希算法自动生成 ID
  • $message的内容连同id
  • 存入数据库
  • 生成 link 到您的页面 html-email/{id}
  • 将其附加到 $message
  • 发送邮件

从电子邮件本身单击 link 后,您可以加载存储的消息内容并根据需要显示。

是的,存储已发送的电子邮件对我们有短期和长期的好处。将电子邮件存储在服务器的数据库中后,您可以将其作为第二个任务发送。

数据库结构

  • id
  • mail_to
  • mail_body
  • sent_at

在访问 webhook 时,像这样将您的电子邮件详细信息存储在 table 中,其中 mail_body 是您的 html 电子邮件正文。

保存您的电子邮件并获取您的insert_id();电子邮件。并将其附加在您的电子邮件正文底部。 Open email in browser。 Link 像这样:

example.com/view-email.php?id={your-generated-insert-id}

您可以创建 view-mail.php

根据您通过get()方法获得的id,从数据库中查找详细信息并像魅力一样显示在您的浏览器中。

出于安全原因,请务必进行额外检查。

  • 添加 &refer-from=<?php echo base64_encode($user_mail_id)?> 的额外参数 您可以与 id 一起验证,这样就无法猜测下一个 ID,并且会有额外的身份验证层。

You can additionally use this to access sent email list from your admin panel as well, which will be helpful specially for debugging purpose.

奖励积分

如果您使用群发电子邮件,为了优化存储,您可以规范化 table 以将 mail_body 存储一次,所有其他详细信息存储一次,并在另一个 mail_body_id 中引用 table.

示例。

mail_log_record

  • mail_log_id
  • mail_sent_to
  • mail_body_id
  • mail_sent_at

mail_body

  • mail_body_id
  • mail_body_内容
  • mail_body_added_at

通过这种方式,您可以为群发电子邮件节省更多 space。

这里的其他答案有我会使用的方法,但它们是通用的,正如你提到的,这是在 WordPress 中,我想我会给你一个更具体的 WordPress 解决方案。

首先,我会创建一个新的 WordPress post 类型,名为 emailsent_woocommerce_emails 或任何您真正喜欢的类型:

// Customise this post type as your please
function create_email_posttype() {

    register_post_type( 'email',
        array(
            'labels' => array(
                'name' => __( 'Emails' ),
                'singular_name' => __( 'Email' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'emails'),
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_email_posttype' );

对于每个要 stored/sent 的电子邮件,我将以编程方式创建一个新的 'Email' post。 从这里您可以将 post 内容设置为电子邮件的内容,将 slug 设置为随机标识符(md5 post 内容?),甚至可以设置您随机生成的密码。您还可以设置您想要存储的任何其他元数据,例如用户、电子邮件等。

// You might consider setting this password to a hash of the username + date_created or something in order to allow the user the same password for all of their emails.
$slug = md5($email_content);
$post = array(
    'post_content' => $email_content,
    'post_title' => $email_title,
    'post_type' => 'email',
    'post_status' => 'publish',
    'post_password' => md5($slug), // Change this it's insecure, just here as an example
    'post_name' => $slug
);

然后您可以存储此 post,并在电子邮件的末尾附加一个字符串,其中包含创建的 post 的 link 和密码。

$post_id = wp_insert_post( $post );
$link = get_permalink($post_id);
$email_content.= "<a href=\"$link\">View this email in your browser</a> Password: $post_password";

使用此方法,您可以在 WordPress 仪表板中访问和管理所有这些电子邮件。正确地为电子邮件设置密码(如果您愿意)。并且避免必须创建表甚至直接与数据库交互。

从这里进一步扩展此功能并不是一项艰巨的任务,例如创建一个页面,每个用户都可以在其中列出自己的电子邮件等。

请注意,我尚未测试任何此代码。它可能有错误,并且完全没有任何错误检查等。此外,还需要考虑更复杂的问题,例如如何生成密码和 slug。这里的代码只是给个思路