2

我们的网站使用Kohana并且php我们正在使用sendgrid发送交易电子邮件。由于gmail我们遇到了大量垃圾邮件问题,我们只发送选择加入的电子邮件并且打开​​率很高。潜在问题之一是我们的电子邮件似乎在标题中有两个返回路径:

  1. 被我们设定在Kohana
  2. 被 插入sendgrid

Sendgrid表示当他们发送消息时,为了处理退回管理,他们接管了“信封”。但是我们想不出办法让 Kohana 不插入这个。有什么建议么?代码示例:Kohana 使用 Swift 发送邮件。我们现在如何发送它们如下。我们已尝试删除回复通过

$message->headers->set('reply-to', '');

但它似乎不起作用。有趣的是,将其设置为非空值会改变它,但似乎没有办法完全摆脱它。

此功能的完整代码:

/**
 * Send an email message.
 *
 * @param   string|array  recipient email (and name), or an array of To, Cc, Bcc names
 * @param   string|array  sender email (and name)
 * @param   string        message subject
 * @param   string        message body
 * @param   boolean       send email as HTML
 * @param   string        Reply To address. Optional, default null, which defaults to From address
 * @return  integer       number of emails sent
 */
public static function send($category, $to, $from, $subject, $message, $html = FALSE, $replyto = null)
{
    // Connect to SwiftMailer
    (email::$mail === NULL) and email::connect();

    // Determine the message type
    $html = ($html === TRUE) ? 'text/html' : 'text/plain';

// Append mixpanel tracking pixel to html emails
if ($html) {
  $mixpanel_token = FD_DEV_MODE ? "08c59f4e26aa718a1038459af75aa559" : "d863dc1a3a6242dceee1435c0a50e5b7";
  $json_array = '{ "event": "e-mail opened", "properties": { "distinct_id": "' . $to . '", "token": "' . $mixpanel_token . '", "time": ' . time() . ', "campaign": "' . $category . '"}}';
  $message .= '<img src="http://api.mixpanel.com/track/?data=' . base64_encode($json_array) . '&ip=1&img=1"></img>';
}

    // Create the message
    $message = new Swift_Message($subject, $message, $html, '8bit', 'utf-8');

// Adding header for SendGrid, added by David Murray
$message->headers->set('X-SMTPAPI', '{"category" : "' . $category . '"}');

    if (is_string($to))
    {
        // Single recipient
        $recipients = new Swift_Address($to);
    }
    elseif (is_array($to))
    {
        if (isset($to[0]) AND isset($to[1]))
        {
            // Create To: address set
            $to = array('to' => $to);
        }

        // Create a list of recipients
        $recipients = new Swift_RecipientList;

        foreach ($to as $method => $set)
        {
            if ( ! in_array($method, array('to', 'cc', 'bcc')))
            {
                // Use To: by default
                $method = 'to';
            }

            // Create method name
            $method = 'add'.ucfirst($method);

            if (is_array($set))
            {
                // Add a recipient with name
                $recipients->$method($set[0], $set[1]);
            }
            else
            {
                // Add a recipient without name
                $recipients->$method($set);
            }
        }
    }

    if (is_string($from))
    {
        // From without a name
        $from = new Swift_Address($from);
    }
    elseif (is_array($from))
    {
        // From with a name
        $from = new Swift_Address($from[0], $from[1]);
    }

    // Reply To support, not standard in Swift, added by Soham
    if (!$replyto) $replyto = $from;

    $message->setReplyTo($replyto);

    return email::$mail->send($message, $recipients, $from);
}
4

2 回答 2

1

这不是一个真正的 Kohana 问题,而是一个 Swiftmailer 问题,因为 Swiftmailer 不是 Kohana 框架的标准。根据Swiftmailer 文档,您可以明确设置/获取 Return-Path:

$message->setReturnPath('bounces@address.tld');

希望这会有所帮助!

于 2013-02-06T10:53:58.770 回答
0

我只想说谢谢你间接地为我提供这个解决方案..

    // Adding header for SendGrid, added by David Murray
$message->headers->set('X-SMTPAPI', '{"category" : "INSERT CATEGORY HERE"}');

Sendgrid 网站上的 X-SMTPAPI 使用文档很烂。

于 2018-02-01T03:01:15.887 回答