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

4

5

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>");
 }

?>
Supernatant answered 3/8, 2012 at 18:6 Comment(2)
Where do you add $mime to the mail message?Caliginous
That is all I have, if it needs to be added then where should I add it.Supernatant
S
8

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);
Soda answered 3/8, 2012 at 18:25 Comment(3)
I tried this, and it works fine. but now I have this warning Warning: Constants may only evaluate to scalar values in C:\xampp\php\PEAR\Mail\mime.php on line 1089Supernatant
You can suppress with @ check line by line see where the warning is throwing.Soda
This doesn't explain where the author is wrong, so it doesn't answer the question. This is just sharing working code. However, the code is easy to read.Beget
C
2

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

Caliginous answered 3/8, 2012 at 18:15 Comment(0)
F
0
<?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'    => '[email protected]',
'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);
?>
Frydman answered 11/7, 2020 at 19:51 Comment(0)
B
-1

This is working code from one of my apps:

/**
  *  
  * @param type $recipient
  * @param type $subject
  * @param type $message
  * @param type $attachment
  *  
  * To make this mail PEAR work, I needed to do:
  * pear install mail
  * pear install Net_SMTP
  * pear install Mail_Mime
  * 
*/

public function sendmail($recipient, $subject, $message, $attachment = '') {
require_once "Mail.php";
require_once "Mail/mime.php";
$from = "Dispatcher <[email protected]>";
$host = "smtp.mymailserver.com";
$port = "587";
$username = "mailuser";
$password = "password";

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

if ($attachment != '') {
  $crlf = "\n";
  $mime = new Mail_mime($crlf);
  $mime->setTXTBody($message);
  $mime->addAttachment($attachment, 'application/pdf');
  $body = $mime->get();
  $headers = $mime->headers($headers);
} else {
  $body = $message;
}

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

$smtp->send($recipient, $headers, $body);
}
Bunco answered 21/5, 2014 at 17:16 Comment(1)
This doesn't explain where the author is wrong, so it doesn't answer the question. This is just sharing working code. Even with that aside, the code is difficult to read.Beget

© 2022 - 2024 — McMap. All rights reserved.