Wordpress - 发送带附件的电子邮件

时间:2016-12-12 06:42:27

标签: php wordpress email email-attachments

这是我的html部分,请告诉我在表单操作中传递的内容:

<form action="" method="post" enctype="multipart/form-data">
<div class="inputtext"><input type="text" placeholder="Fast Name*" name="fname"></div>
<div class="inputtext"><input type="text" placeholder="Email ID*" name="email"></div>
<div class="inputtext">
  <select name="bcontact">
  <option value="">-- Best Way to Contact --</option>
  <option value="Phone">Phone</option>
  <option value="Email">Email</option>
  </select></div>
<div class="inputtext"><input type="text" placeholder="Years Of Experience" name="experience"></div>
<div class="inputtext"><input type="file" name="uploaded_file"></div>
<div class="input100per"><textarea placeholder="How can we help you?" rows="5" name="message"></textarea></div>
<div class="input100per"><input type="submit" value="Submit" name="submit">
<div>
</form>

代码: 我需要发送一封附有pdf / word文档附件的电子邮件。

 if ( isset( $_POST['submit'] ) ) {
 global $wpdb;
 $to ='test@gmail.com';
 $from = 'test@gmail.com'; 
 $name = get_bloginfo('name');
 $attachment = $_POST['file'];
 $headers  = 'MIME-Version: 1.0' . "\r\n";
 $headers .= 'Content-type: multipart/mixed; charset=iso-8859-1' . "\r\n";
 $headers .= 'From: ' . $name . ' <' . $from . '>' . "\r\n";   
 $subject = 'Testing';
 $msg = 'Yay!';
 $mail_attachment = array( $attachment );
 wp_mail($to, $subject, $msg, $headers, $mail_attachment);
 echo 'Email sent';
 } else {
 echo 'Email not sent';
 }

请找出解决方案。谢谢!

3 个答案:

答案 0 :(得分:1)

将您的PHP代码更改为:

if ( isset( $_POST['submit'] ) ) {
    global $wpdb;
    $to ='test@gmail.com';
    $from = 'test@gmail.com'; 
    $name = get_bloginfo('name');
    $attachment = WP_CONTENT_DIR ."/Path-of-your-file/".$_POST['file'];
    $headers  = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type: text/html;charset=utf-8"."\r\n";
    $headers .= "From: " . $name . " <" . $from . ">" . "\r\n";   
    $subject = 'Testing';
    $msg = 'Yay!';
    wp_mail($to, $subject, $msg, $headers, $attachment);
    echo 'Email sent';
} else {
    echo 'Email not sent';
}

答案 1 :(得分:1)

现在正在工作

if ( isset( $_POST['submit'] ) ) {

    $fname   = sanitize_text_field( $_POST["fname"] );
    $lname   = sanitize_text_field( $_POST["lname"] );
    $email   = sanitize_email( $_POST["email"] );
    $number  = $_POST["number"];
    $contact = $_POST["bcontact"];
    $position= $_POST["position"];

    $sName   = $fname." ".$lname;
    $subject  = 'Career Form';
    //$to = get_option( 'admin_email' );
    $attachments = array(WP_CONTENT_DIR . '/uploads/file_to_attach.zip');
    $to       = 'test@gmail.com';
    //$headers .= "From: $email" . "\r\n";
        $headers .= "Reply-To: ".$email. "\r\n";
        //$headers .= "CC: test@gmail.com\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

    $message = '<html><body>';
    $message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
    $message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>". $sName. "</td></tr>";
    $message .= "<tr><td><strong>Email:</strong> </td><td>" .$email. "</td></tr>";
    $message .= "<tr><td><strong>Phone Number:</strong> </td><td>".$number."</td></tr>";
    $message .= "<tr><td><strong>Best Way to Contact:</strong> </td><td>" .$contact. "</td></tr>";
    $message .= "<tr><td><strong>Desired Position:</strong> </td><td>".$position."</td></tr>";

    $message .= "</table>";
    $message .= "</body></html>";
    // If email has been process for sending, display a success message
    if ( wp_mail( $to,$subject,$message, $headers,$attachments) ) {
        echo '<div>';
        echo '<p>Thanks for contacting me, expect a response soon.</p>';
        echo '</div>';
    } else {
        echo 'Field markered with * required. ';
    }
}

答案 2 :(得分:0)

检查一下。请在电子邮件中使用“myname@yourdomain.com”而不是gmail.com

<?php
if ( isset( $_POST['submit'] ) ) {
    global $wpdb;
    $to ='test@gmail.com';
    $name = get_bloginfo('name');
    $headers = 'From: My Name <myname@mydomain.com>' . "\r\n";
    $subject = 'Testing';
    $msg = 'Yay!';
    $mail_attachment = array(WP_CONTENT_DIR . '/uploads/2012/03/image.png');   
    wp_mail($to, $subject, $msg, $headers, $mail_attachment);
    echo 'Email sent';
} else {
    echo 'Email not sent';
}
?>