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?
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 or SwiftMailer .
APD TorontoSoftware DeveloperAuthor Commented:
Yes, I remeber now, but I cannot make it work.

Actually, the following works.
 * 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

require '../PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
//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 = "";
//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 = "";
//Password to use for SMTP authentication
$mail->Password = "xxx; 
//Set who the message is to be sent from
$mail->setFrom('', 'Do Not Reply');
//Set an alternative reply-to address
$mail->addReplyTo('', 'Mistress Nataliia');
//Set who the message is to be sent to
$mail->addAddress('', '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

//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.
$env = array();
 //Email Settings
        $env['mail_debug'] = 2; //0 = off; 1 = client messages; 2 = client and server
        $env['mail_host'] = '';
        $env['mail_port'] = 45;
        $env['mail_auth'] = true;
        $env['mail_user'] = '';
        $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'] = '';
function send_test(){ /*DELETE ME*/
    $email = array();
    $email['subject'] = 'S test';
    $email['body'] = 'One <b>bolded</b>body';
    $email['to_address'] = '';
    $email['to_name'] = 'jessica Natalievna';

function send_email($email){
    //this function sends email with php mailer
    global $env;
    require "PHPMailer/PHPMailerAutoload.php";
    $mail = new PHPMailer;
    $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'];
    if (isset($email['attach'])){
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;


Open in new window

Dave BaldwinFixer of ProblemsCommented:
I never heard of port 45 being used for email.  Maybe 25, 465 or 587?
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

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

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?
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.
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.
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

From novice to tech pro — start learning today.