Send email using gmail smtp in zend?

10,694

Solution 1

This is from our application.ini

resources.mail.transport.type = Zend_Mail_Transport_Smtp
resources.mail.transport.host = "smtp.gmail.com"
resources.mail.transport.port = 587
resources.mail.transport.auth = "login"
resources.mail.transport.username = "[email protected]"
resources.mail.transport.password = "password"
resources.mail.transport.ssl = "tls"

And it "Just works (tm)"!

Solution 2

You can try with: ssl = tls or port = 587

Share:
10,694
Awan
Author by

Awan

Upvoter

Updated on June 18, 2022

Comments

  • Awan
    Awan almost 2 years

    Possible Duplicate:
    email zend framwork smtp

    I have following configuration:

    smtp.type = Zend_Mail_Transport_Smtp
    smtp.smtpServer = "smtp.gmail.com"
    smtp.username = "[email protected]"
    smtp.password = "dddd"
    smtp.email = "[email protected]"
    smtp.port = "587"
    smtp.ssl = "tls"
    smtp.auth = "login"
    

    I am getting following error:

    5.7.0 Must issue a STARTTLS command first. 74sm813723wem.41
    

    My COde:

    public function sendEmail( $mailData, $bootstrap = null ) {
    
            // Get SMTP server configurations
            if ( $bootstrap == null ) {
                $front = Zend_Controller_Front::getInstance();
                $bootstrap = $front->getParam('bootstrap');
            }
            $smtpSettings = $bootstrap->getOption('smtp');
    
            print_r($smtpSettings);
    
            // Only pass username password settings if the authentication is required.
            if ( $smtpSettings['auth'] == 'login' ) {
    
                $config = array('ssl' => $smtpSettings['ssl'],
                            'username' => $smtpSettings['username'],
                            'password' => $smtpSettings['password']);
    
                $transport = new Zend_Mail_Transport_Smtp( $smtpSettings['smtpServer'], $config );
            } else {
                $transport = new Zend_Mail_Transport_Smtp( $smtpSettings['smtpServer'] );
            }
    
            $mail = new Zend_Mail( 'utf-8' );
    
            try {
    
                if ( $mailData['user'] == true ) { 
                    $mail->setFrom( $mailData['from'], $mailData['fromName'] );
    
                } else {
                      $mail->setFrom( $smtpSettings['email'], "eCHDP" );
                }
    
                // Do we have a single reciepent or multiple receipents?
                if ( !is_array($mailData['to']) ) {
                    $mail->addTo( $mailData['to'] , $mailData['toName'] );
                } else {
                    // We have multiple receipents. Add all of them.
                    foreach ( $mailData['to'] as $id => $value ) {
                        $mail->addTo( $value , $mailData['toName'][$id] );
                    }
                }
    
                $mail->setSubject( $mailData['subject'] );
                $mail->setBodyHtml( $mailData['body'] );
    
                // If attachment found then attach
                if ( $mailData['attachment'] ) {
                    $attach = new Zend_Mime_Part( file_get_contents( $mailData['attachment'] ) );
                    $attach->type = 'application/pdf';
                    $attach->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;
                    $attach->filename = 'Invoice.pdf';
                    $mail->addAttachment( $attach );
                }
    
                $mail->send( $transport );
    
                return true;
    
            } catch ( Exception $e ) {
                echo "Error sending Email : ";
                $logger = Zend_Registry::get('Logger');
                $logger->err($e->getMessage());
                echo $e->getMessage() . "\n\n\n";
                return false;
            }
    
        } 
    

    Can someone guess that what is the error ? I can post code as well if required.

    Thanks

    • Piskvor left the building
      Piskvor left the building almost 13 years
      I'm guessing that you must issue a STARTTLS command first ;) No, seriously: you are using ssl = ssl, you should try ssl = tls - those are two different methods of securing the connection to the server.
  • Awan
    Awan almost 13 years
    OK. Let me try this but where are you using this resources.mail.transport.type ??
  • Phliplip
    Phliplip almost 13 years
    Ehh.. in application.in :) Do you use ONLY Zend_Mail or is it a fullblown ZF site? framework.zend.com/manual/en/learning.quickstart.html Anyways, you should be able to use our config values in your specifications.
  • Jon Skarpeteig
    Jon Skarpeteig almost 13 years
    The mail resource simply autoregister to Zend_Mail when used with Zend MVC as a mail transport
  • Awan
    Awan almost 13 years
    I tried this setting and error is changed: 5.5.1 Authentication Required. Learn more at 5.5.1 http://mail.google.com/support/bin/answer.py?answer=14257 h43sm824480wes.35
  • Awan
    Awan almost 13 years
    @Phliplip: I am running it in Zend environment but I dont have "resources.mail.transport.type = Zend_Mail_Transport_Smtp " in my application.ini
  • Phliplip
    Phliplip almost 13 years
    This means wrong username/password combination.
  • Awan
    Awan almost 13 years
    I am using correct username/password combination. I am successfully login to gmail using my username and password.
  • Awan
    Awan almost 13 years
    If you paste your mail sending code. May be it will help me..
  • Phliplip
    Phliplip almost 13 years
    @Awan: What if you paste your code, then we can help you :)
  • Awan
    Awan almost 13 years
    OK review my question again...
  • Phliplip
    Phliplip almost 13 years
    @Awan: You need to add port and auth to your $config array. auth is the authentificationType used. In this case 'login', it could be 'digest' in other cases.