Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

smtp e-mail issue #7700

Closed
cansozeri opened this issue Dec 13, 2017 · 17 comments
Closed

smtp e-mail issue #7700

cansozeri opened this issue Dec 13, 2017 · 17 comments

Comments

@cansozeri
Copy link

cansozeri commented Dec 13, 2017

Affected Version

4.0.0

Description

I can not send e-mail with mail function with using smtp. I can not produce any error too ? I think there must a debug option.

I have added settings to config.yml

SilverStripe\Core\Injector\Injector:
  Swift_Transport:
    class: Swift_SmtpTransport
    properties:
      Host: mail.example.com
      Port: 587
      Encryption: tls
    calls:
      Username: [ setUsername, ['mailadmin@example.com'] ]
      Password: [ setPassword, ['examplepass1'] ]
      AuthMode: [ setAuthMode, ['login'] ]
$email = new Email('mailadmin@example.com', $user_email, $subject, $text);
$email->send()

I tried to get print out swift mailer transport host and other stuff but I can't. It seems null.

@tractorcow
Copy link
Contributor

Not sure what's causing this issue; Someone needs to investigate and determine the cause of this issue.

@tractorcow
Copy link
Contributor

Your config above seems correct after checking the Swift_SmtpTransport class.

You may need to debug the executeCommand manually (var_dump everywhere?).

@dhensby
Copy link
Contributor

dhensby commented Dec 14, 2017

It does work, however you need to make sure you add your configuration into a new file (email.yml) like so:

mysite/_config/email.yml

---
Name: myemailconfig
After:
  - '#emailconfig'
---
SilverStripe\Core\Injector\Injector:
  Swift_Transport:
    class: Swift_SmtpTransport
    properties:
      Host: mail.example.com
      Port: 587
      Encryption: tls
    calls:
      Username: [ setUsername, ['mailadmin@example.com'] ]
      Password: [ setPassword, ['examplepass1'] ]
      AuthMode: [ setAuthMode, ['login'] ]

When I do this I get the following error:

[Emergency] Uncaught Swift_TransportException: Connection could not be established with host mail.example.com [php_network_getaddresses: getaddrinfo failed: Name or service not known #0]
GET /
Line 268 in /vagrant/www/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php

Which feels expected to me

@dhensby
Copy link
Contributor

dhensby commented Dec 14, 2017

I've added a clarification to the docs regarding this too

@cansozeri
Copy link
Author

@dhensby Thanx - but nothing changed. mail->send() returns false and there is no error. I have created email.yml and flush. Still it is not working.. It is very sad that I can not get any output why it is broken or why it returned false.. There must be an option that getting output about what is gonna wrong about it.

@cansozeri
Copy link
Author

cansozeri commented Dec 14, 2017

in Swift_SmtpTransport class var_dump($host) in __construct method prints nothing. I think it is never loaded. I don't know what to do then :=) Am I have to do something else, another config option for email option ?? I didn't understand, the class is not loading at all and there is no error message just returns false.

And interesting thing is I can get $email->getSwiftMessage() message or with $email->debug - I can see sender mail and message before email->send() method. But $email->send() is not working :=)

@dhensby
Copy link
Contributor

dhensby commented Dec 14, 2017

What happens when you debug out Injector::inst()->get(Mailer::class)?

If it's not returning a Swift_SmtpTransport object then your config isn't right and you're not registering the smtp transport correctly.

@cansozeri
Copy link
Author

cansozeri commented Dec 14, 2017

The output is

