Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send emails with PHP using the PEAR Mail package with attachment

I am trying to send an email with PHP by using the PEAR mail package with an attachment. The email sends successfully with a code I got from the internet. However, the attachment does not get sent or attached. Where am I going wrong, below is my code.

<?php

require_once "Mail.php"; 
require_once "Mail/mime.php";

$from = "<[email protected]>";
$to = "<[email protected]>";
$subject = "Testing email from PHP with attachment";
$body = "Testing email from PHP with attachment";
$file = "invoices/PRINV7_3.pdf";
$host = "host";
$port = "25";
$username = "username";
$password = "password";

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);

$mime = new Mail_mime();

if ($mime->addAttachment($file,'application/pdf')){
    echo "attached successfully! </br>";
} else {
    echo "Nope, failed to attache!! </br>";
}

$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'port' => $port,
    'auth' => true,
    'username' => $username,
    'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
 } else {
  echo("<p>Message successfully sent!</p>");
 }

?>
like image 469
Richard Tonata Avatar asked Aug 03 '12 18:08

Richard Tonata


People also ask

How email attachment is send with PHP?

The PHP mail() function with some MIME type headers can be used to send email with attachment in PHP. In the following example code, MIME and Content-Type headers are used with mail() function to send email with attachment using PHP. $to – Recipient email address. $from – Sender email address.

How to send emails with PHP mail() function and PHP Mailer?

To send an email using the PHP mail feature, create a new PHP file in the public_html directory and input the mail() function. Then, execute the script using your web browser.


2 Answers

I haven't checked this code so if don't work I am sorry

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' => '[email protected]',
    'Subject' => 'Test mime message'
);
$host = "host";
$port = "25";
$username = "username";
$password = "password";

$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('smtp',
  array ('host' => $host,
    'port' => $port,
    'auth' => true,
    'username' => $username,
    'password' => $password));
$mail->send('postmaster@localhost', $hdrs, $body);
like image 197
ARIF MAHMUD RANA Avatar answered Oct 06 '22 08:10

ARIF MAHMUD RANA


I believe you need to add the headers from your $mime object to your $headers

$attachmentheaders = $mime->headers($headers);

and then change your mail call:

$mail = $smtp->send($to, $attachmentheaders, $body);

Here's a tutorial that may help: http://www.html-form-guide.com/email-form/php-email-form-attachment.html

like image 23
ametren Avatar answered Oct 06 '22 08:10

ametren