Sending html mail with function

好久不见. 提交于 2019-12-11 14:29:14

问题


I've been working on this for ages and just can't find the issue. Hope somebody is so kind enough to help me out.

I've made a function for my mailing script. The function is inside useful_functions.inc.php

<?php
include ('mailer/class.phpmailer.php');

function sendmail($mail)
{
  $mail = new PHPMailer(true);  

  if (($_SERVER["REQUEST_METHOD"] == "POST") && $geslachtErr== "" && $voornaamErr== "" && $familienaamErr== "" && $emailErr== "" && $telErr== "" && $afileErr== "")
  {
    $full_name  = "Sofie Doe";
    $email      = "sofie@gmail.com";
    $subject    = "HTML test mail.";
    $text_message    = "Hello ".$full_name.",<br /><br /> This is a test email in html.";         

    $message  = "<html><body>";

    $message .= "<table width='100%' bgcolor='#e0e0e0' cellpadding='0' cellspacing='0' border='0'>";

    $message .= "<tr><td>";

    $message .= "<table align='center' width='100%' border='0' cellpadding='0' cellspacing='0' style='max-width:650px; background-color:#fff; font-family:Verdana, Geneva, sans-serif;'>";

    $message .= "<thead>
          <tr height='80'>
            <th colspan='4' style='background-color:#f5f5f5; border-bottom:solid 1px #bdbdbd; font-family:Verdana, Geneva, sans-serif; color:#333; font-size:34px;' >TEST</th>
          </tr>
          </thead>";


    $message .= "</table>";

    $message .= "</td></tr>";
    $message .= "</table>";

    $message .= "</body></html>";

    try
    {
      $mail->IsSMTP(); 
      $mail->isHTML(true);
      $mail->SMTPDebug  = 0;                     
      $mail->SMTPAuth   = true;                  
      $mail->SMTPSecure = "STARTTLS";                 
      $mail->Host       = "mailout.one.com";      
      $mail->Port       = 587;             
      $mail->AddAddress($email);
      $mail->Username   ="joe@gmail.com";  
      $mail->Password   ="Password";            
      $mail->SetFrom("joe@gmail.com");
      $mail->AddReplyTo("joe@gmail.com");
      $mail->Subject    = $subject;
      $mail->Body     = $message;
      $mail->AltBody    = $message;
    }
  }
}

?>

I have a form, test.php. I start them test.php with

<?php
include ('useful_functions.inc.php')
sendmail($mail);
?>

When I run test.php I keep getting HTTP ERROR 500

I've tested the exact mailer with a simple html form and calling it the same way and it works. My test.php works when I remove sendmail($mail) and remove the function in my useful_function.inc.php

Why is this not working?


回答1:


Why don't you have the email "send itself" by modifying try{} to include $mail->Send();?

Your "try" will become:

try
{
  $mail->IsSMTP(); 
  $mail->isHTML(true);
  $mail->SMTPDebug  = 0;                     
  $mail->SMTPAuth   = true;                  
  $mail->SMTPSecure = "STARTTLS";                 
  $mail->Host       = "mailout.one.com";      
  $mail->Port       = 587;             
  $mail->AddAddress($email);
  $mail->Username   ="joe@gmail.com";  
  $mail->Password   ="Password";            
  $mail->SetFrom("joe@gmail.com");
  $mail->AddReplyTo("joe@gmail.com");
  $mail->Subject    = $subject;
  $mail->Body     = $message;
  $mail->AltBody    = $message;
  $mail->Send();
  echo "Message Sent OK\n";
  } catch (phpmailerException $e) {
    echo $e->errorMessage(); //Pretty error messages from PHPMailer
  } catch (Exception $e) {
    echo $e->getMessage(); //Boring error messages from anything else!
  }


来源:https://stackoverflow.com/questions/44593868/sending-html-mail-with-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!