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.
Related
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');
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.
I got a contact form that succesfully sends emails to me.
heres the extract:
$_POST['message'] = wordwrap($_POST['message'], 70);
mail ('myemail#test.com', $_POST['subject'], $_POST['message'] , $_POST['email']);
echo "<div class='registertext'>Your email was succesfully sent to a member of the administration team. Please wait 24 hours for as to reply and ensure you check your junk mail!<br />To login please click <a href='login.php'>here</a></div>";
The issue I have is, the email gets sent from my host. Not a email I want to specify. How would I overcome this?
You can specify it in the email headers:
$recipient = "recipient#test.com";
$from = "You#yoursite.com";
$replyTo = "You#yoursite.com";
$subject = "Hi!";
$text = "<p>This is a test!<p>";
$headers = "MIME-Version: 1.0\r\n"
."Content-Type: text/html; charset=utf-8\r\n"
."Content-Transfer-Encoding: 8bit\r\n"
."From: =?UTF-8?B?". base64_encode([Your Name]) ."?= <$from>\r\n"
."Reply-To: $replyTo\r\n"
."X-Mailer: PHP/". phpversion();
//send it!
if (mail($recipients, $subject, $text, $headers, "-f $from")){
echo "sent";
} else {
echo "did not send";
};
but there's a good chance it will get caught be SPAM filters. Your best bet in this case would be to use a PHP mailing library that handles SMTP emailing and uses your actual account to send mail (there are several packages that can handle this for you: Pear Mail, and PHP Mailer amongst others.
You can use PEAR mail which will use a SMTP account. Here is some code from my mail form I use
$from = "Name <webmaster#domain.com>";
$to = "Name <address#domain.com>";
$subject = "Subject";
$body = 'A message!';
$host = "ssl://domain.com";
$port = "465";
$username = "username";
$password = "password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$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>");
}
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);
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