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.
APD TorontoSoftware DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
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/ .
1
APD TorontoSoftware DeveloperAuthor Commented:
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

0
Dave BaldwinFixer of ProblemsCommented:
I never heard of port 45 being used for email.  Maybe 25, 465 or 587?
0
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

APD TorontoSoftware DeveloperAuthor Commented:
I know, but it is a custom server, and port 45 is also in the first (working) script.
0
Dave BaldwinFixer of ProblemsCommented:
Try putting the 'sendtest' and 'send_email' functions before they are called.  To me they are in the wrong order.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
APD TorontoSoftware DeveloperAuthor Commented:
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?
0
APD TorontoSoftware DeveloperAuthor Commented:
You made me think.

I am actually declaring my $env array in another file, which I was including under my mail function file.
0
Dave BaldwinFixer of ProblemsCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.