Search code examples
phpswiftmailer

How to feed a backslash to Swift-mailer setPassword


I have a problem trying to insert a password containing a backslash in Swift-mailer setPassword, like so:

$transport = Swift_SmtpTransport::newInstance('smtp.myserver.com', portnumber)
  ->setUsername('[email protected]')
  ->setPassword('Somepassword\');

The problem is that PHP will interpret the backslash as an escape and will throw an error, but if I escape the backslash adding another one the password will not be correct.

I also tried using the ASCII code for a backslash and putting everything into a variable, giving setPassword something like this:

$password = 'Somepassword'.chr(92);
$transport = Swift_SmtpTransport::newInstance('smtp.myserver.com', portnumber)
  ->setUsername('[email protected]')
  ->setPassword($password);

Is there a way to make a password containing a backslash work? I can't change the password.

Thank you!


Solution

  • You simply have to escape it with another backslash

    ->setPassword('Somepassword\\');
    

    This will be interpreted as:

    Somepassword\