Note that there are some explanatory texts on larger screens.

plurals
  1. POSend Email in CakePHP afterSave
    primarykey
    data
    text
    <p>I'm trying to create an email component/model that will add an email to the database (with certain fields like, to, from, subject, message, created, modified, etc).</p> <p>AFTER the data has been sucessfully saved (which it currently does), I'd like to actually send the message.</p> <p>I figure this would be easiest with an afterSave() function, but I cannot get the email to send.</p> <p>Here is some relevant code:</p> <p>Email Model</p> <pre><code>&lt;?php class Email extends AppModel { var $name = 'Email'; var $displayField = 'subject'; function afterSave() { $this-&gt;Email-&gt;to = $this-&gt;data['Email']['email']; $this-&gt;Email-&gt;subject = $this-&gt;data['Email']['subject']; $this-&gt;Email-&gt;replyTo = $this-&gt;data['Email']['email']; $this-&gt;Email-&gt;from = 'Private Message &lt;' . $this-&gt;data['Email']['email'] . '&gt;'; //$this-&gt;Email-&gt;template = 'simple_message'; $this-&gt;Email-&gt;send($this-&gt;data['Email']['email_text']); } } </code></pre> <p>add.ctp for email</p> <pre><code>&lt;div class="universities form"&gt; &lt;?php echo $this-&gt;Form-&gt;create('Email');?&gt; &lt;fieldset&gt; &lt;legend&gt;&lt;?php __('Add Email'); ?&gt;&lt;/legend&gt; &lt;?php echo $this-&gt;Form-&gt;input('subject'); echo $this-&gt;Form-&gt;input('email_text'); echo $this-&gt;Form-&gt;hidden('email', array('value' =&gt; $this-&gt;params['named']['contact_email'])); echo $this-&gt;Form-&gt;hidden('user_from', array('value' =&gt; $this-&gt;Session-&gt;read('User.id'))); echo $this-&gt;Form-&gt;hidden('created', array('value' =&gt; date("Y-m-d"))); echo $this-&gt;Form-&gt;hidden('modified', array('value' =&gt; date("Y-m-d"))); ?&gt; &lt;/fieldset&gt; &lt;?php echo $this-&gt;Form-&gt;end(__('Submit', true));?&gt; &lt;/div&gt; </code></pre> <p>Controller save code:</p> <pre><code> function add() { if (!empty($this-&gt;data)) { $this-&gt;Email-&gt;create(); // pr($this-&gt;data); // die; if ($this-&gt;Email-&gt;save($this-&gt;data)) { $this-&gt;Session-&gt;setFlash(__('The email has been saved', true)); } else { $this-&gt;Session-&gt;setFlash(__('The email could not be saved. Please, try again.', true)); } } } </code></pre> <p>Error I am getting on trying to send:</p> <pre><code>Fatal error: Call to undefined method stdClass::send() in /Users/[USER]/Sites/example_app/app/models/email.php on line 14 </code></pre> <p>New Controller Code:</p> <pre><code>function add() { if (!empty($this-&gt;data)) { $this-&gt;Email-&gt;create(); // pr($this-&gt;data); // die; if ($this-&gt;Email-&gt;save($this-&gt;data)) { $this-&gt;Session-&gt;setFlash(__('The email has been saved', true)); function _sendMail() { $this-&gt;Email-&gt;to = $this-&gt;data['Email']['email']; $this-&gt;Email-&gt;subject = $this-&gt;data['Email']['subject']; $this-&gt;Email-&gt;replyTo = $this-&gt;data['Email']['email']; $this-&gt;Email-&gt;from = 'Private Message &lt;' . $this-&gt;data['Email']['email'] . '&gt;'; $this-&gt;Email-&gt;sendAs = 'text'; //Send as 'html', 'text' or 'both' (default is 'text') $email-&gt;send(); } $this-&gt;_sendMail(); } else { $this-&gt;Session-&gt;setFlash(__('The email could not be saved. Please, try again.', true)); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload