使用swiftmailer发送多条消息 - PHP

时间:2013-11-28 19:28:42

标签: swiftmailer php

我正在尝试使用swiftmailer发送电子邮件。我想向填写表格的人发​​送信息给自己。我在下面使用的脚本是发送两条消息中的第一条,但第二条消息从未到达。我该如何解决这个问题?

$transport = Swift_SmtpTransport::newInstance('smtp.mysite.nl', 25)
      ->setUsername('myusername')
      ->setPassword('mypassword')
      ;
$mailer = Swift_Mailer::newInstance($transport);


// Create the message
$message = Swift_Message::newInstance()

  // Set the content type
  /* ->setContentType("text/html"); */

  // Give the message a subject
  ->setSubject("mysubject")

  // Set the From address with an associative array
  ->setFrom(array('email@email.com' => "emailer"))

  // Set the To addresses with an associative array
  ->setTo(array('email@email.com' => "emailer"))

  // Give it a body
  ->setBody('My message', 'text/html');

// Create the message
$message2 = Swift_Message::newInstance()

  // Set the content type
  /* ->setContentType("text/html"); */

  // Give the message a subject
  ->setSubject("mysubject")

  // Set the From address with an associative array
  ->setFrom(array('email@email.com' => "emailer"))

  // Set the To addresses with an associative array
  ->setTo(array('email@email.com' => "emailer"))

  // Give it a body
  ->setBody('My message', 'text/html');

$result = $mailer->send($message, $message2);

简而言之:$message已发送,$message2未发送。我没有从这篇文章中得到任何错误!

2 个答案:

答案 0 :(得分:0)

尝试使用以下内容:

require_once 'lib/swift_required.php';

// Create the Transport
$transport = Swift_SmtpTransport::newInstance('localhost', 25);

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
  ->setFrom(array('john@doe.com' => 'John Doe'))
  ->setBody('Here is the message itself');

// Send the message
$failedRecipients = array();
$numSent = 0;
$to = array('receiver@domain.org', 'other@domain.org' => 'A name');

foreach ($to as $address => $name)
{
  if (is_int($address)) {
    $message->setTo($name);
  } else {
    $message->setTo(array($address => $name));
  }

  $numSent += $mailer->send($message, $failedRecipients);
}

printf("Sent %d messages\n", $numSent);

此类服务通常采用不同的方式发送多封电子邮件。

答案 1 :(得分:0)

尝试一个一个地发送它。

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
        ->setUsername('yourUsername')
        ->setPassword('yourPassword');

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

$message = Swift_Message::newInstance('Your message')
        ->setFrom(array('enquiries@dtect.com' => 'Enquiry'))
        ->setTo(array('enquiries@dtect.com'))
        ->setBody("Body content");

$result = $mailer->send($message);

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

$message2 = Swift_Message::newInstance('Dtect Report Portal - '.$typeOfEnquiry)
        ->setFrom(array('enquiries@dtect.com' => 'Enquiry'))
        ->setTo($emailAddress)
        ->setBody("Dear ".$name.","."\r\n".
                  "Thank you for contacting us"."\r\n".
                  "We will respond to your enquiry as soon as possible"."\r\n".
                  "Regards");

$result2 = $mailer2->send($message2);

希望得到这个帮助。