APIs & Integrations

PiotrM
Member

SMTP token in Swift mailer

SOLVE

Hello,

Is it possible to use SMTP API token (user name and password) in Swift mailer settings to send an emails from my website?

0 Upvotes
1 Accepted solution
IsaacTakushi
Solution
HubSpot Employee
HubSpot Employee

SMTP token in Swift mailer

SOLVE

Hi, @PiotrM.

 

Thanks for your patience.

 

I see that you're setting $port = 587 and $encryption = SSL. Would you mind trying the following combinations and let me know if either work?

  1. $port = 587 and $encryption = TLS
  2. $port = 465 and $encryption = SSL

 

Isaac Takushi

Associate Certification Manager

View solution in original post

0 Upvotes
13 Replies 13
pawel-trauth
Participant

SMTP token in Swift mailer

SOLVE

For future reference:

 

TCP with port 25 doesn't work as of today (Connection timed out)

TLS with port 587 doesn't work as of today (Expected response code 220 but got an empty response)

SSL with port 465 works

 

Would be good to see that reflected somewhere in the documentation or when setting the tokens

0 Upvotes
IsaacTakushi
HubSpot Employee
HubSpot Employee

SMTP token in Swift mailer

SOLVE

Welcome, @PiotrM.

 

Yes. According to this post, other users have successfully sent transactional emails through SwiftMailer using HubSpot SMTP API tokens.

Isaac Takushi

Associate Certification Manager
0 Upvotes
PiotrM
Member

SMTP token in Swift mailer

SOLVE

Hi Issac,

Thank you for your feedback, unfortunatelly I had no luch with it. Swift Mailer throws me an error message: Connection could not be established with host smtp.hubapi.com. Any idea what could be the reson?

0 Upvotes
IsaacTakushi
HubSpot Employee
HubSpot Employee

SMTP token in Swift mailer

SOLVE

Hi, @PiotrM.

 

I don't have any ideas at the moment, but let's try to dig in further.

 

Per the other post:

  1. Can you use the Logger plugin for SwiftMailer to obtain a log of the SMTP exchange?
  2. Would you share a sample snippet of code so we know how you’re configuring SwiftMailer?

Isaac Takushi

Associate Certification Manager
0 Upvotes
PiotrM
Member

SMTP token in Swift mailer

SOLVE

Hi Isaac,

 

Thank you for your reply and sorry for my late feedback. I was able to add logger plugin and this is what I get in the log now:

