1

can one help me with a code which can send mail with two attachments of either .dox .docx .pdf .jpeg only at once which are already on the server and the link paths were saved in database (MySql)?

For the sake of Mechanism

To:

From:

Subject:

Message:

Check Boxes Of attachments

Am Just a Beginner I only Know How to send Mail We out Attachments like Bellow

<?php 
$errors = '';
$myemail = 'admin@mydomain.net'
if(empty($_POST['name'])  || 
   empty($_POST['email']) || 
   empty($_POST['message']))
{
    $errors .= "\n Error: all fields are required";
}

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$message = $_POST['message']; 

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
    $to = $myemail; 
    $email_subject = "New Mail From: $name";
    $email_body = "$message"; 

    $headers = "From: $myemail\n"; 
    $headers .= "Reply-To: $email_address";

    mail($to,$email_subject,$email_body,$headers);
    //redirect to the 'thank you' page
    header('Location: thank-you.html');
} 

?>
BenMorel
  • 34,448
  • 50
  • 182
  • 322
madcoder
  • 35
  • 2
  • 7
  • Please consider using a premade library for mailing instead of using "mail()" directly. With Mailheader-Injection, your code is exploitable and can be abused for sending spam. – Honk der Hase Jun 13 '13 at 12:04

1 Answers1

0

You should check out this page: http://webcheatsheet.com/PHP/send_email_text_html_attachment.php

Or if your server supports PEAR, then you should use PEAR Mail with Mail_Mime, it's a much cleaner solution then the above.

<?php

include 'Mail.php';
include 'Mail/mime.php' ;

$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = '/home/richard/example.php';
$crlf = "\n";
$hdrs = array(
          'From'    => 'you@yourdomain.com',
          'Subject' => 'Test mime message'
          );

$mime = new Mail_mime(array('eol' => $crlf));

$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'text/plain');

$body = $mime->get();
$hdrs = $mime->headers($hdrs);

$mail =& Mail::factory('mail');
$mail->send('postmaster@localhost', $hdrs, $body);

?>
langm
  • 43
  • 2
  • whats inside mail.php and mime.php in your code – madcoder Jun 13 '13 at 12:11
  • Mail.php and Mail/mime.php is part of PEAR Mail and Mail_Mime packages. If your server supports PEAR, you don't have to copy these files to your script's folder, PHP will include them anyway. – langm Jun 14 '13 at 15:13