sending email from php file using SMTP

Discussion in 'Site Programming, Development and Design' started by Wenhu Tong, May 10, 2018.

  1. Hi, the following is the code I used to send email from my php file using SMPT with winhost. After running this php file, there is no error message but I did not get email in the destination mail box. I downloaded phpmailer and uploaded to the server.

    Can someone please check what is the problem. Many thanks.

    I hid up my password in the following code.

    <html>
    <head><title>Test email via php</title></head>
    <body>
    <?php
    require_once "phpmailer/phpmailerautoload.php";
    $from = "Sender <[email protected]>";
    $to = "Recipient <[email protected]>";
    $subject = "This is a test email sent via php";
    $body = "This is a test email";
    $host = "mail.canadasmallbusiness.org";
    $username = "postmaster";
    $password = "********";
    $headers = array ('From' => $from,
    'To' => $to,
    'Subject' => $subject);
    $smtp = Mail::factory('smtp',
    array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));
    $mail = $smtp->send($to, $headers, $body);
    ?>
    </body>
    </html>
     
  2. ComputerMan

    ComputerMan Winhost Staff

    The username when authenticating against the SMTP service needs to be the full email address.
     
  3. still does not work.

    <html>
    <head><title>Test email via php</title></head>
    <body>
    <?php
    require_once "phpmailer/phpmailerautoload.php";
    $from = "Sender <[email protected]>";
    $to = "Recipient <[email protected]>";
    $subject = "This is a test email sent via php";
    $body = "This is a test email";
    $host = "mail.canadasmallbusiness.org";
    $username = "[email protected]";
    $password = "*******";
    $headers = array ('From' => $from,
    'To' => $to,
    'Subject' => $subject);
    $smtp = Mail::factory('smtp',
    array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));
    $mail = $smtp->send($to, $headers, $body);
    ?>
    </body>
    </html>
     
  4. Elshadriel

    Elshadriel Winhost Staff

    I just tried your code and checked the mail server. The message was sent and delivered.
     
    ComputerMan likes this.
  5. ComputerMan

    ComputerMan Winhost Staff

    I helped you via chat and I used my own test code:

    <html>
    <head><title>Test email via php</title></head>
    <body>
    <?php
    require_once "Mail.php";
    $from = "Sender <[email protected]>";
    $to = "Recipient <[email protected]>";
    $subject = "This is a test email sent via php";
    $body = "This is a test email";
    $host = "mail.HostingAccountDomain.com";
    $username = "[email protected]";
    $password = "email_password";
    $headers = array ('From' => $from,
    'To' => $to,
    'Subject' => $subject);
    $smtp = Mail::factory('smtp',
    array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));
    $mail = $smtp->send($to, $headers, $body);
    ?>
    </body>
    </html>

    Our test code works above.
     
  6. Yes。 Thanks everybody. It works now :)
     
    ComputerMan likes this.

Share This Page