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);
Related
This seems to not work. My webhost claims to have PEAR installed. Does anybody spot any mistakes? I do have every var/field correct. But yet, when this runs, it stops the loading of the rest of the php html page.
<?php
// Report all PHP errors
error_reporting(-1);
if (isset($_POST['email'])) {
require_once "Mail.php";
$from = "No Reply <redacted#redacted.redacted>";
$to = "redacted";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "redacted.redacted.redacted";
$username = "redacted#redacted.redacted";
$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>");
}
}
?>
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.
I'm trying to send email via PHP script. I can send successfully but no email arriving to the recipient under Cc field.. Is there something wrong with the headers?
function send_email($to,$subject,$body,$cc = ''){
require_once "Mail.php";
$from = "<removed>";
$host = "smtp.domain.com";
$username = "<removed>";
$password = "<removed>";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
'Cc' => $cc);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
$response["success"] = 0;
$response["message"] = $mail->getMessage();
echo json_encode($response);
}else {
$response["success"] = 1;
$response["message"] = "Email sent to " . $to;
echo json_encode($response);
}
}
If you use PHPMailer (from your comment), add the addresses like so:
$mail->addCC('somebodyelse#bla.com', 'somebody else');
$mail->addCC('otherguy#blabla.com', 'other somebody');
Or call the addCC method in a loop for your convenience.
I moved to PHPMailer and It is now working..
This is the working code
<?php
// array for JSON response
$response = array();
$to = $_POST['rcpt'];
$subject = $_POST['subject'];
$body = $_POST['msgBody'];
$cc = $_POST['cc'];
send_email($to,$subject,$body,$cc);
function send_email($to,$subject,$body,$cc = ''){
require_once "Mail.php";
$from = "monitor<monitor#m2techtronix.com>";
$host = "smtp.domain.com";
$username = "monitor#m2techtronix.com";
$password = "gUZfE6SVLV";
// $headers = array ('From' => $from,
// 'To' => $to,
// 'Subject' => $subject,
// 'Cc' => $cc);
$headers['From'] = $from;
$headers['To'] = $to.", ".$cc;
$headers['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)) {
$response["success"] = 0;
$response["message"] = $mail->getMessage();
echo json_encode($response);
}else {
$response["success"] = 1;
$response["message"] = "Email sent to " . $to;
echo json_encode($response);
}
}
?>
Required classes can be found here - https://github.com/romelemperado/PHPMailer
This is how I send a mail using PEAR's Mail package.
Note you also need to install the Mail/Mime package - which is trivial to do.
<?php
require_once 'Mail.php';
require_once 'Mail/mime.php';
$from = "from#example.com";
$msg = "this is a test";
$to = 'to#example.com';
$cc = 'cc#example.com';
$sbj = "Testing";
$message = new Mail_mime();
$message->setTXTBody($msg);
$message->setHTMLBody("<html><body>$msg</body></html>");
$body = $message->get();
$extraheaders = [
"From" => $from,
"Cc" => $cc,
"To" => $to,
"Subject" => $sbj
];
$mail = Mail::factory("mail");
$headers = $message->headers($extraheaders);
// In case you have more than one address in the $to variable.
$addresses = implode(",", [$to]);
if ($mail->send($addresses, $headers, $body)) {
echo "Successfully Sent";
} else {
echo "Message Failed";
}
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');
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