Skip to content

Instantly share code, notes, and snippets.

@dasbairagya
Last active July 31, 2018 09:51
Show Gist options
  • Select an option

  • Save dasbairagya/cd7ea14aa6b8a9b52cfcae9cba45d5b8 to your computer and use it in GitHub Desktop.

Select an option

Save dasbairagya/cd7ea14aa6b8a9b52cfcae9cba45d5b8 to your computer and use it in GitHub Desktop.
Multiple attachement sending using wp mail...
<?php
if(isset($_POST['submit'])){
$fname = $_POST['fname'];
$lname= $_POST['lname'];
$phonenumber= $_POST['phonenumber'];
$email= $_POST['email'];
$enquiry=$_POST['enquiry'];
$fullname= $fname. ' '.$lname;
function my_custom_email_content_type( $content_type ) {
return 'text/html';
}
if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
$files = $_FILES[ 'my_file' ];
$upload_overrides = array( 'test_form' => false );
$attachments = array();
$galleryImages = array();
foreach ( $files['name'] as $key => $value ) {
if ( $files[ 'name' ][ $key ] ) {
$file = array(
'name' => $files[ 'name' ][ $key ],
'type' => $files[ 'type' ][ $key ],
'tmp_name' => $files[ 'tmp_name' ][ $key ],
'error' => $files[ 'error' ][ $key ],
'size' => $files[ 'size' ][ $key ]
);
$movefile = wp_handle_upload(
$file,
$upload_overrides
);
$attachments[] = $movefile[ 'file' ];//send to mail
$filename = $movefile['file'];
// -----------------upload to media-----------
$filetype = wp_check_filetype( basename( $filename ), null );
// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $filename);
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
array_push($galleryImages, $attach_id);
// --------------------upload to media end--------------------
}
}
$admin_email = get_bloginfo('admin_email');
$form_email = $email;
$to = $admin_email;
$subject = 'Contact Us';
$message = '<html>
<head></head>
<body>
<h1>New Enquery By: <em> '.$fullname.'</em></h1>
<h4>Name</h4>:'.$fullname.'</br><h4>Email</h4>:'.$form_email.'</br><h4>Phone</h4>:'.$phonenumber.'</br>
<h3><u>Enquery Details</u>:</h3>';
$message .='<p>'.$enquiry.'</p>';
$message .='<br /> <center style="color:blue;">© 2018 guidanceway. All rights reserved.</center>
</body></html>';
$headers[] = 'From: ' . get_option( 'blogname' ) . ' <'.$form_email.'>';
add_filter(
'wp_mail_content_type',
'my_custom_email_content_type'
);
$wp_mail_return = wp_mail(
$to,
$subject,
$message,
$headers,
$attachments
);
if( $wp_mail_return ) {
echo '<div class="alert alert-success" role="alert">
<button style="width: 50px" type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<p>Thank you for connecting with us! your message has been sent.</p></div>';
} else {
echo '<div class="alert alert-danger" role="alert">
<button style="width: 50px" type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Oh snap!</strong> Something went wrong please try again..
</div>';
}
remove_filter(
'wp_mail_content_type',
'my_custom_email_content_type'
);
}
?>
<div class="query-form">
<form method="POST" action="" enctype="multipart/form-data">
<div class="form-group">
<label>First Name
</label>
<input type="text" class="form-control" name="fname" id="name" required>
</div>
<div class="form-group">
<label>Last Name
</label>
<input type="text" class="form-control" name="lname" required >
</div>
<div class="form-group">
<label>Mobile Number
</label>
<input type="tel" class="form-control" name="phonenumber" required>
</div>
<div class="form-group">
<label>Email
</label>
<input type="email" class="form-control" name="email" required>
</div>
<div class="form-group">
<label>Enquiry
</label>
<textarea class="form-control" name="enquiry">
</textarea>
</div>
<div class="form-group">
<input type="file" name="my_file[]" multiple>
</div>
<div class="form-group">
<input type="submit" class="btn button3" name='submit' value="Send">
</div>
</form>
</div>
<!-- Sample Source Code -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>ctrlq
</title>
</head>
<body>
<h1>Hello World
</h1>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment