At the moment we have this code to send emails with phpmailer via smtp. How can I make it work without showing username, password, host and port? I do not want to show my credentials in the file :

require_once "Mail.php";
$from = "myemail@email.com";
$to = "test@email.com";

$subject = "Test";
$body = "Hello";
$host = "mail.mysite.com";
$port = "myport";
$username = "myemail@email.com";
$password = "my_password";
$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'port' => $port,
    'auth' => true,
    'username' => $username,
    'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
 } else {

Thanks !


If you are creating a Magento module you can create configuration variables which are set on the Configuration page and can be referenced in your code using syntax like:


Here's a tutorial on how to create configuration values https://inchoo.net/magento/create-configuration-for-your-magento-extension/

If you aren't creating your own extension there are existing SMTP modules for Magento. A popular free one is aschroder's SMTP Pro extension. https://github.com/aschroder/Magento-SMTP-Pro-Email-Extension

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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