Search code examples
phpemailsendmail

How to set up the sendmail configration from script?


Is there a way in which we can just configure the sendmail settings dynamically from PHP script, instead of going manually to php.ini and sendmail.ini files to make changes?

Can we do this by using an HTML form allowing a user to change the values?


Solution

  • You could use something such as PHPMailer instead: https://github.com/PHPMailer/PHPMailer

    Example usage:

    require('./PHPMailer/class.phpmailer.php');
    $mail=new PHPMailer();
    $mail->CharSet = 'UTF-8';
    
    $body = 'This is the message content';
    
    $mail->IsSMTP();
    $mail->Host       = 'smtp.gmail.com';
    
    $mail->SMTPSecure = 'tls';
    $mail->Port       = 587;
    $mail->SMTPDebug  = 1;
    $mail->SMTPAuth   = true;
    
    $mail->Username   = '[email protected]';
    $mail->Password   = '12345';
    
    $mail->SetFrom('[email protected]', $name);
    $mail->AddReplyTo('[email protected]','no-reply');
    $mail->Subject    = 'subject';
    $mail->MsgHTML($body);
    
    $mail->AddAddress('[email protected]', 'title1');
    $mail->AddAddress('[email protected]', 'title2'); /* ... */
    
    $mail->AddAttachment($fileName);
    $mail->send();