++ Starting Swift_SmtpTransport
!! Connection could not be established with host smtp.hubapi.com [ #0] (code: 0)

so for some reason I can't connect to smtp.

0 Upvotes
IsaacTakushi
HubSpot Employee
HubSpot Employee

SMTP token in Swift mailer

SOLVE

Hey, @PiotrM.

 

Thanks for sharing that. Unfortunately,  I don't think it gets us any closer to a solution.

 

Per this post in the other thread, could you share a snippet of the code you're using to configure SwiftMailer? Also, could you confirm the version you're using?

Isaac Takushi

Associate Certification Manager
0 Upvotes
PiotrM
Member

SMTP token in Swift mailer

SOLVE

Hi Isaac,

 

Once again thank you for your help. The appication we develope is build with Drupal 8. To send an email we use Swift Mailer module (https://www.drupal.org/project/swiftmailer) and it works fine when I use google smtp for example. This is the module function that sends an email and bellow values of the variables:

 

public function mail(array $message) {
    try {

      // Create a new message.
      $m = Swift_Message::newInstance();

      // Not all Drupal headers should be added to the e-mail message.
      // Some headers must be suppressed in order for Swift Mailer to
      // do its work properly.
      $suppressable_headers = swiftmailer_get_supressable_headers();

      // Keep track of whether we need to respect the provided e-mail
      // format or not.
      $respect_format = $this->config['message']['respect_format'];

      // Process headers provided by Drupal. We want to add all headers which
      // are provided by Drupal to be added to the message. For each header we
      // first have to find out what type of header it is, and then add it to
      // the message as the particular header type.
      if (!empty($message['headers']) && is_array($message['headers'])) {
        foreach ($message['headers'] as $header_key => $header_value) {

          // Check wether the current header key is empty or represents
          // a header that should be suppressed. If yes, then skip header.
          if (empty($header_key) || in_array($header_key, $suppressable_headers)) {
            continue;
          }

          // Skip 'Content-Type' header if the message to be sent will be a
          // multipart message or the provided format is not to be respected.
          if ($header_key == 'Content-Type' && (!$respect_format || swiftmailer_is_multipart($message))) {
            continue;
          }

          // Get header type.
          $header_type = Conversion::swiftmailer_get_headertype($header_key, $header_value);

          // Add the current header to the e-mail message.
          switch ($header_type) {
            case SWIFTMAILER_HEADER_ID:
              Conversion::swiftmailer_add_id_header($m, $header_key, $header_value);
              break;

            case SWIFTMAILER_HEADER_PATH:
              Conversion::swiftmailer_add_path_header($m, $header_key, $header_value);
              break;

            case SWIFTMAILER_HEADER_MAILBOX:
              Conversion::swiftmailer_add_mailbox_header($m, $header_key, $header_value);
              break;

            case SWIFTMAILER_HEADER_DATE:
              Conversion::swiftmailer_add_date_header($m, $header_key, $header_value);
              break;

            case SWIFTMAILER_HEADER_PARAMETERIZED:
              Conversion::swiftmailer_add_parameterized_header($m, $header_key, $header_value);
              break;

            default:
              Conversion::swiftmailer_add_text_header($m, $header_key, $header_value);
              break;

          }
        }
      }

      // Set basic message details.
      Conversion::swiftmailer_remove_header($m, 'From');
      Conversion::swiftmailer_remove_header($m, 'To');
      Conversion::swiftmailer_remove_header($m, 'Subject');

      // Parse 'from' and 'to' mailboxes.
      $from = Conversion::swiftmailer_parse_mailboxes($message['from']);
      $to = Conversion::swiftmailer_parse_mailboxes($message['to']);

      // Set 'from', 'to' and 'subject' headers.
      $m->setFrom($from);
      $m->setTo($to);
      $m->setSubject($message['subject']);

      // Get applicable format.
      $applicable_format = $this->getApplicableFormat($message);

      // Get applicable character set.
      $applicable_charset = $this->getApplicableCharset($message);

      // Set body.
      $m->setBody($message['body'], $applicable_format, $applicable_charset);

      // Add alternative plain text version if format is HTML and plain text
      // version is available.
      if ($applicable_format == SWIFTMAILER_FORMAT_HTML && !empty($message['plain'])) {
        $m->addPart($message['plain'], SWIFTMAILER_FORMAT_PLAIN, $applicable_charset);
      }

      // Validate that $message['params']['files'] is an array.
      if (empty($message['params']['files']) || !is_array($message['params']['files'])) {
        $message['params']['files'] = array();
      }

      // Let other modules get the chance to add attachable files.
      $files = $this->moduleHandler->invokeAll('swiftmailer_attach', array('key' => $message['key'], 'message' => $message));
      if (!empty($files) && is_array($files)) {
        $message['params']['files'] = array_merge(array_values($message['params']['files']), array_values($files));
      }

      // Attach files.
      if (!empty($message['params']['files']) && is_array($message['params']['files'])) {
        $this->attach($m, $message['params']['files']);
      }

      // Attach files (provide compatibility with mimemail)
      if (!empty($message['params']['attachments']) && is_array($message['params']['attachments'])) {
        $this->attachAsMimeMail($m, $message['params']['attachments']);
      }

      // Embed images.
      if (!empty($message['params']['images']) && is_array($message['params']['images'])) {
        $this->embed($m, $message['params']['images']);
      }

      static $mailer;

      // If required, create a mailer which will be used to send the message.
      if (empty($mailer)) {

        // Get the configured transport type.
        $transport_type = $this->config['transport']['transport'];

        // Configure the mailer based on the configured transport type.
        switch ($transport_type) {
          case SWIFTMAILER_TRANSPORT_SMTP:
            // Get transport configuration.
            $host = $this->config['transport']['smtp_host'];
            $port = $this->config['transport']['smtp_port'];
            $encryption = $this->config['transport']['smtp_encryption'];
            $username = $this->config['transport']['smtp_username'];
            $password = $this->config['transport']['smtp_password'];

            // Instantiate transport.
            $transport = Swift_SmtpTransport::newInstance($host, $port);
            $transport->setLocalDomain('[127.0.0.1]');

            // Set encryption (if any).
            if (!empty($encryption)) {
              $transport->setEncryption($encryption);
            }

            // Set username (if any).
            if (!empty($username)) {
              $transport->setUsername($username);
            }

            // Set password (if any).
            if (!empty($password)) {
              $transport->setPassword($password);
            }

            $mailer = Swift_Mailer::newInstance($transport);
            break;

          case SWIFTMAILER_TRANSPORT_SENDMAIL:
            // Get transport configuration.
            $path = $this->config['transport']['sendmail_path'];
            $mode = $this->config['transport']['sendmail_mode'];

            // Instantiate transport.
            $transport = Swift_SendmailTransport::newInstance($path . ' -' . $mode);
            $mailer = Swift_Mailer::newInstance($transport);
            break;

          case SWIFTMAILER_TRANSPORT_NATIVE:
            // Instantiate transport.
            $transport = Swift_MailTransport::newInstance();
            $mailer = Swift_Mailer::newInstance($transport);
            break;

          case SWIFTMAILER_TRANSPORT_SPOOL:
            // Instantiate transport.
            $spooldir = $this->config['transport']['spool_directory'];
            $spool = new Swift_FileSpool($spooldir);
            $transport = Swift_SpoolTransport::newInstance($spool);
            $mailer = Swift_Mailer::newInstance($transport);
            break;
        }
      }

      // Send the message.
      Conversion::swiftmailer_filter_message($m);
      return $mailer->send($m);
    }
    catch (Exception $e) {

      $headers = !empty($m) ? $m->getHeaders() : '';
      $headers = !empty($headers) ? nl2br($headers->toString()) : 'No headers were found.';
      $this->logger->error(
        'An attempt to send an e-mail message failed, and the following error
        message was returned : @exception_message<br /><br />The e-mail carried
        the following headers:<br /><br />@headers',
        array('@exception_message' => $e->getMessage(), '@headers' => $headers));
      drupal_set_message(t('An attempt to send an e-mail message failed.'), 'error');
    }
  }

$host = smtp.hubapi.com

 

$port = 587

$encryption = SSL

$username: generated token username

$password = generated token password

0 Upvotes
IsaacTakushi
HubSpot Employee
HubSpot Employee

SMTP token in Swift mailer

SOLVE

Thank you for all this information, @PiotrM. I'll dig in further with my team!

Isaac Takushi

Associate Certification Manager
0 Upvotes
PiotrM
Member

SMTP token in Swift mailer

SOLVE

Hi Isaac,

Have you got a chance to look into this? I got stucked with it.

0 Upvotes
IsaacTakushi
HubSpot Employee
HubSpot Employee

SMTP token in Swift mailer

SOLVE

Hi, @PiotrM.

 

It's good to hear from you. Sincere apologies — I let this message fall through the cracks and have yet to investigate.

I'll make this a priority tomorrow and get back to you as soon as I have additional information.

Isaac Takushi

Associate Certification Manager
0 Upvotes
IsaacTakushi
Solution
HubSpot Employee
HubSpot Employee

SMTP token in Swift mailer

SOLVE

Hi, @PiotrM.

 

Thanks for your patience.

 

I see that you're setting $port = 587 and $encryption = SSL. Would you mind trying the following combinations and let me know if either work?

  1. $port = 587 and $encryption = TLS
  2. $port = 465 and $encryption = SSL

 

Isaac Takushi

Associate Certification Manager
0 Upvotes
PiotrM
Member

SMTP token in Swift mailer

SOLVE

Hi Isaac,

Than you for your help, switching port and enription type solved the problem.

IsaacTakushi
HubSpot Employee
HubSpot Employee

SMTP token in Swift mailer

SOLVE

Hey, @PiotrM.

 

I'm glad to hear that! Which combination did you use?

Isaac Takushi

Associate Certification Manager
0 Upvotes