Can somebody help me please??

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
keenboy
Forum Newbie
Posts: 1
Joined: Tue Feb 26, 2008 6:57 am

Can somebody help me please??

Post by keenboy »

Hello all,

I am putting together a we based e-mailer which allows us to send monthly newsletters to our customers. I have got the swiftmailer class working correctly so it sends an embedded image using html along with the content of a text area typed by a user.

The problem I have is I cannot seem to send the e-mail to a variable based on the e-mail addresses selected from a MySQL database.

I have read the swiftmailer docs and followed the tutorial to do this however it just won't work. When I enter a single address the e-mail sends fine.

Here is my php:

Code: Select all

<?php
include("dbconnection.php");
   if (!isset($_POST['submit'])):
?>
 
<html>
<head>
</head>
 
</head>
 
<body>
 
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<table align="center" bgcolor="#D6DEE7" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center">
<b><h2>MAILING LIST ADMIN</h2></b>
 
 
Send message:
<select name="to" size="1" style="background-color: #F7F7F7">
<!--<option selected value="all">Entire list-->
<option value="notall">By Category
</select>
<br><br>
Country:
<select name="country" size="5">
<OPTION value="retailer">retailer
<OPTION value="supplier">supplier
<OPTION value="staff">staff
</select>
<br><br>
Title or Subject: <input name="subject" type=text maxlength=100 size=40>
<br><br>
Message:
<textarea wrap name="message" rows=10 cols=45>
</textarea>
<br><br>
<input type=submit name="submit" value="SUBMIT">
 
</td>
</tr>
</table>
</form>
</body>
</html>
 
<?php else:
 
  $to = $_POST['to'];
  $subject = $_POST['subject'];
  $main = $_POST['message'];
  $country = $_POST['country'];
 
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";
require_once "lib/Swift/Plugin/VerboseSending.php";
require_once "lib/Swift/Iterator/MySQLResult.php"; 
$swift =& new Swift(new Swift_Connection_SMTP("localhost", 25));
 
$view =& new Swift_Plugin_VerboseSending_DefaultView();
$swift->attachPlugin(new Swift_Plugin_VerboseSending($view), "verbose");
 
$query = "SELECT email FROM details";
$result = mysql_query($query);
 
$it =& new Swift_Iterator_MySQLResult($result);
 
$recipients =& new Swift_RecipientList();
$recipients->setIterator($it, "bcc"); //or cc, bcc
 
$message =& new Swift_Message("$subject", "$main", "text/html");
$message->attach(new Swift_Message_Part("<img src=\"" . $message->attach(new Swift_Message_Image(new Swift_File("stlogo.jpg"))) . "\" /><br />
$main", "text/html"));
 
if ($swift->send($message, "$recipients", "sender@domain.co.uk"))
{
    echo "Message sent";
}
else
{
    echo "Message failed to send";
}
 
//It's polite to do this when you're finished
$swift->disconnect();
 
?>
<?php endif; ?>
 
 
Here's the line which is not functioning correctly:

Code: Select all

if ($swift->send($message, "$recipients", "sender@domain.co.uk"))
Any help would be gratefully appreciated.

Thanks
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Can somebody help me please??

Post by s.dot »

I think you are looking for batch sending.

Moved to Swift Mailer forum. Also, please use a descriptive subject when posting a new topic to receive a better/faster response!
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Can somebody help me please??

Post by Zoxive »

Code: Select all

if ($swift->send($message, "$recipients", "sender@domain.co.uk"))
"$recipients" is your problem. You are converting an object to a string, and should be getting an error.

Code: Select all

Object of class Swift_RecipientList could not be converted to string
Post Reply