Pages

Tuesday, March 6, 2012

Wordpress emailing with attachment

Some of the code lines are not visible, just copy the code block and paste into a PHP editor - entire code will be available.

 
<?php 
    require_once ABSPATH . WPINC . '/class-phpmailer.php';
    $mail_to_send = new PHPMailer(); 
    $mail_to_send->FromName = $_REQUEST['name'];
    $mail_to_send->From     = $_REQUEST['email'];
    $mail_to_send->Subject  = 'This is Subject';
    $mail_to_send->Body     = 'This is Body'
    $mail_to_send->AddReplyTo($_REQUEST['email']);
    $mail_to_send->AddAddress( 'to@todomain.???' ); //destination e-mail
    // e.g. $mail_to_send->AddAddress('abedin.fakhrul@gmail.com');
 
    // Attachment
    if ( !$_FILES['attachment']['error'] == 4 ) { //something was send        
        $extTest=true;
        $extTest = preg_match('/\.(doc|docx|pdf)$/', $_FILES['attachment']['name']);
        //var_dump($_FILES['attachment']['error']);
        if ( $_FILES['attachment']['error'] == 0 && is_uploaded_file($_FILES['attachment']['tmp_name']) && $extTest ) { 
            $mail_to_send->AddAttachment($_FILES['attachment']['tmp_name'], $_FILES['attachment']['name']);        
            if ( !$mail_to_send->Send() ) wp_die('Error: e-mail can not be sent.');
        }
    } else $mail_to_send->Send();
?>

No comments :

Post a Comment