object(SilverStripe\Control\Email\SwiftMailer)#722 (1) { ["swift":"SilverStripe\Control\Email\SwiftMailer":private]=> object(Swift_Mailer)#730 (1) { ["_transport":"Swift_Mailer":private]=> object(Swift_MailTransport)#726 (3) { ["_extraParams":"Swift_Transport_MailTransport":private]=> string(4) "-f%s" ["_eventDispatcher":"Swift_Transport_MailTransport":private]=> object(Swift_Events_SimpleEventDispatcher)#728 (3) { ["_eventMap":"Swift_Events_SimpleEventDispatcher":private]=> array(5) { ["Swift_Events_CommandEvent"]=> string(28) "Swift_Events_CommandListener" ["Swift_Events_ResponseEvent"]=> string(29) "Swift_Events_ResponseListener" ["Swift_Events_SendEvent"]=> string(25) "Swift_Events_SendListener" ["Swift_Events_TransportChangeEvent"]=> string(36) "Swift_Events_TransportChangeListener" ["Swift_Events_TransportExceptionEvent"]=> string(39) "Swift_Events_TransportExceptionListener" } ["_listeners":"Swift_Events_SimpleEventDispatcher":private]=> array(1) { [0]=> object(SilverStripe\Control\Email\SwiftPlugin)#733 (0) { } } ["_bubbleQueue":"Swift_Events_SimpleEventDispatcher":private]=> array(0) { } } ["_invoker":"Swift_Transport_MailTransport":private]=> object(Swift_Transport_SimpleMailInvoker)#725 (0) { } } } }

@dhensby
Copy link
Contributor

dhensby commented Dec 14, 2017

yep, so it's using Swift_MailTransport and not Swift_SmtpTransport so you haven't registered the transport correctly.

I have tested this with 4.x-dev and can confirm it works with the yaml I've provided.

@cansozeri
Copy link
Author

cansozeri commented Dec 14, 2017

But I have added email.yml as you have described.. What can I do extra ??

My code is very simple ..

use SilverStripe\Control\Email\Email;

$email = new Email('mailadmin@example.com', $user_email, $subject, $text);

$email->send()

I think email.yml is not loaded because of cache maybe I don't know, but I have used ?flush too... My version is 4.0.0

@dhensby
Copy link
Contributor

dhensby commented Dec 14, 2017

What can I do extra ??

You'll need to debug your code and find out why the wrong mailer is being created by Injector, which is likely a service definition issue (ie: the yml you're using)

@cansozeri
Copy link
Author

cansozeri commented Dec 14, 2017

email.yml is

---
Name: myemailconfig
After:
  - '#emailconfig'
SilverStripe\Core\Injector\Injector:
  Swift_Transport:
    class: Swift_SmtpTransport
    properties:
      Host: mail.example.com
      Port: 587
      Encryption: null
    calls:
      Username: [ setUsername, ['example@example.com'] ]
      Password: [ setPassword, ['123456'] ]
      AuthMode: [ setAuthMode, ['login'] ]

@dhensby
Copy link
Contributor

dhensby commented Dec 14, 2017

there's an error in that yaml, it needs to be:

---
Name: myemailconfig
After:
  - '#emailconfig'
+---
SilverStripe\Core\Injector\Injector:
  Swift_Transport:
    class: Swift_SmtpTransport
    properties:
      Host: mail.example.com
      Port: 587
      Encryption: tls
    calls:
      Username: [ setUsername, ['mailadmin@example.com'] ]
      Password: [ setPassword, ['examplepass1'] ]
      AuthMode: [ setAuthMode, ['login'] ]

@cansozeri
Copy link
Author

Thanx. Now the object is Swift_SmtpTransport :=) I still get return false but maybe it is related with something else...

I think it would be nice - that something about debug option that what is wrong while sending mail. (i.e password wrong or other smtp errors )

@cansozeri
Copy link
Author

If anyone looking for something output error like me :=) in abstract class Swift_Transport_AbstractSmtpTransport - _doMailTransaction method - You can get error message

catch (Swift_TransportException $e) {
              var_dump($e->getMessage()); 
                $failedRecipients[] = $forwardPath;//Just adds failedRecipients - Swift_TransportException is more usefull.
            }

@tractorcow
Copy link
Contributor

As an aside, it's interesting that your YML got grouped in with the block header instead of the body... I wonder if we should validate that and raise that error earlier, in silverstripe/config module.

@tractorcow
Copy link
Contributor

Raised at silverstripe/silverstripe-config#27

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants