简体   繁体   English

使用swiftmailer发送多个消息-PHP

[英]Sending multiple messages with swiftmailer - PHP

I'm trying to send e-mails using swiftmailer. 我正在尝试使用swiftmailer发送电子邮件。 I want to send a message to someone who fills out a form, and to myself. 我想向填写表格的人和我自己发送消息。 The script I use below is sending the first of the two messages, but the second one never arrives. 我在下面使用的脚本正在发送两个消息中的第一个,但是第二个消息永远不会到达。 How can I solve this? 我该如何解决?

$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);

In short: $message is sent, $message2 is not sent. 简而言之:发送$message ,不发送$message2 I DO NOT GET ANY ERRORS FROM THIS SCRIPT! 我没有从此脚本中得到任何错误!

Try to use something like: 尝试使用类似:

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);

Such services usually have different methods of sending multiple emails. 此类服务通常具有发送多封电子邮件的不同方法。

Try sending it one bye one. 尝试再发送一次。

$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);

Hope this help. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM