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!

2 Answers 2

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 = '[email protected]';
$email->subject = "Subject";    
$message = "...";

$email->body = $message;

craft()->email->sendEmail($email);
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 = '[email protected]';

craft()->email->sendEmail($emailModel);
5
  • 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
    Commented Aug 12, 2019 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
    Commented Aug 13, 2019 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
    Commented Aug 13, 2019 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
    Commented Aug 20, 2019 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
    Commented Aug 26, 2019 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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