接收电子邮件地址和附件无法在phpmailer中正确

时间:2017-04-23 15:19:25

标签: php html phpmailer

我想在php中发送带附件的电子邮件。但接收电子邮件地址和附件无法在我的代码中正确使用。当我手动给电子邮件地址发送电子邮件成功虽然电子邮件发送成功附件没有正确接收端。

    <form action="sendemail.php" enctype="multipart/form-data" >
                            <h1 class="cta-title">Its a Call To Action</h1>
                            <div class="cta-desc">
                                <input type="text" value='<?= $row['catogary'];?>' readonly style="width: 75%"><br><br>
                                <input type="text" value='<?= $row['company_name'];?>' readonly style="width:    75%"><br><br>
                                <input type="text" value='<?= $row['location'];?>' readonly style="width: 75%"><br><br>
                                <input type="text" value='<?= $row['qulification'];?>' readonly style="width: 75%"><br><br>
                                <input type="text" value='<?= $row['catogary'];?>' readonly style="width: 75%"><br><br>
                                <input type="text" value='<?= $row['indate'];?>' readonly style="width: 37.5%">&nbsp;
                                <input type="text" value='<?= $row['expdate'];?>' readonly style="width: 37.5%">
                                <input type="text" value='<?= $row['email'];?>' ><br>
                                <input type="file" name="uploaded_file" id="uploaded_file" class="text-center center-block well well-sm">
                                <input type="submit" id="btn" name="btn" class="btn btn-primary" value="Apply">
                            </div>
                            </form>

sendemail.php

    <?php
    $email2=$_POST['email'];
    require_once('PHPMailer/PHPMailerAutoload.php');
    //PHPMailer Object
    $mail = new PHPMailer;
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "getinternshipuwu@gmail.com";
$mail->Password = "uwucst14xxxx";
$mail->SetFrom("getinternshipuwu@gmail.com");
$mail->FromName = "Internship Management";

//To address and name
$mail->addAddress($email2);
//$mail->addAddress("recepient1@example.com"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("getinternshipuwu@gmail.com", "Reply");

//CC and BCC
//$mail->addCC("CC");
//$mail->addCC("CC");
//$mail->addBCC("bcc@example.com");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = 'CV for internship Vacancy';
$mail->Body =  "Attached";
//$mail->AltBody = "This is the plain text version of the email content";
//$mail->AddAttachment($target_path,$CV);
if (isset($_FILES['uploaded_file']) && $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK) {

    $mail->AddAttachment($_FILES['uploaded_file']['tmp_name'],$_FILES['uploaded_file']['name']);
}

if(!$mail->send())
{
    echo "Mailer Error: " . $mail->ErrorInfo;
    //echo 'Not sent: <pre>'.print_r(error_get_last(), true).'</pre>';

}
else
{
    //echo "Message has been sent successfully";
    echo 'Successfully Applied for vacancy';
}

//**end of send e-mail**
?>

错误是:

    Undefined index: email in /storage/h7/602/1448602/public_html/sendemail.php on line 2

1 个答案:

答案 0 :(得分:0)

在文件sendemail.php中,您正在使用

   <?php
    $email2=$_POST['email'];

这是你的表格标签。

<form action="sendemail.php" enctype="multipart/form-data" >

这是使用HTTP方法GET发送表单,但它需要是POST。 所以你需要在表单标签中添加属性method =“post”,如此

<form action="sendemail.php" enctype="multipart/form-data" method="post">

您修复错误

Undefined index: email in /storage/h7/602/1448602/public_html/sendemail.php on line 2

通过更改此行

<input type="text" value='<?= $row['email'];?>' ><br>

<input type="text" name="email" value='<?= $row['email'];?>' ><br>