phpemailzend-frameworkzend-mail

How can I set default setFrom in Zend_Mail


How can I set the default setFrom field with title in Zend_Mail

If I send mail like this

public function sendActivationEmail($id) {
    $mail = new Zend_Mail();
    $mail   ->addTo('emailId1@gmail.com',"Recepient Name")
            ->setFrom("emailId1@gmail.com","Site Name")
            ->setSubject("My Subject")
            ->setBodyText("Some body msg")
            ->setBodyHtml("Some body msg")
            ->send();
}

I get an email in nice format with From Field in mail as "Site Name"

But If I send mail without setFrom, I still get the mail, but the title is "emailId1@gmail.com". I dont want to use SetFrom() just to get "Site From". Is there a way I can set it so that "Site Name" gets picked up by default ?

I have followed this tutorial for setting up my email : http://www.zendcasts.com/introduction-to-zend_mail/2010/02/

Thank you,


Solution

  • If you want to set a default email address for the from attribute, you can use setDefaultFrom() static method like this:

    For example, in your boostrap:

    protected function _initFromMail(){
        // You can get the mail from an init file
        Zend_Mail::setDefaultFrom('emailId1@gmail.com', 'Site Name');
    }
    

    In your function, the from adress mail is automatically fiiled.

    public function sendActivationEmail($id) {
        $mail = new Zend_Mail();
        $mail   ->addTo('emailId1@gmail.com',"Recepient Name")
                ->setSubject("My Subject")
                ->setBodyText("Some body msg")
                ->setBodyHtml("Some body msg")
                ->send();
    }