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

问题描述 投票:0回答:1

无论如何,在 php 或 cakephp 中是否有使用 smtp 发送电子邮件的方法,以便我可以将电子邮件发送到封闭/内部邮件列表。例如:

在我的公司,我们有一个电子邮件列表:[电子邮件受保护],只有当有人也使用 [电子邮件受保护] 电子邮件时才能发送该列表。在该电子邮件之外,它将被阻止。所以在cakephp中,无论如何都可以以某种方式获取用户凭据并在公司下发送电子邮件,或者如果是谷歌组,则可以在gmail电子邮件中发送电子邮件

我想我真正想要的是,如何设置cakephp电子邮件,以便它像我直接从电子邮件服务器发送一样发送,如果我使用gmail,那么我希望它看起来像gmail(标题等)

最终的代码应该做的是,我将从用户那里获取他们的电子邮件凭据,并使用它发送邮件。这是我的 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('[email protected]');
            $email->to($recipient);

            $email->smtpOptions = array(
                'port'=>'465',
                'timeout'=>'30',
                'host' => 'ssl://smtp.gmail.com',
                'username'=>'[email protected]', //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;
    }
php email cakephp smtp gmail
1个回答
1
投票
$this->Email->delivery = ...

及以后

$email = new CakeEmail();

在尝试访问它之前,“新对象”语句在哪里? 如果没有它,你就不能直接使用它。 您还同时使用 $this->Email 和 $email。这可能是您的错误(复制和粘贴错误)。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.