PHP邮件报告不起作用

时间:2017-11-14 21:35:00

标签: php mailer

我遇到了问题,因为我通过php进行文件记录并不起作用。我尝试了不同的教程。我的最后一次尝试是这里的代码:

if($isValid === true) {

    // Submit Mail
    $mail = new SimpleMail();
    $mail->setTo(YOUR_EMAIL_ADDRESS, YOUR_COMPANY_NAME)
    ->setSubject('Neue Mietanfrage')
    ->setFrom(htmlspecialchars($_POST['email-address']), htmlspecialchars($_POST['first-name'].' '.$_POST['last-name']))
    ->addGenericHeader('X-Mailer', 'PHP/' . phpversion())
    ->addGenericHeader('Content-Type', 'text/html; charset="utf-8"')
    ->setMessage(createMessage($_POST))
    ->setWrap(100);

    $mail->send();


    // Submit Client Mail
    $mailClient = new SimpleMail();
    $mailClient->setTo(htmlspecialchars($_POST['email-address']), htmlspecialchars($_POST['first-name'].' '.$_POST['last-name']))
    ->setSubject('Ihre Mietanfrage bei '.YOUR_COMPANY_NAME)
    ->setFrom(YOUR_EMAIL_ADDRESS, YOUR_COMPANY_NAME)
    ->addGenericHeader('X-Mailer', 'PHP/' . phpversion())
    ->addGenericHeader('Content-Type', 'text/html; charset="utf-8"')
    ->setMessage(createClientMessage($_POST))
    ->setWrap(100);

        $mailClient->send();

    $result = array(
        'result' => 'success', 
        'msg' => array('Ihre Reservierung wurde erfolgreich übermittelt.')
        );

    echo json_encode($result);

} else {
    $result = array(
        'result' => 'error', 
        'msg' => $isValid
        );

    echo json_encode($result);
}

//*** Attachment ***//  
if($_FILES["fileAttach"]["name"] != "")  
{  
$strFilesName = $_FILES["fileAttach"]["name"];  
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));  
$strHeader .= "--".$strSid."\n";  
$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";  
$strHeader .= "Content-Transfer-Encoding: base64\n";  
$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";  
$strHeader .= $strContent."\n\n";  
}




<input type="file" name="fileAttach" id="fileAttach" hidden>

为什么这对我不起作用?我只是按照教程,但没有任何帮助。你知道我要做什么吗?其他信息,如文本字段发送没有问题。

我使用javascript从该表单中获取信息。该脚本是否支付电子邮件不与附件一起发送的任何角色?

&#13;
&#13;
$( "#umzug-form" ).submit(function() {

  $('#umzug-form-msg').addClass('hidden');
  $('#umzug-form-msg').removeClass('alert-success');
  $('#umzug-form-msg').removeClass('alert-danger');

  $('#umzug-form input[type=submit]').attr('disabled', 'disabled');

  $.ajax({
    type: "POST",
    url: "php/umzug.php",
    data: $("#umzug-form").serialize(),
    dataType: "json",
    success: function(data)  {

      if('success' == data.result)
      {
        $('#umzug-form-msg').css('visibility','visible').hide().fadeIn().removeClass('hidden').addClass('alert-success');
        $('#umzug-form-msg').html(data.msg[0]);
        $('#umzug-form input[type=submit]').removeAttr('disabled');
        $('#umzug-form')[0].reset();
      }

      if('error' == data.result)
      {
        $('#umzug-form-msg').css('visibility','visible').hide().fadeIn().removeClass('hidden').addClass('alert-danger');
        $('#umzug-form-msg').html(data.msg[0]);
        $('#umzug-form input[type=submit]').removeAttr('disabled');
      }

    }
  });

  return false;
});
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

您不是在任何地方将文件添加到邮件对象。

SimpleMail提供方法addAttachment,以便为您的电子邮件添加附件。

我正在使用所需的行扩展您的代码:

// Submit Mail
$mail = new SimpleMail();
$mail->setTo(YOUR_EMAIL_ADDRESS, YOUR_COMPANY_NAME)
    ->setSubject('Neue Mietanfrage')
    ->setFrom(htmlspecialchars($_POST['email-address']), htmlspecialchars($_POST['first-name'].' '.$_POST['last-name']))
    ->addGenericHeader('X-Mailer', 'PHP/' . phpversion())
    ->addGenericHeader('Content-Type', 'text/html; charset="utf-8"')
    ->setMessage(createMessage($_POST))
    ->setWrap(100);

// add the following lines to your code
if (isset($_FILES['fileAttach']['tmp_name'])) {
    $mail->addAttachment(
        $_FILES['fileAttach']['tmp_name'],
        $_FILES['fileAttach']['name']
    );
}

$mail->send();

SimpleMail将自行加载文件的内容。