1

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? … 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!

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 = '<html><body>My HTML Body</body></html>';
$emailModel->toEmail = 'abc@xyz.com';

craft()->email->sendEmail($emailModel);
  • 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 Aug 12 at 8:04
  • 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 Aug 13 at 2:29
  • 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 Aug 13 at 7:24
  • 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 Aug 20 at 7:38
  • 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 Aug 26 at 10:01
0

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.