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 TorontoAsked:
Who is Participating?
 
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
 
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 TorontoAuthor 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Dave BaldwinFixer of ProblemsCommented:
I never heard of port 45 being used for email.  Maybe 25, 465 or 587?
0
 
APD TorontoAuthor Commented:
I know, but it is a custom server, and port 45 is also in the first (working) script.
0
 
APD TorontoAuthor 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 TorontoAuthor 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.