minutoms
minutoms

Reputation: 1

not able to send email using swift mailer

I am new to php. I was trying to send mail using following code. my reference document is this. I think its related to my result portion. can anyone help with this? My error is given below

Warning: Invalid argument supplied for foreach() in /home/mysite/public_html/sendmail.php on line 50

Warning: Invalid argument supplied for foreach() in /home/mysite/public_html/sendmail.php on line 70

MY FULL CODE IS GIVEN BELOW PLEASE GUID ME

require_once 'lib/swift_required.php';
include('database.php');
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.mydomain.com', 25)
    ->setUsername('me@mydomain.com')
    ->setPassword('mypassword');

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

// Send the message
//$result = $mailer->send($message);

//database connection part
$con = mysql_connect("localhost","username","password");
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $con);

$sql = "SELECT email,name FROM email_test";     
$result = mysql_query($sql);

//tested the results, works fine 
while($row = mysql_fetch_array($result)) {
    echo "--".$row['email']."---";
}

//using result to get enail and user name
$replacements = array();
foreach ($result as $user) {
    $replacements[$user['email']] = array(
        '{email}'=>$user['email'],
        '{name}'=>$user['name']
    );
}

$decorator = new Swift_Plugins_DecoratorPlugin($replacements);
$mailer->registerPlugin($decorator);

$message = Swift_Message::newInstance()
    ->setSubject('Important notice for {name}')
    ->setBody("Hello {name}, we have reset your password");

foreach ($result as $user) {
    $message->addTo($user['email']);
}

Upvotes: 0

Views: 334

Answers (1)

Robbie
Robbie

Reputation: 17720

  1. As in comments: $result is a mysql_resource, not an array. You need to run fetch_row on that resource to actually get the details. Mentioned in comments (can you update code to what you have now?)

  2. When you run through the results the second time, you need to "reset" the results set to start at the top. You can do this with mysql_data_seek($result, 0) to say go back to the top.

Upvotes: 2

Related Questions