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

I am sending emails successfully using following code. But now I want to attach a text file (example: test.txt) with email. Any Idea?
require_once "Mail.php";
$from = "Usman <from#example.com>";
$to = "Naveed <to#example.com>";
$subject = "subject";
$body = "";
$host = "smtp.gmail.com";
$username = "username";
$password = "password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory( 'smtp', array('host' => $host,
'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>" );
}

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

If you additionally make use of the PHP PEAR Mail_Mime module it provides the appropriate handling and encoding to incorporate attachments as part of your email.

here is the code you're looking for:
<?php
require_once "Mail.php"; // PEAR Mail package
require_once ('Mail/mime.php'); // PEAR Mail_Mime packge
$from = "Robert Davis <robertdavis#pobox.com>";
$to = "Sam Hill <sam.hill#aol.com>";
$subject = 'Test mime message with an attachment';
$headers = array ('From' => $from,'To' => $to, 'Subject' => $subject);
$text = 'Text version of email';// text and html versions of email.
$html = '<html><body>HTML version of email. <strong>This should be bold</strong></body> </html>';
$file = './sample.txt'; // attachment
$crlf = "\n";
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'text/plain');
//do not ever try to call these lines in reverse order
$body = $mime->get();
$headers = $mime->headers($headers);
$host = "sasl.smtp.pobox.com";
$username = "robertdavis#pobox.com";
$password = "Kdu48Adi3";
$smtp = Mail::factory('smtp', array ('host' => $host, '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>");
}
?>

Sending email with PHP always feels a little bit like a struggle. If you are able to use them I would recommend one of these two mail libraries for PHP:
PHP Mailer
Swiftmailer

You seem to be using the PEAR Mail package.
Take a look at the Mail_Mine object which does what you're trying to do and has an easy way of adding attachments (simply call addAttachments).
http://pear.php.net/manual/en/package.mail.mail-mime.php

Related

PHP PEAR MAIL Not working with base64 image or dataurl

I am trying to send dynamically created canvas object as an embedded image in mail.For that i have converted canvas to dataurl and given it to mailer library. Unfortunately it is not working with dataurl.
I tried with src having actual path and it worked but dataurl is having issue.
<?php
require_once "Mail.php";
require_once 'Mail/mime.php';
$from = "xyz#gmail.com";
$subject = $_REQUEST['subject'];
$data=$_REQUEST['data']; //dataurl
$img = $_REQUEST['body'];
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "myaccount#gmail.com";
$password = "mypassword";
$to="someone#xyz.com";
$body="<html><body><h1>Report is Here</h1><img src='cid:whatever'/></body></html>";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mime = new Mail_mime("\n");
$mime->setTXTBody("");
$mime->addHTMLImage($data, "image/png","1234.png",false,"whatever");
$mime->setHTMLBody($body);
$body = $mime->get();
$headers = $mime->headers($headers);
$mail = $smtp->send($to, $headers, $body);
;
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("Message successfully sent");
echo "<img src='data:image/png;base64,".$data."'/>";
}
?>
Mail sent successfully and image too rendered as i have put code at last but it doesn't appear in mail.
You are passing a string, while addHTMLImage expects a file name or the binary contents.
Either store it in a temporary file and pass that path, or decode the base64 encoded data to binary, and pass that.

how to send attachment with pear smtp

I want to send a file with email using pear smtp. Email is successfully going to the email. Also an attachment is going with email but with noname and no content in it. When i download the attachment it has only one line "This is a multi-part message in MIME format...".
I didn't understand whats wrong with it. I have attached the code with this question that i am using to send the attachment with email. Please guys review once and let me know what's wrong with this code.
Thanks in advance.
require_once "Mail.php";
include('Mail/mime.php');
$from = "Dispatcher <server#mymailserver.com>";
$host = "smtp.mymailserver.com";
$username = "mailuser";
$password = "password";
$to = "email#example.com";
$subject = "Subject of the email";
$hdrs = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
);
$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = './test.txt';
$crlf = "rn";
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file);
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
return 1;
}
$crlf means "carriage return, line feed" and should be that.
Use "\n" to make it work with e-mails.

Using PHP Pear mail :Failed to add recipient:

I am using Php Pear mail for sending an attachment to the user who fills out a form. If I hard code the "to email" address it works fine. But when I use
$to = $_POST['email'] ; I get the following error.
Failed to add recipient: #localhost [SMTP: Invalid response code received from server (code: 501, response: <#localhost>: no local part)]
<?php
require_once 'Mail.php';
require_once 'Mail/mime.php';
$from = "email#domain.com";
$to = $_POST['email'] ;
$subject = 'Free Diagnostic Test Coupon';
$headers = array ('From' => $from,'To' => $to, 'Subject' => $subject);
$text = 'Please find attached the coupon';// text and html versions of email.
$html = '<html><body>Please find attached the coupon</body> </html>';
$file = 'img/coupon.jpg'; // attachment
$crlf = "\n";
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'image/jpeg');
//do not ever try to call these lines
$host = "host";
$username = "username";
$password = "password";
in reverse order
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp', array ('host' => $host, '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>");
}
?>
Any help will be appreciated.
Check your code by adding the top of it.
error_reporting(E_ALL); ini_set('display_errors', '1');

MIME file attachment is not opening

My code is
<?php
require_once "Mail.php"; // PEAR Mail package
require_once ('Mail/mime.php'); // PEAR Mail_Mime packge
$from = "<meenu.spiralbean#gmail.com>";
$to = "<meenu.spiralbean#gmail.com>";
$subject = "Hi!";
$host = "ssl://smtp.googlemail.com";
$port = "465";
$username = "example#gmail.com";
$password = "example";
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
// attachment
$file = '/var/www/html/formsubmit/TodoList/upload/abc.pdf';
$crlf = "n";
$text="Helllooooo";
$html = "<html> <head> <title>Mail test</title> </head> <body>something</body></html>";
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
if($mime->addAttachment($file))
{
echo "Success";
}
else
{
echo "Failed";
}
$body = $mime->get();
$headers = $mime->headers($headers);
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
The attachment is unable to open even after downloading it.
Mail is coming in inbox and attachment is also there.But the attachment seems like txt file.but I am actually attaching a pdf file. I used SMTP mail and MIME mail functions.
Pls help me.
The relevant code in the mail is:
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream;
name=abc.pdf
Content-Disposition: attachment;
filename=abc.pdf;
size=4997278
The content type is a generic one; try to use application/pdf as second parameter to addAttachment().
Also, NEVER post your real mail address and password anywhere.

Attaching file in the php mail code

This code is working fine in my unix shared hosting account but how do I attach file with it?
I've removed the hostname, login, passwd etc.
<?php
require_once "Mail.php";
$from = "contactus#site.com";
$to = "to email id";
$subject = "this is the message from your domain";
$body = "give your message body here";
$host = "mail.site.com";
$username = "user";
$password = "pass123";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) { echo("" . $mail->getMessage() . "");
} else {
echo("Message Sent successfully ");
}
?>
You have to include both PEAR::Mail and PEAR::Mail_Mime, as it takes both classes to get the full email sent. Example...
include_once('Mail.php');
include_once('Mail_Mime/mime.php');
// include_once('Mail/mime.php');
// The code below composes and sends the email
$message = new Mail_mime();
$message->setTXTBody($text);
$message->addAttachment($path_of_attachment_file);
$body = $message->get();
$extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$visitor_email);
$headers = $message->headers($extraheaders);
$mail = Mail::factory("mail");
$mail->send($to, $headers, $body);

Categories