如何在函数调用中传递变量

时间:2017-07-15 07:31:28

标签: php function email

我需要帮助我的代码,我试图发送一个具有此功能的邮件,但它没有发送邮件。如果有人帮助我查看整个代码库并更正错误,请相信我。感谢。

emailClass.php

<?php

class email
{
function emailWithAttach($fromaddress, $toAddress, $mailSubject, $mailMessageHead, $mailMessageMain, $mailMessageEnd, $fileName)
{
    $fileatt_name = "Invoice";
    $fileatt = $fileName;
    $fileatt_type = "application/octet-stream";
    $email_from = $fromaddress;
    $email_subject = $mailSubject;

    $email_message = $mailMessageHead."<br>";
    $email_message .= $mailMessageMain."<br>";
    $email_message .= $mailMessageEnd;

    $email_to = $toAddress;
    $headers = "From: ".$email_from;

    $file = fopen($fileatt, 'rb');
    $data = fread($file, filesize($fileatt));
    fclose($file);

    $semi_rand=md5(time());
    $mime_boundary .= "==Multipart_Boundary_x{$semi_rand}x";
    $headers .= "MIME-Version:1.0\n".
    "Content-Type: Multipart/mixed;\n".
    "boundary=\"{$mime_boundary}\"";

    $email_message .= "This is a multi-part message in MIME format.\n\n".
    "--{$mime_boundary}\n".
    "Content-Type: text/html; charset=\"iso-8859-1\"\n".
    "Content-Transfer-Encoding: 7bit\n\n".
    $email_message .= "\n\n";

    $data = chunk_split(base64_encode($data));

    $email_message .= "--{$mime_boundary}\n".
    "Content-Type: {$fileatt_type};\n".
    "name=\"{$fileatt_name}\"\n".
    "Content-Transfer-Encoding: base64\n\n".
    $data .="\n\n".
    "--{$mime_boundary}--\n";

    if(mail($email_to, $email_subject, $email_message, $headers))
    {
        return true;
    }

}
}


?>

这是我的模板 index.php

<?php
include "emailClass.php";

$testEmail = new email();
$from = 'chidiebere.ezeka@yahoo.com';
$sendTo = 'chidiebere.ezeka@gmail.com';
$subject ='ABS Travels';
$bodyHead = 'Welcome to attachment of Email';
$bodyMain = 'I love the way php handles email attachment, it is amazing to me';
$bodyEnd = 'Thank you';
//$filePath = '';
$fileName = 'reports.pdf';

if($testEmail->emailWithAttach($from, $sendTo, $subject, $bodyHead, $bodyMain, $bodyEnd, $fileName))
{
    echo 'Email send successful';
}
else
{
    echo 'Email Failed';
}
?>

我已更新我的代码并进行了更正,但邮件没有发送。请任何建议。除了“电子邮件失败”消息之外,它不会显示任何错误。

1 个答案:

答案 0 :(得分:0)

修正拼写错误,它应该有效!

<?php
[...]

$testEmail = new email(); # When you call a new class you need the parenthese 
[...]
?>