Avatar of APD Toronto
APD TorontoFlag for Canada

asked on 

PHP Mail Settings

Hi Experts,

Is it possible for me to set my Mail Settings at runtime (in my .php file)? Like SMTP Port, User and Password?

If I cannot set these at runtime, where do I set them?

TThank you.
PHP

Avatar of undefined
Last Comment
Dave Baldwin
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

In theory, you can set the SMTP port.  But the builtin PHP mail() function does not support  authentication.  You would have to use something like PHPMailer https://github.com/PHPMailer/PHPMailer or SwiftMailer https://swiftmailer.symfony.com/ .
Avatar of APD Toronto
APD Toronto
Flag of Canada image

ASKER

Yes, I remeber now, but I cannot make it work.

Actually, the following works.
<?php
/**
 * This example shows making an SMTP connection with authentication.
 */

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
//date_default_timezone_set('Etc/UTC');

require '../PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = "mail.apdcompany.ca";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 45;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "donotreply@apdcompany.ca";
//Password to use for SMTP authentication
$mail->Password = "xxx; 
//Set who the message is to be sent from
$mail->setFrom('donotreply@apdcompany.ca', 'Do Not Reply');
//Set an alternative reply-to address
$mail->addReplyTo('nataliia@aces-project.com', 'Mistress Nataliia');
//Set who the message is to be sent to
$mail->addAddress('aleks@aces-project.com', 'Aleks Poposki');
//Set the subject line
$mail->Subject = 'PHPMailer SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

Open in new window


But the follwing using the same setup give me Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
$env = array();
 //Email Settings
        $env['mail_debug'] = 2; //0 = off; 1 = client messages; 2 = client and server
        $env['mail_host'] = 'mail.apdcompany.ca';
        $env['mail_port'] = 45;
        $env['mail_auth'] = true;
        
        $env['mail_user'] = 'donotreply@apdcompany.ca';
        $env['mail_password'] = "xxx"
        
        $env['mail_send_from_address'] = $env['mail_user'];
        $env['mail_send_from_name'] = 'CRM System';
        
        $env['mail_reply_to_address'] = '';
        $env['mail_reply_to_name'] = '';
        
send_test();
function send_test(){ /*DELETE ME*/
        
    $email = array();
    
    $email['subject'] = 'S test';
    $email['body'] = 'One <b>bolded</b>body';
    $email['to_address'] = 'aleks@aces-project.com';
    $email['to_name'] = 'jessica Natalievna';
    
    send_email($email);
    
}

function send_email($email){
    
    //this function sends email with php mailer
    global $env;
    
    require "PHPMailer/PHPMailerAutoload.php";
    
    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->SMTPDebug = $env['mail_debug'];
    $mail->Debugoutput = 'html';
    $mail->Host = $env['mail_host'];
    $mail->Port = $env['mail_port'];
    $mail->SMTPAuth = $env['mail_auth'];
    
    $mail->Username = $env['mail_user'];
    $mail->Password = $env['mail_password'];
    
    $mail->setFrom($env['mail_send_from_address'], $env['mail_send_from_name']);
    
    if ($env['mail_reply_to_address'] != ''){
        $mail->addReplyTo($env['mail_reply_to_address'], $env['mail_reply_to_name']);
    }
    
    $mail->addAddress($email['to_address'], $email['to_name']);
    
    $mail->Subject = $email['subject'];
    $mail->msgHTML($email['body']);
    
    if (isset($email['attach'])){
        $mail->addAttachment($email['attach']);
    }
    
 
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    }    
    
}

?>

Open in new window

Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

I never heard of port 45 being used for email.  Maybe 25, 465 or 587?
Avatar of APD Toronto
APD Toronto
Flag of Canada image

ASKER

I know, but it is a custom server, and port 45 is also in the first (working) script.
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of APD Toronto
APD Toronto
Flag of Canada image

ASKER

send_test() is called from line 17

send_test() is declared on line 18

send_email() is called on line 27

send_email() is declared on line 31

Am I not seeing something?
Avatar of APD Toronto
APD Toronto
Flag of Canada image

ASKER

You made me think.

I am actually declaring my $env array in another file, which I was including under my mail function file.
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Glad you got it working.  In some languages, the function declaration must come before it is called.  Javascript is very picky about that.  I thought PHP was too.  I would have put them in the reverse order.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo