SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot Gman's Avatar
    Join Date
    Jan 2002
    Location
    Sarasota, FL
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    PHP Code:
    class Email
    {        
    private 
    $_mail;        
    private 
    $_transport;
                   
    public function 
    __construct()
    {                
    $this->_transport = \Swift_SmtpTransport::newInstance(SMTP_HOSTSMTP_PORTSMTP_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:
    PHP Code:
    ->setFrom(array('john@doe.com' => 'John Doe'))  
    ->
    setTo(array('receiver@domain.org''other@domain.org' => 'A name')) 
    Im passing the array like so:

    PHP Code:
    //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:
    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?!

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

    PHP Code:
    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_HOSTSMTP_PORTSMTP_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!';
            }
        }

    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  3. #3
    SitePoint Zealot Gman's Avatar
    Join Date
    Jan 2002
    Location
    Sarasota, FL
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ty got it working now!

    Had to make a change
    PHP Code:
    if (!$this->_mail($this->_message$failed)) { 

    //should be

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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •