0

我有一个表单,用户可以在其中输入他们的电子邮件和朋友的电子邮件。如果电子邮件验证通过,我的控制器将使用电子邮件类向朋友发送消息。我想让朋友收到一封来自用户但无法正常工作的电子邮件。如果我将发件人设置为静态的,例如“myname@mydomain.com”,我只能成功发送电子邮件。这是我的表单:

<?php
    echo $this->Form->create('User', array( 'action' => 'invite',
                                    'controller' => 'users')  
                                    );
    echo $this->Form->input('User.from', array('label'=>"Your email",'value'=>'your email','class'=>"",'type'=>'email'));
    echo $this->Form->input('User.to', array('label'=>"Your friend's", 'value'=>"your friend's email",'class'=>"",'type'=>'email'));
    echo $this->Form->end(array('label'=>'Invite', 'class'=>'special-button use-transition')); 
?>

然后我的用户控制器处理表单。这很成功,因为我可以让应用程序将电子邮件发送到有效的电子邮件地址。但是,如果我尝试将 $from 设置为用户的电子邮件地址,它不会发送任何内容。如何用户输入的电子邮件地址发送此电子邮件?以下是用户控制器的相关部分:

// tell app to use Cake Email
App::uses('CakeEmail', 'Network/Email'); 

public function invite(){
    if ($this->request->is('post')) {
        // Get data from the form and send an email
        $to = $this->request->data['User']['to'];
        $from = $this->request->data['User']['from'];
        $subject = "Some text for the subject line";
        $message = "Some text for the message";
        // I use this data to send an email but it won't work unless
        // I change $from to something static like so:
        $from = "myemail@mydomain.com";
        $this->send($to, $from, $subject, $message);
        // redirect on success not shown...
    }
}

// Send function takes the to/from/subject/message and sends it
public function send($to, $from, $subject, $msg) {
    $email = new CakeEmail();
    $email->template('welcome')
          ->emailFormat('html')
          ->from($from)
          ->to($to)
          ->subject($subject);
    if ($email->send($msg)){
        return true;
    }

}
4

1 回答 1

0

我建议您也尝试收集发件人的姓名并添加到来自字段。

所以你的表格应该是这样的:

<?php
    echo $this->Form->create('User', array( 'action' => 'invite',
                                'controller' => 'users')  
                                );
 echo $this->Form->input('User.fromName', array('label'=>"Your Name", 'value'=>"your name",'class'=>""));
echo $this->Form->input('User.from', array('label'=>"Your email",'value'=>'your email','class'=>"",'type'=>'email'));
echo $this->Form->input('User.to', array('label'=>"Your friend's", 'value'=>"your friend's email",'class'=>"",'type'=>'email'));
echo $this->Form->end(array('label'=>'Invite', 'class'=>'special-button use-transition')); 
?>

在您的邀请函数中,让 $formMail 如下所示:

 $fromMail = array($this->request->data['User']['from'] => $this->request->data['User']['fromName']);

希望这能奏效

于 2012-10-09T12:32:49.823 回答