I am trying to get an email form working but it keeps erroring out.
My contact.php code looks like this:
<?php
ini_set("include_path", '/home4/hipsiguy/php:' . ini_get("include_path") );
require_once "Mail.php";
$username = 'my#emailaddress.ca';
$password = 'myemailpassword';
$smtpHost = 'mail.myemailserver.ca';
$smtpPort = '465';
$to = 'my#emailaddress.ca';
$from = 'my#emailaddress.ca';
$subject = 'Contact Form';
$successMessage = 'Message successfully sent!';
$replyTo = $_POST['your-email'];
$name = $_POST['your-name'];
$body = $_POST['your-message'];
$headers = array(
'From' => $name . " <" . $from . ">",
'Reply-To' => $name . " <" . $replyTo . ">",
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => $smtpHost,
'port' => $smtpPort,
'auth' => true,
'username' => $username,
'password' => $password
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo($mail->getMessage());
} else {
echo($successMessage);
}
?>
I host with HostGator (if that matters) and have installed the Mail and Net_SMTP extensions through CPanel.
I have tried a couple different methods, all of which error out.
The errors below seem to be the most common.
[16-Sep-2021 08:27:00 America/Chicago] PHP Warning: require_once(Mail.php): failed to open stream: No such file or directory in /home4/hipsiguy/public_html/TEST/contact.php on line 30
[16-Sep-2021 08:27:00 America/Chicago] PHP Fatal error: require_once(): Failed opening required 'Mail.php' (include_path='.:/opt/cpanel/ea-php74/root/usr/share/pear:/usr/lib/pear') in /home4/hipsiguy/public_html/TEST/contact.php on line 30
What am I missing?
Related
I checked out this similar question, but the answer wasn't useful: Pear Mail "Unable to find class for driver smtp" Cannot find file, that is there
So, basically I'm getting an error message of "Unable to find class for driver smtp". I am using Mail PEAR and Net_SMTP php files from:
https://pear.php.net/package/Mail/download
https://pear.php.net/package/PEAR/download
http://pear.php.net/package/Net_SMTP/download
and the section of code where I use these is here:
require_once "Mail.php";
$from = "xyz#gmail.com";
$to = "xyz#gmail.com";
$subject = "Project Request: " . $_POST['project_title'];
$body = "Project In One Sentence: " . $_POST['project_in_one_sentence'] . "\r\n\r\n" .
"Project In Detail: " . $_POST['project_in_detail'] . "\r\n\r\n" .
"Client Name: " . $_POST['your_name'] . "\r\n\r\n" .
"Client Email: " . $_POST['your_email'] . "\r\n\r\n";
$headers = array('From' => $from, 'To' => $to, 'Subject' => $subject);
$smtp = Mail::factory('smtp', array('host' => 'smtp.gmail.com', 'port' => '465', 'auth' => true, 'username' => 'xyz#gmail.com', 'password' => 'xyz'));
if (PEAR::isError($smtp)) {
echo $smtp->getMessage() . "\n" . $smtp->getUserInfo() . "\n";
die();
}
$mail = $smtp->send($to, $headers, $body);
I have all the files in the same directory. I'm not sure why I'm getting the error. Any ideas? Thanks in advance.
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 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 trying to send an email from my wordpress website using the function wp_mail and I am getting this error:
Warning: trim() expects parameter 1 to be string, array given in /var/www/html/wp-includes/class-phpmailer.php on line 973 0
I don't really understand why I am getting it.
This is my current code:
// Contact form Ajax
add_action('wp_ajax_nopriv_submit_contact_form', 'submit_contact_form');
function submit_contact_form(){
if(isset($_POST['email'])) {
$email = $_POST['email'];
$email_to = "info#yyy.com";
$host = "ssl://smtp.gmail.com:465";
$username = 'myEmail#gmail.uk';
$password = 'mypassword';
$email_subject = "You have a new email from $email via yyy.com website";
$message = $_POST['text'];
$headers = array ('From' => $email, 'To' => $email_to,'Subject' => $email_subject);
/*$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));*/
//$mail = $smtp->send($email_to, $headers, $message);
wp_mail( $email_to, $headers, $message );
/*if (PEAR::isError($mail)) {
echo($mail->getMessage());
} else {
echo("Message successfully sent!\n");
}*/
}
}
error_reporting(E_ALL);
ini_set("display_errors", 1);
I installed a package called "PostFix" which is responsible for sending emails from an Ubuntu server (which is the servers linux I use), and it solved the problem!
For some time I was trying to create and send automatic email using php script and gmail server and I always get this error:
Warning: mail() [function.mail]: Failed to connect to mailserver at "ssl://smtp.gmail.com" port 465, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\php\PEAR\Mail.php on line 141
Here is my code:
require_once ("mail.php");
ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("SMTP_PORT", 465);
$to = $sqlmail['email'];
$from = $fromemail['email'];
$body = "Hello, \n\n";
$body .= "This is a request to borrow the following book\n";
$body .= "Title: $title\n";
$body .= "Author: $author\n";
$body .= "Year: $year\n";
$body .= "From the user $_SESSION[username]\n";
$subject = $title . " " . $author . " " . $year;
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "slesher.gmail.com";
$password = "xxxxxxxx";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = new Mail();
$smtp ->factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
If you can deliver to a local Postfix and let Postfix deliver to Gmail, you can try the solution, I've blogged about some time ago.