I use the SMTP module for sending various notification emails from the websites.

With one domain everything is fine, but with multiple domains one will generally want different SMTP servers for different domains.

That is, one needs some way to specify different "SMTP Authentication Support" settings for each domain:

Example:

Domain 1:

SMTP server: smtp.gmail.com
SMTP port: 465
Use encrypted protocol: Use SSL
Username: noreply@domain-one.com
Password: ****
E-mail from Address: noreply@domain-one.com
E-mail from name: Company 1


Domain 2:

SMTP server: smtp.gmail.com
SMTP port: 465
Use encrypted protocol: Use SSL
Username: noreply@domain-two.com
Password: ****
E-mail from Address: noreply@domain-two.com
E-mail from name: Company 2

How could one achieve that?

Comments

agentrickard’s picture

It appears that SMTP module stores all its data in the {variables} table. You can simply user Domain Settings module to store information per-domain, using the SMTP module's existing settings form.

koyama’s picture

Version: 7.x-3.x-dev » 6.x-2.5
Component: Miscellaneous » - Domain Conf
Status: Active » Fixed

Thanks, agent, for pointing me in the right direction.

In case others need this, the solution was to use hook_domainconf() to expand the "Domain site settings" at
admin/build/domain/conf/2

so you get SMTP settings for the additional domains. I put this in a custom module, mymodule.module, copying almost directly the form from smtp.module:

function mymodule_domainconf() {
  $form['smtp'] = array(
    '#type'  => 'fieldset',
    '#title' => t('SMTP Authentication Support'),
    '#collapsible' => true,
  );
  $form['smtp']['server'] = array(
    '#type'  => 'fieldset',
    '#title' => t('SMTP server settings'),
  );
  $form['smtp']['server']['smtp_host'] = array(
    '#type'          => 'textfield',
    '#title'         => t('SMTP server'),
    '#default_value' => variable_get('smtp_host', ''),
    '#description'   => t('The address of your outgoing SMTP server.'),
  );
  $form['smtp']['server']['smtp_hostbackup'] = array(
    '#type'          => 'textfield',
    '#title'         => t('SMTP backup server'),
    '#default_value' => variable_get('smtp_hostbackup', ''),
    '#description'   => t('The address of your outgoing SMTP backup server. If the primary server can\'t be found this one will be tried. This is optional.'),
  );
  $form['smtp']['server']['smtp_port'] = array(
    '#type'          => 'textfield',
    '#title'         => t('SMTP port'),
    '#size'          => 6,
    '#maxlength'     => 6,
    '#default_value' => variable_get('smtp_port', '25'),
    '#description'   => t('The default SMTP port is 25, if that is being blocked try 80. Gmail uses 465. See !url for more information on configuring for use with Gmail.', array('!url' => l(t('this page'), 'http://gmail.google.com/support/bin/answer.py?answer=13287'))),
  );
  // Only display the option if openssl is installed.
  if (function_exists('openssl_open')) {
    $encryption_options = array(
      'standard' => t('No'),
      'ssl'      => t('Use SSL'),
      'tls'      => t('Use TLS'),
    );
    $encryption_description = t('This allows connection to an SMTP server that requires SSL encryption such as Gmail.');
  }
  // If openssl is not installed, use normal protocol.
  else {
    $encryption_options = array('standard' => t('No'));
    $encryption_description = t('Your PHP installation does not have SSL enabled. See the !url page on php.net for more information. Gmail requires SSL.', array('!url' => l(t('OpenSSL Functions'), 'http://php.net/openssl')));
  }
  $form['smtp']['server']['smtp_protocol'] = array(
    '#type'          => 'select',
    '#title'         => t('Use encrypted protocol'),
    '#options'       => $encryption_options,
    '#default_value' => variable_get('smtp_protocol', 'standard'),
    '#description'   => $encryption_description,
  );

  $form['smtp']['auth'] = array(
    '#type'        => 'fieldset',
    '#title'       => t('SMTP Authentication'),
    '#description' => t('Leave blank if your SMTP server does not require authentication.'),
  );
  $form['smtp']['auth']['smtp_username'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Username'),
    '#default_value' => variable_get('smtp_username', ''),
    '#description'   => t('SMTP Username.'),
  );
  $form['smtp']['auth']['smtp_password'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Password'),
    '#default_value' => variable_get('smtp_password', ''),
    '#description'   => t('SMTP password. Leave blank if you don\'t wish to change it.'),
  );

  $form['smtp']['email_options'] = array(
    '#type'  => 'fieldset',
    '#title' => t('E-mail options'),
  );
  $form['smtp']['email_options']['smtp_from'] = array(
    '#type'          => 'textfield',
    '#title'         => t('E-mail from address'),
    '#default_value' => variable_get('smtp_from', ''),
    '#description'   => t('The e-mail address that all e-mails will be from.'),
  );
  $form['smtp']['email_options']['smtp_fromname'] = array(
    '#type'          => 'textfield',
    '#title'         => t('E-mail from name'),
    '#default_value' => variable_get('smtp_fromname', ''),
    '#description'   => t('The name that all e-mails will be from. If left blank will use the site name of: ') . variable_get('site_name', 'Drupal powered site'),
  );

  return $form;
}

Alternatively, I guess you could also hack the smtp module and put the above function at the end of the file sites/all/modules/smtp.module as function smtp_domainconf()

On a side note I noticed that one has to visit admin/build/domain/conf/2 from the domain one is about to configure. Otherwise it will load the wrong values into the form. For example, if you are configuring SMTP settings for domain-two.com, you should visit:
http://domain-two.com/admin/build/domain/conf/2

and not
http://domain-one.com/admin/build/domain/conf/2

Environment:

  • Domain Access 6.x-2.5
  • SMTP Authentication Support 6.x-1.x-dev
agentrickard’s picture

That's by design. They should not be the 'wrong' values, just the default values (from the primary domain) if you haven't set them for domain two yet.

If you're going to do it this way, I suggest using hook_domainbatch(), which also gives you a batch settings page.

You might also submit a patch to SMTP module.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

drusite’s picture

Issue summary: View changes

Does it work for Drupal 7? Are there any particularities which must be taken into account? Perhaps something must be changed to avoid some problems?

drusite’s picture

Is it possible to use smtp server per role (for every role the different server must be used)