Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
转发一封由php SwiftMailer从mandrill webhook解码的电子邮件_Php_Email_Smtp_Swiftmailer - Fatal编程技术网

转发一封由php SwiftMailer从mandrill webhook解码的电子邮件

转发一封由php SwiftMailer从mandrill webhook解码的电子邮件,php,email,smtp,swiftmailer,Php,Email,Smtp,Swiftmailer,我正在使用mandrill来管理我的网站电子邮件。但我还想使用雅虎或谷歌这样的邮箱,所以我在mandrill中设置了一条路由,转发将发送到的入站电子邮件info@mydomain.com,到我的默认邮箱(myEmail@ymail.com)我写了一个PHP代码,可以接收电子邮件,解码后转发到我的电子邮件中。我使用SwiftMailer发送SMTP电子邮件。 对于没有任何附件的电子邮件来说,它工作得很好。但是附件有一个奇怪的问题。它们传送的很腐败。我无法打开它们。 我进行了彻底的搜索和测试,但不幸

我正在使用mandrill来管理我的网站电子邮件。但我还想使用雅虎或谷歌这样的邮箱,所以我在mandrill中设置了一条路由,转发将发送到的入站电子邮件info@mydomain.com,到我的默认邮箱(myEmail@ymail.com)我写了一个PHP代码,可以接收电子邮件,解码后转发到我的电子邮件中。我使用SwiftMailer发送SMTP电子邮件。 对于没有任何附件的电子邮件来说,它工作得很好。但是附件有一个奇怪的问题。它们传送的很腐败。我无法打开它们。 我进行了彻底的搜索和测试,但不幸的是没有找到问题

<?php   

if(!isset($_POST['mandrill_events'])) {
    echo 'A mandrill error occurred: Invalid mandrill_events';
    exit;
}

// -------------- Receive --------------------------
$mail = array_pop(json_decode($_POST['mandrill_events']));


// ----------------- Send ----------------------------------------
include_once "swiftmailer-master/lib/swift_required.php";

$subject = $mail->msg->subject . " From " . $mail->msg->from_email;
$from = array('info@myDomain.ir' =>'myDomain');
$to = array(
 'myEmail@yahoo.com' => 'Hamed Gh'
);

$transport = Swift_SmtpTransport::newInstance('smtp.mandrillapp.com', 25,tls);
$transport->setUsername('username');
$transport->setPassword('***********');

$swift = Swift_Mailer::newInstance($transport);

$message = new Swift_Message($subject);
$message->setFrom($from);

//I think there is a problem here!!
foreach ($mail->msg->attachments as $attachment) {
    $myType = $attachment->type;
    $myName = $attachment->name;
    $myContent = $attachment->content;

    $attachment = Swift_Attachment::newInstance()
          ->setFilename($myName)
          ->setContentType($myType)
          ->setBody($myContent)
          ;
    $message->attach($attachment);
}

$body = $mail->msg->html;
$message->setBody($body, 'text/html');

$message->setTo($to);

$text = "Mandrill speaks plaintext";
$message->addPart($text, 'text/plain');

if($recipients = $swift->send($message, $failures) )
{
 echo 'Message successfully sent!';
} else {
 echo "There was an error:\n";
 print_r($failures);
}

最后,我找不到SwiftMailer的问题,但我可以用mandrill API满足我的需要

此代码适用于我:

 <?php  
if(!isset($_POST['mandrill_events'])) {
    echo 'A mandrill error occurred: Invalid mandrill_events';
    exit;
}
require 'mandrill/src/Mandrill.php';
// -------------- Receive --------------------------

$mail = array_pop(json_decode($_POST['mandrill_events']));
$attachments = array();
foreach ($mail->msg->attachments as $attachment) {
    $attachments[] = array(
        'type' => $attachment->type,
        'name' => $attachment->name,
        'content' => $attachment->content,
    );
}

$headers = array();
// Support only Reply-to header
if(isset($mail->msg->headers->{'Reply-to'})) {
    $headers[] = array('Reply-to' => $mail->msg->headers->{'Reply-to'});
}

// ----------------- Send ----------------------------------------
try {
    $mandrill = new Mandrill('-------------');
    $message = array(
        'html' => $mail->msg->html,
        'text' => 'Example text content',
        'subject' => $mail->msg->subject . " From " . $mail->msg->from_email,
        'from_email' => 'info@mydomain.com',
        'from_name' => 'info',
        'to' => array(
            array(
                'email' => 'me@yahoo.com',
                'name' => 'me',
                'type' => 'to'
            )
        ),
        'headers' => array('Reply-To' => 'info@mydomain.com'),
        'important' => false,
        'track_opens' => null,
        'track_clicks' => null,
        'auto_text' => null,
        'auto_html' => null,
        'inline_css' => null,
        'url_strip_qs' => null,
        'preserve_recipients' => null,
        'view_content_link' => null,
        'bcc_address' => null,
        'tracking_domain' => null,
        'signing_domain' => null,
        'return_path_domain' => null,
        'merge' => true,
        'attachments' => $attachments
    );
    $async = false;
    $ip_pool = 'Main Pool';
    $send_at = null;
    $result = $mandrill->messages->send($message, $async, $ip_pool, $send_at);

} catch(Mandrill_Error $e) {
    // Mandrill errors are thrown as exceptions
    echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
    // A mandrill error occurred: Mandrill_Unknown_Subaccount - No subaccount exists with the id 'customer-123'
    throw $e;
}
 ?>


也许你应该试试PHPMailer。我试过了,但还有其他问题!为什么
tls
不在引号中<代码>未定义
。可能没关系,只是不安全。我可以看看
JSON
。我对
$mail->msg->attachments
很好奇。当然,它必须是一个JavaScript数组。另外,请确保您的服务器允许附件。