1

This is php code and its working, all the form content successfully send to mail id, but i want to attach PDF file also to email using php this function. But how i can, i don't know.

I would like to add a specified PDF file as a file attachment to the email. How would I do that?

<?php $name = $_REQUEST["name"]; $phone = $_REQUEST["phone"]; $email = $_REQUEST["email"]; $address = $_REQUEST["address"]; $message = $_REQUEST["message"]; date_default_timezone_set("Asia/Calcutta"); $todayis = date("y-m-d H:i:s", time()); $subject = "PVC Order From Website "; $to = "[email protected]"; $msg .= "<table width='600' border='0' align='center' cellpadding='0' ellspacing='0' style='font-family:Arial, Helvetica, sans-serif; font-size:13pt; border:1px solid #0f3f6a;'>"; $msg .= "<tr>"; $msg .= "<td width='16' height='25' bgcolor='#f9cf7b'>&nbsp;</td>"; $msg .= "<td width='96' bgcolor='#f9cf7b'>Name</td>"; $msg .= "<td width='10' height='25' bgcolor='#f9cf7b'><strong>:</strong></td>"; $msg .= "<td height='25' bgcolor='#f9cf7b'>$name</td>"; $msg .= "</tr>"; $msg .= "<tr>"; $msg .= "<td height='25' bgcolor='#f9cf7b'>&nbsp;</td>"; $msg .= "<td height='25' bgcolor='#f9cf7b'>Mobile </td>"; $msg .= "<td height='25' bgcolor='#f9cf7b'><strong>:</strong></td>"; $msg .= "<td height='25' bgcolor='#f9cf7b'>$phone</td>"; $msg .= "</tr>"; $msg .= "<tr>"; $msg .= "<tr>"; $msg .= "<td height='25' bgcolor='#f9cf7b'>&nbsp;</td>"; $msg .= "<td height='25' bgcolor='#f9cf7b'>Email Id </td>"; $msg .= "<td height='25' bgcolor='#f9cf7b'><strong>:</strong></td>"; $msg .= "<td height='25' bgcolor='#f9cf7b'>$email</td>"; $msg .= "</tr>"; $msg .= "<tr>"; $msg .= "<td height='25'>&nbsp;</td>"; $msg .= "<td height='25'>Address</td>"; $msg .= "<td height='25'><strong>:</strong></td>"; $msg .= "<td height='25'>$address</td>"; $msg .= "</tr>"; $msg .= "<tr>"; $msg .= "<td height='25'>&nbsp;</td>"; $msg .= "<td height='25'>Discription</td>"; $msg .= "<td height='25'><strong>:</strong></td>"; $msg .= "<td height='25'>$message</td>"; $msg .= "</tr>"; $msg .= "</table>"; $headers = "From: $c_name < $c_email >."; $headers = "Bcc: admin < $Bcc >."; $headers .= "\r\nContent-Type: text/html; charset=ISO-8859-1\r\n"; mail($to, $subject, $msg, $headers); header("Location:thankyou.html"); 
1
  • 1
    Do yourself a favour and use an existing library like PHPMailer. Will take all the pain out of doing this. Commented Jul 6, 2017 at 9:26

2 Answers 2

1

