PROBLEM: Sending email using gmail server - php

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.

Related

Undefined error when submitting email form using Pear

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?

Unable to find class for driver smtp error sending emails in php

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.

How to send two emails in a single contact form using PEAR Mail

I have a contact form and I want to receive message through mail and simultaneously send mail to the client. I have the below code it works some times. That is some times I get the message and client also get the message but sometimes only one email is sent either to client or to me but never both all the time.
I have seen example in the following link https://teamtreehouse.com/community/how-to-send-two-different-email-with-different-bodies-using-phpmailer but it uses PHP Mailer I want the same functionality but by using PEAR Mail.
I have seen other similar question over here PEAR Mail using gmail SMTP won't send 2 emails in sucession but the answer given in that question doesn't solve my problem
Below is the code which works some time.
<?php
require_once('Mail.php');
require_once('Mail/mime.php');
if (isset($_POST['Captcha'])) {
$name = filter_var($_POST['Name'] , FILTER_SANITIZE_STRING);
$email = filter_var($_POST['Email'] , FILTER_SANITIZE_EMAIL);
$phone = filter_var($_POST['Phone'] , FILTER_SANITIZE_STRING);
$budget = filter_var($_POST['Budget'] , FILTER_SANITIZE_STRING);
$timeline = filter_var($_POST['Timeline'] , FILTER_SANITIZE_STRING);
$description = filter_var($_POST['Description'] , FILTER_SANITIZE_STRING);
$spam = filter_var($_POST['usernameoftest'] , FILTER_SANITIZE_STRING); // Bot trap
if($spam)
{ // If the hidden field is not empty, it's a bot
die("No spamming allowed bitch!");
} else {
$from = "sales#ganeshghate.com";
$to = "ganeshghate#hotmail.com";
$subject = "Message from : ".$name;
$body = "Client's Name : ".$name."\r\n";
$body .= "Client's Email ID : ".$email."\r\n";
$body .= "Client's Phone Number : ".$phone."\r\n";
$body .= "Client's Budget : ".$budget."\r\n";
$body .= "Client's Timeline : ".$timeline."\r\n";
$body .= "Client's Message : ".$description."\r\n";
$host = "myhost";
$port = "myport";
$username = "myusername";
$password = "mypassword";
$crlf = "\n";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$mime = new Mail_mime($crlf);
$mime->setTXTBody($body);
$body = $mime->get();
$headers = $mime->headers($headers);
$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)) {
$output["errorclientemail"] = "<div>
<h2 class='error'>".$mail->getMessage().$mail->getUserInfo()."</h2>
<h2>Oops there was some error we have not received your message</h2>
<h2>Please report the above error to us via skype , email , phone details are on the left side</h2>
</div>"."\r\n";
} else {
$output["successclientemail"] = "Thank you we will get back to you soon";
}
$bodyback = "We have received your message :".$body."\r\n";
$bodyback .= "We will get back to you soon"."\r\n";
$bodyback .= "With Regards "."\r\n";
$bodyback .= "Ganesh Ghate "."\r\n";
$subjectback = "Thank You for contacting us";
$headersreply = array ('From' => $from,
'To' => $email,
'Subject' => $subjectback);
$crlf1 = "\n";
$mime = new Mail_mime($crlf1);
$mime->setTXTBody($bodyback);
$bodyback = $mime->get();
$headersreply = $mime->headers($headersreply);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($email, $headersreply, $bodyback);
if (PEAR::isError($mail)) {
$output["errorreplyemail"] = "<div>
<h2 class='error'>".$mail->getMessage().$mail->getUserInfo()."</h2>
<h2>Oops there was some error we have not delivered confirmation email to you</h2>
<h2>Please report the above erorr to us via skype , email , phone details are on the left side</h2>
</div>"."\r\n";
} else {
$output["successreplyemail"] = "We have sent confirmation email to you. Please check your inbox / junk mail";
}
echo json_encode($output);
}
}
?>
Thanks in advance

how do i configure smtp setting with mail function?

how do i configure smtp setting with mail function smtp is required because of web hosting doesn't allow to sending email without smtp setting due to security reason.
warning error: mail() has been disabled for security reasons
now how can i configure smtp setting with this mail function ()?
<?php
if(isset($_REQUEST['confirm'])){
$your_email = 'non-reply#test.com'; //CHANGE TO YOUR SETTINGS
$domain = $_SERVER["HTTP_HOST"]; //YOUR DOMAIN AND EXTENSION
$to = $email;
$subject = 'Demo Your Order';
$message = 'test1234';
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: Demo.Pk<'.$your_email.'#'.$domain.'>\r\n" .
//"Reply-To: $from \r\n" .
"X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers);
}
?>
It seems that mail is disable or does not have proper setting in your PHP.ini. I think, you can still send email using SMTP set to some other SMTP server like gmail. Please check below code, it might help you in solving this issue. Below code is using Mail.php frm PEAR
<?php
require_once "Mail.php";
$from = "<me#gmail.com>";
$to = "<you#gmail.com>";
$subject = "Hi!";
$body = "Hi,\n\nHope this helps?";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "me#gmail.com"; //<> give errors
$password = "mypassword";
$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>");
}
?>
You can also use some other Mail libraries like PHPMailer

signing gmail with php imap

My code
$headers = 'MIME-Version: 1.0' . PHP_EOL;
$headers .= 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL;
$headers .= 'From: Me <me#gmail.com>' . PHP_EOL;
imap_mail('you#gmail.com','test',"$output","$headers");
Is there a way to sign the mail? when I use above code to test sending emails, I receive the email but I am getting the error
This message may not have been sent by: me#gmail.com Learn more Report phishing
according to gmail they attach signed data to the headers to authenticate
I am using gmail imap to send the mails
$inbox = imap_open($hostname,$username,$password)
or die('Cannot connect to Gmail: ' . imap_last_error());
Is there a way to authenticate the email using php imap?
If you want to send mails as a Gmail user I recommend using the Gmail's SMTP. As IMAP is mainly used to receive mails from the inbox.
Here's how to send mails from Gmail's SMTP.
First
Make sure the PEAR Mail package is installed.
Typically, in particular with PHP 4 or later, this will have already
been done for you. Just give it a try. (Mail.php)
Then
Sending Mail from PHP Using SMTP Authentication - Example
<?php
require_once "Mail.php";
$from = "Sender <sender#gmail.com>";
$to = "Recipient <recipient#gmail.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "smtp.gmail.com";
$username = "username#gmail.com";
$password = "Gmail 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>");
}
?>
Sending Mail from PHP Using SMTP Authentication and SSL Encryption - Example
<?php
require_once "Mail.php";
$from = "Sender <sender#gmail.com>";
$to = "Recipient <recipient#gmail.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "username#gmail.com";
$password = "Gmail 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>");
}
?>
Source about.com

Categories