Pass array into a method

Hey all, not sure what im doing wrong here…

I integrated Swiftmail into my app, so I made a very small wrapper so I could call it without having to construct it in my controllers.

Here are some snippets:


class Email
{        
private $_mail;        
private $_transport;
               
public function __construct()
{                
$this->_transport = \\Swift_SmtpTransport::newInstance(SMTP_HOST, SMTP_PORT, SMTP_SSL)
->setUsername(SMTP_USER)
 ->setPassword(SMTP_PASS);

$this->_mail = \\Swift_Message::newInstance();
}


public function send($subject, &$from, &$to, $body, $part = null, $type = 'text/html', $attachment = null)
{                
// Check to see if we get our array, 
// which we do and get the correct output as expected:
// array(1) { ["myemail@gmail.com"]=> string(12) "My Name" }                
var_dump($to);

 // Give the message a subject                
$this->_mail->setSubject($subject) 
                               
// Set the From address with an associative array                
->setFrom($from)

// Set the To addresses with an associative array                
->setTo($to)

// Give it a body                
->setBody($body);

//------ and the rest

So, setFrom() and setTo() expect an array, from their documentation:


->setFrom(array('john@doe.com' => 'John Doe'))  
->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))

Im passing the array like so:


//From within my controller class im calling send() like:
// Note _mail is NOT the same _mail property in Email()
$to = array('myemail@gmail.com' => 'My Name');
$from = array('myemail@gmail.com' => 'My Name');
$this->_mail->send('This is the subject', $from, $to, 'this is my email body');

However, I get an error, which shows this:
[COLOR=#333333][FONT=Open Sans]Email->send(‘This is the sub…’, Array, Array, 'this is my emai… in

It’s only seeing Array, Array for the To and From…

What am I doing wrong?![/FONT][/COLOR]

Currently your only creating a new Message instance which will never work as you always need a Mailer instance in between the transporter and the message, see the updated code below.

class Email {
	private $_mail;
	private $_transport;
	private $_message;

	/*!
	 * Constructs the transporter and mailer handlers for the email class
	 *
	 * @function __construct
	 * @uses Swift_SmtpTransport
	 * @uses Swift_Mailer
	 */
	public function __construct() {
		// Create a new "Swift_SmtpTransport" instnace
		$this->_transport = Swift_SmtpTransport::newInstance(SMTP_HOST, SMTP_PORT, SMTP_SSL)
			->setUsername(SMTP_USER)
			->setPassword(SMTP_PASS);

		// Create a new "Swift_Mailer" instance
		$this->_mail = Swift_Mailer::newInstance($this->_transport);
	}

	/*!
	 * Sends the email using the "Swift_Message" class
	 *
	 * @function send
	 * @uses Swift_Message
	 */
	public function send($subject, &$from, &$to, $body, $part = null, $type = 'text/html', $attachment = null) {
		// Create a new "Message" instance
		$this->_message = new Swift_Message::newInstance($subject)

			// Set the From address with an associative array
			->setFrom($from)

			// Set the To addresses with an associative array
			->setTo($to)

			// Give it a body
			->setBody($body);

		// Send the message
		if (!$this->_mail($this->_message, $failed)) {
			echo 'The following email\\'s failed to receive the message!<br /><br />' . join('<br />', $failed);
		} else {
			echo 'The email was sent successfully!';
		}
	}
}

ty :slight_smile: got it working now!

Had to make a change


if (!$this->_mail($this->_message, $failed)) { 

//should be

if (!$this->_mail->send($this->_message, $failed)) {