Normally you will pass three values to the mail() function plus some headers. In the example below I skip the value for the message value, because the message is defined as a MIME part together with the attachment.

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) { $file = $path.$filename; $file_size = filesize($file); $handle = fopen($file, "r"); $content = fread($handle, $file_size); fclose($handle); $content = chunk_split(base64_encode($content)); $uid = md5(uniqid(time())); $header = "From: ".$from_name." <".$from_mail.">\r\n"; $header .= "Reply-To: ".$replyto."\r\n"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; $header .= "This is a multi-part message in MIME format.\r\n"; $header .= "--".$uid."\r\n"; $header .= "Content-type:text/plain; charset=iso-8859-1\r\n"; $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; $header .= $message."\r\n\r\n"; $header .= "--".$uid."\r\n"; $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here $header .= "Content-Transfer-Encoding: base64\r\n"; $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"; $header .= $content."\r\n\r\n"; $header .= "--".$uid."--"; if (mail($mailto, $subject, "", $header)) { echo "mail send ... OK"; // or use booleans here } else { echo "mail send ... ERROR!"; } } 

Below is an example on how I use this function to send an email message with one attached .zip file:

$my_file = "somefile.zip"; $my_path = "/your_path/to_the_attachment/"; $my_name = "Olaf Lederer"; $my_mail = "[email protected]"; $my_replyto = "[email protected]"; $my_subject = "This is a mail with attachment."; $my_message = "Hallo,rndo you like this script? I hope it will help.rnrngr. Olaf"; mail_attachment($my_file, $my_path, "[email protected]", $my_mail, $my_name, $my_replyto, $my_subject, $my_message); 
Sign up to request clarification or add additional context in comments.

6 Comments

hi, have u done, or getting any error, you also use php mailer class to send mail using SMTP with your server host, username and password. SMTP also allow to send mail from localhost. so if u want SMTP example reply me i will provide u his details also
in below example you need to pass //$mail->SMTPDebug = 3; //Enable verbose debug output $mail->isSMTP(); //Set mailer to use SMTP $mail->Host = 'smtp1.example.com;smtp2.example.com'; //Specify main and backup SMTP servers $mail->SMTPAuth = true; //Enable SMTP authentication $mail->Username = '[email protected]'; //SMTP username $mail->Password = 'secret'; //SMTP password $mail->SMTPSecure = 'tls'; //Enable TLS encryption, ssl also accepted$mail->Port = 587; $mail->setFrom('[email protected]', 'Mailer'); $mail->addAddress('[email protected]', 'Joe User'); // Add a recipient
create one email in your cpanel and user his smtp detail or just pass GMAIL or any other email smtp details here to send mail from localhost in your domain it possible gmail or other client details is not work for security reason, for that you must pass your domain email it ex. ([email protected]) where example.com is you own domain
update you question and add latest code so i will check it and reply u.
Give me ur number or email id, i will send error on whatapp to u
|
1

Download the PHPMailer library from the link given below:

https://github.com/PHPMailer/PHPMailer

and then use this code

require_once('../PHPMailer/class.phpmailer.php'); $mail = new PHPMailer(); $mail->From = ''; $mail->FromName = ''; $mail->Subject = ''; $MESSAGE_BODY = "Name: ".$name." "."\r\n"; $MESSAGE_BODY .= "Email: ".$email." "."\r\n"; $MESSAGE_BODY .= "Contact No.: ".$cno." "."\r\n"; $MESSAGE_BODY .= "Message: ".$message." "."\r\n"; $mail->Body = $MESSAGE_BODY; $mail->AddAddress( '' ); $mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']); if ($mail->Send()) { echo "Mail Sent"; } else { echo "Could not send mail"; } 

It will work fine.

14 Comments

i used but it not working its show "Could not send mail"
<?php require_once('phpmailer/class.phpmailer.php'); $mail = new PHPMailer(); $mail->From = ''; $mail->FromName = ''; $mail->Subject = ''; $MESSAGE_BODY = "Name: ".$username." "."\r\n"; $MESSAGE_BODY .= "Contact No.: ".$contact." "."\r\n"; $MESSAGE_BODY .= "Message: ".$message." "."\r\n"; $mail->Body = $MESSAGE_BODY; $mail->AddAddress( '' ); $mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['username']); if ($mail->Send()) { echo "Mail Sent"; } else { echo "Could not send mail"; } ?>
yes bro but it still not working. ;( Please send me full code of php ..
i have already sent you the full code.....try to send the email firstly without attachment and one more thing is your application is online or offline?
just upload the error image file at onionfiles.com and give me the link.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.