【问题标题】:cakephp2 how to send email using gmail smtp dynamically to a closed mailing listcakephp2 如何使用 gmail smtp 将电子邮件动态发送到封闭的邮件列表
【发布时间】:2013-07-15 18:58:31
【问题描述】:

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

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

我想我真正想要的是,如何设置 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('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;
    }

【问题讨论】:

    标签: email cakephp smtp gmail cakephp-2.0


    【解决方案1】:
    $this->Email->delivery = ...
    

    以后

    $email = new CakeEmail();
    

    在尝试访问之前,您的“新对象”声明在哪里? 你不能不使用它。 您还同时使用了 $this->Email 和 $email。这可能是您的错误(复制和粘贴错误)。

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

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

    【讨论】:

    • 你是对的,现在错误已经消失了。你知道为什么如果我发送到 1 封电子邮件它可以正常工作 (abc@test.com),但它不适用于 (appsteam@test.com) 等邮件列表
    • 也许您没有这样做的权限(邮件列表可能只允许特定的发件人),但这超出了您的问题范围。
    猜你喜欢
    • 2017-02-09
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    相关资源
    最近更新 更多