<p>So basically I solved it like this, but couldn’t apply other SMTP settings than the global ones. But we did a workaround with the replyTo-address, so practically we are fine. If someone knows how to apply other SMTP-settings, feel free to answer. Thanks!</p>

<pre><code>namespace Craft;
$email = new EmailModel();
$emailSettings = craft()-&gt;email-&gt;getSettings();

$email-&gt;toEmail   = $_REQUEST['email'];
$email-&gt;replyTo = 'test@test.com';
$email-&gt;subject = "Subject";    
$message = "...";

$email-&gt;body = $message;

craft()-&gt;email-&gt;sendEmail($email);
</code></pre>

CC BY-SA 4.0
2019-09-02T07:28:13.060Z
2019-09-02T07:28:13.060Z
romadev
<p>I don't think I'm following the 2nd half of your post, but a simple example of sending an email in Craft 2 looks like this:</p>

<pre><code>$emailModel = new Craft\EmailModel();

$emailModel-&gt;subject = 'My Subject';
$emailModel-&gt;body = 'My Body';
$emailModel-&gt;htmlBody = '&lt;html&gt;&lt;body&gt;My HTML Body&lt;/body&gt;&lt;/html&gt;';
$emailModel-&gt;toEmail = 'abc@xyz.com';

craft()-&gt;email-&gt;sendEmail($emailModel);
</code></pre>

CC BY-SA 4.0
2019-08-12T08:04:50.513Z
Thanks for writing!

In the meantime I have already tried this way, but I get a internal server error «Class 'EmailModel' not found». 

Regarding the 2nd half of my post: I meant how I could change the global settings with server etc. and its from-address to something defined completely in this code?
romadev
CC BY-SA 4.0
2019-08-13T02:29:44.483Z
Edited the answer to fix your internal server error.  Regarding the 2nd part, you want to permanently change the Craft install's email settings programatically?  Or just temporarily for this one request?
Brad Bell
CC BY-SA 4.0
2019-08-13T07:24:56.987Z
Thanks! Now the error changes to "Call to undefined function craft()" … About your question: no, no permanent change, but for our two custom plugins I need to change the smtp-from-address to other settings than the global ones, because it needs to go to different recipients.
romadev
CC BY-SA 4.0
2019-08-20T07:38:20.367Z
Anyone? I know maybe it is not such a contemporary problem with Craft 2, but we will have to solve this problem soon … and I can’t find any hints with searching.
romadev
CC BY-SA 4.0
2019-08-26T10:01:24.043Z
So I added the essential "namespace Craft;" at the top and something seems to be different, the following errors are smaller ones. It seems quite complicated to build an easy email service, I hope I will manage alone, but it will take long.
romadev
CC BY-SA 4.0
2019-08-09T21:10:15.563Z
2019-08-13T02:28:04.877Z
2019-08-13T02:28:04.877Z
Brad Bell
<p>I have looked a lot for this but cannot find any dedicated solution. Basically I want to do this: <a href="https://craftcms.stackexchange.com/q/23804/6034">How to send email from Craft 3 custom plugin?</a> … but for a Craft 2 plugin.</p>

<p>A how-to for the global email settings would be already helpful, but ideally we would like to use not those SMTP settings in the global email settings but dedicated ones for another email address. Can this be done?</p>

<p>Thanks!</p>

<p>edit August 8th: no ideas? Any help is welcome!</p>

<plugin-development><plugins><emails><craft2>
Send email with dedicated SMTP settings from a Craft 2 plugin
# Send email with dedicated SMTP settings from a Craft 2 plugin

(Asked by: username_0 on 2019-08-05)

I have looked a lot for this but cannot find any dedicated solution. Basically I want to do this: [How to send email from Craft 3 custom plugin?](https://craftcms.stackexchange.com/q/23804/6034) … but for a Craft 2 plugin.

A how-to for the global email settings would be already helpful, but ideally we would like to use not those SMTP settings in the global email settings but dedicated ones for another email address. Can this be done?

Thanks!

edit August 8th: no ideas? Any help is welcome!

**Tags:** plugin-development, plugins, emails, craft2


---

## 2 answers

### Answer by username_0 on 2019-09-02. Score: 1

So basically I solved it like this, but couldn’t apply other SMTP settings than the global ones. But we did a workaround with the replyTo-address, so practically we are fine. If someone knows how to apply other SMTP-settings, feel free to answer. Thanks!
namespace Craft;
$email = new EmailModel();
$emailSettings = craft()->email->getSettings();

$email->toEmail   = $_REQUEST['email'];
$email->replyTo = 'test@test.com';
$email->subject = "Subject";    
$message = "...";

$email->body = $message;

craft()->email->sendEmail($email);


---

### Answer by username_1 on 2019-08-09. Score: 0

I don't think I'm following the 2nd half of your post, but a simple example of sending an email in Craft 2 looks like this:
$emailModel = new Craft\EmailModel();

$emailModel->subject = 'My Subject';
$emailModel->body = 'My Body';
$emailModel->htmlBody = 'My HTML Body';
$emailModel->toEmail = 'abc@xyz.com';

craft()->email->sendEmail($emailModel);

> **Comments:**
> **username_0**: Thanks for writing!  In the meantime I have already tried this way, but I get a internal server error «Class 'EmailModel' not found».   Regarding the 2nd half of my post: I meant how I could change the global settings with server etc. and its from-address to something defined completely in this code?  
> **username_1**: Edited the answer to fix your internal server error.  Regarding the 2nd part, you want to permanently change the Craft install's email settings programatically?  Or just temporarily for this one request?  
> **username_0**: Thanks! Now the error changes to "Call to undefined function craft()" … About your question: no, no permanent change, but for our two custom plugins I need to change the smtp-from-address to other settings than the global ones, because it needs to go to different recipients.  
> **username_0**: Anyone? I know maybe it is not such a contemporary problem with Craft 2, but we will have to solve this problem soon … and I can’t find any hints with searching.  
> **username_0**: So I added the essential "namespace Craft;" at the top and something seems to be different, the following errors are smaller ones. It seems quite complicated to build an easy email service, I hope I will manage alone, but it will take long.  

---

