cakephp2如何使用gmail smtp动态发送电子邮件到封闭的邮件列表

时间:2013-07-15 18:58:31

标签: email cakephp smtp gmail cakephp-2.0

无论如何在php或cakephp中,使用smtp发送电子邮件,以便我可以发送电子邮件到封闭/内部邮件列表。 e.g:

在我的公司,我们有一个电子邮件列表:appsteam@company.com,只有当一个人也使用xxx@company.com电子邮件时才能发送。在该电子邮件之外,它将被阻止。所以在cakephp中,无论如何以某种方式获取用户凭证并在公司下发送电子邮件或者在gmail电子邮件中,如果它是谷歌组

我认为我真正想要的是,如何设置cakephp电子邮件,使其发送就像我直接从电子邮件服务器发送,如果我使用gmail,那么我希望它看起来像gmail(标题等)< / p>

最终的代码将会做什么,我会从用户那里获取他们的电子邮件的凭证,并使用它发送邮件。这是我的AbcComponent.php代码

/*
 * Abc components
 */
class AbcComponent extends Component {
    public $options = array();

    /**
     *  Constructor
     **/ 
    public function __construct(ComponentCollection $collection, $options = array()){
        parent::__construct($collection,$options);
        $this->options = array_merge($this->options, $options);
    }


    public function send_link ($user_email = null, $recipients = null, $subject = null, $message = null, $group_id = null, $user_id = null, $email_id = null, $download_password = null) {

        $final_subject = $subject;
        $final_recipients = $recipients;
        $final_message = $message;
        App::uses('CakeEmail', 'Network/Email');            
        $recipients = str_replace(' ', '', $recipients);
        $recipients = explode(',', $recipients);
        $recipient_queue_num = 1;
        //Send the email one by one
        foreach ($recipients as $recipient) :           
            $email = new CakeEmail();
                    $email->delivery = 'smtp';
            //$email->from($user_email);
            $email->from('xxxxx@gmail.com');
            $email->to($recipient);

            $email->smtpOptions = array(
                'port'=>'465',
                'timeout'=>'30',
                'host' => 'ssl://smtp.gmail.com',
                'username'=>'xxxxx@gmail.com', //this will be later grab from dbase based on user
                'password'=>'password',
            );


            $email->subject($final_subject);
            $email->template('download_link_email');
            $email->emailFormat('both');
            $email->viewVars(array('final_message' => $final_message));         
            if($email->send()) {
                debug('email is sent');
            } else {
                debug('email is not sent');
            }

            //queue number increase for hashing purpose
            $recipient_queue_num++;
        endforeach;
    }

1 个答案:

答案 0 :(得分:1)

$this->Email->delivery = ...

以后

$email = new CakeEmail();

在尝试访问之前,您的“新对象”语句在哪里? 你不能在没有它的情况下使用它。 您还使用$ this-&gt;电子邮件和$ email。这可能是你的错误(复制和粘贴错误)。

$this->Email = new CakeEmail();
$this->Email->delivery = ...
...

“间接修改重载属性”通常始终是您尝试访问的非声明对象的指示符。