How to Send Email with Attachment Using PHP

Send attachments with PHP Mail()?

I agree with @MihaiIorga in the comments – use the PHPMailer script. You sound like you're rejecting it because you want the easier option. Trust me, PHPMailer is the easier option by a very large margin compared to trying to do it yourself with PHP's built-in mail() function. PHP's mail() function really isn't very good.

To use PHPMailer:

  • Download the PHPMailer script from here: http://github.com/PHPMailer/PHPMailer
  • Extract the archive and copy the script's folder to a convenient place in your project.
  • Include the main script file -- require_once('path/to/file/class.phpmailer.php');

Now, sending emails with attachments goes from being insanely difficult to incredibly easy:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$email = new PHPMailer();
$email->SetFrom('you@example.com', 'Your Name'); //Name is optional
$email->Subject = 'Message Subject';
$email->Body = $bodytext;
$email->AddAddress( 'destinationaddress@example.com' );

$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';

$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );

return $email->Send();

It's just that one line $email->AddAttachment(); -- you couldn't ask for any easier.

If you do it with PHP's mail() function, you'll be writing stacks of code, and you'll probably have lots of really difficult to find bugs.

Send email with attachment php not working

Are you looking for something like this:

This should work for you for this scenario:

<?php 
function mail_attachment($filename, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$body = '';
$headers = '';

$file = $filename;
$separator = md5(time());
$attachment = chunk_split(base64_encode(file_get_contents($file)));
$headers = "From: ".$from_name."<".$from_mail.">" . PHP_EOL;
$headers .= "Reply-To: ".$replyto . PHP_EOL;
$headers .= "MIME-Version: 1.0".PHP_EOL;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . PHP_EOL . PHP_EOL;
$body .= "Content-Transfer-Encoding: 7bit" . PHP_EOL;
$body .= "This is a MIME encoded message." . PHP_EOL;
$body .= "--" . $separator . PHP_EOL;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"" . PHP_EOL;
$body .= "Content-Transfer-Encoding: 8bit" . PHP_EOL . PHP_EOL;
$body .= $message . PHP_EOL;
$body .= "--" . $separator . PHP_EOL;
$body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . PHP_EOL;
$body .= "Content-Transfer-Encoding: base64" . PHP_EOL;
$body .= "Content-Disposition: attachment" . PHP_EOL . PHP_EOL;
$body .= $attachment . PHP_EOL;
$body .= "--" . $separator . "--";
if (mail($mailto, $subject, $body, $headers)) {
echo "Message sent!";
} else {
echo "ERROR sending message.";
}
}

$my_file = "2.doc";
$my_name = "Your Name (or) Your Business";
$my_mail = "myemail@yahoo.com";
$my_replyto = "replyemail@gmail.com";
$to_email = "replyemail@gmail.com";
$my_subject = "Your file has arrived!";
$requester = $_POST['Field101'];
$message = "Hey $requester, Your custom email message goes here";
mail_attachment($my_file, $to_email, $my_mail, $my_name, $my_replyto, $my_subject, $message);
?>

PHP: How to send email with attachment using smtp settings?

Found this code as one of the first hits of the google://pear mail attachment search.

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

$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = './files/example.zip';
$hdrs = array(
'From' => 'someone@domain.pl',
'To' => 'someone@domain.pl',
'Subject' => 'Test mime message'
);

$mime = new Mail_mime();

$mime->setTXTBody($text);
$mime->setHTMLBody($html);

$mime->addAttachment($file,'application/octet-stream');

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

$mail =& Mail::factory('mail', $params);
$mail->send('mail@domain.pl', $hdrs, $body);

How to send email with pdf attachment using php?

Start using Swiftmailer, your life will be easier.

Use example :

require_once('swift/lib/swift_required.php');

$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setFrom(array($from))
->setTo(array($to))
->setEncoder(Swift_Encoding::get7BitEncoding())
->setSubject($subject)
->setBody($body, 'text/html')
->addPart(strip_tags($body), 'text/plain')
->attach(Swift_Attachment::fromPath($filename))
;
$mailer->send($message);

send email with attachment in php without saving the file to webserver

You have already stored it when you accept the file-upload from PHP.

Simply use it when it is stored in the tmp folder.

PHP will automagically delete it for you when your script ends.



Related Topics



Leave a reply



Submit