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>");
}
}
?>
Related
Having troubles sending email throw php on centos 6.7 with cpanel installed.
I assume the problem with Mail.php reference path but I don't know how to correct it. The mail.php file located here: /home/userfolder/php/Mail.php (I have installed the following PEAR packages: Auth_SASL, Mail, Net_SMTP, Net_Socket)
The error which I get is:
Fatal error: Class 'Mail' not found in home/userfolder/public_html/_sendmail.php on line 14
<?php
ini_set("include_path", '/home/userfolder/php:' . ini_get("include_path") );
$from = "Sandra Sender <info#domain.com>";
$to = "Ramona Recipient <info#domain.com>";
$subject = "Email";
$body = "Hi,\n\nHow are you?";
$host = "mail.domain.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>");
}
?>
Add into begin of file
require_once dirname(dirname(__FILE__)) . '/Mail.php';
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');
Good day! I have tried and search everything I can find.. but to no avail, code is still not working, and I'm not even getting any error message. I have no problem with my variables, it is in the "mail" part i think. Please help.
require "Mail.php";
$from = 'angelsofmaryacademy#gmail.com';
$to = $hidden1;
$subject = $hidden2;
$body = $_POST['replytomsg'];
$host = "smtp.gmail.com";
$port = "465";
$user = "angelsofmaryacademy#gmail.com";
$pass = "Ang3lsofmaryacademy";
$headers = array ("From" => $from, "To" => $to, "Subject" => $subject);
$smtp = #Mail::factory("smtp",array(
"host" => $host,
"port" => $port,
"auth" =>true,
"username" => $user,
"password" => $pass));
$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);