How to send an email using php script? - php

<?php
require_once "Mail.php";
$from = "<niko#gmail.com>";
$to = "<niko#hotmail.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "<niko#gmail.com>";
$password = "somepassworrd";
$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>");
?>
When I try to execute these php script I get these error
FATAL ERROR Class Mail NOT found ON number line 18
I know the above question is possible duplicate of these
Send email using the GMail SMTP server from a PHP page
But its not working.My PHP version is 5.3.4 Xampp 1.7.4 version.
mail('caffeinated#example.com', 'My Subject', $message); \\ I tried these
But it shows a warning saying that missing headers.
How do I send an email Using the php script?
Can we send an email without using authentication in PHP ? Because vb.net uses server authentication but most of the code i found on google is without authentication for php. so I got a doubt
Finally please help me with these trying from an 2 hours or so!

The PHP mail() function is for sending mail via Sendmail. If you want to use some SMTP server, you can use Zend_Mail which makes this thing very easy:
http://framework.zend.com/manual/en/zend.mail.html
Using Zend_Mail you only need to write something like this:
$config = array('auth' => 'login',
'username' => 'myusername',
'password' => 'password');
$transport = new Zend_Mail_Transport_Smtp('mail.server.com', $config);
$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('sender#test.com', 'Some Sender');
$mail->addTo('recipient#test.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send($transport);
The above handles authentication for you and sends a mail.

Related

Sending SMTP Emails from PHP

I'm a bit stuck here. I have some websites hosted on TSOHosts. I use the php mail() function to send the occassional email, e.g. from a contact form.
The emails have stopped working, and nobody at the hosting provider seems to be capable of fixing it.
They say I should use SMTP emails. OK, I get that, they point me to this article on how to do it:
https://www.lifewire.com/send-email-from-php-script-using-smtp-authentication-and-ssl-1171197
So I write the script (below) and, it doesnt work:
require_once "Mail.php";
$from = "NoReply#mysite.org.uk";
$to = "me#myemailaddress.co.uk";
$subject = "Hi port 465";
$body = "Hello World";
$host = "ssl://mail3.gridhost.co.uk";
$port = "465";
$username = "username#mysite.org.uk;
$password = "Testing123";
$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>";
}
I also have a port 25, non-ssl script, that also doesnt work.
When I say doesnt work, its not throwing an error, just no email arrives (checked in spam).
The hosting company first asked me where the Mail.php is as referred to by the require_once "Mail.php" line
The article suggests that this should be installed on php4+ (Im using 5.6.37)
This is remarkably similar to this article:
smtp configuration for php mail
But I am stummped as to how to proceed.

Given two seperate servers; can PHP decide on the server to recieve e-mails?

I've been thinking, and I have question regarding multiple e-mail addresses and the mail function.
Is it possible to have PHP send an email to a particular smtp server; for instance if I have two addresses.
Configuration
smtp.fakecompany1.co.nz
Intended Recipient
killrawr#fakecompany1.co.nz (smtp.fakecompany1.co.nz)
killrawr#fakecompany1.co.nz (smtp.fakecompany2.com)
Currently, if the smtp of the intended recipient (smtp.fakecompany1.co.nz) is setup then that recipient from the intended smtp (smtp.fakecompany1.co.nz) will receive e-mail; but my question is in regards to whether it could be possible to select an smtp (smtp.fakecompany1.co.nz) and send an email, without requiring to authenticate into a DIFFERENT smtp (smtp.fakecompany2.com).
(source: iforce.co.nz)
TL;DR can I send an email from smtp.fakecompany1.co.nz to smtp.fakecompany2.com (given the email exists on both servers), without modifying the authentication details on the primary server (Due to technical issues with Gmail).
You can SMTP with PEAR::Mail instead of using mail(). See this question for an example: how to use php pear mail
Here is a modified example I copied from the linked Q:
require_once "Mail.php";
$from = "<test#example.com>";
$to = "<testing#example.com>";
$subject = "Hi!";
$body = "Hello world";
$host = "smtp.fakecompany2.com";
$port = "465";
$username = "<testtest#example.com>";
$password = "testtest";
if (YOUR HEADER CHECK HERE) {
$host = "smtp.fakecompany1.co.nz";
}
$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>");
}

PHP/PEAR Mail Not Sending as Text Message

I'm working on a script that will send text messages via email (i.e. ####txt.att.net) and PHP's simple mail() function was not working. I could send email messages just fine through that method, but the texts were not being delivered. I did more research and found that carriers often block messages without a sender and using PEAR with SMTP is a better solution.
However, the following code works to send emails but is still not delivering AT&T text messages:
<?php
require_once "Mail.php";
$from = "XXX <XXX>";
$to = "XXX <XXX#txt.att.net>";
$subject = "Test email using PHP SMTP\r\n\r\n";
$body = "This is a test email message";
$host = "XXX";
$port = "26";
$username = "XXX";
$password = "XXX";
$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>");
}
?>
Is it maybe an issue with headers not being formatted correctly, etc? Any ideas?
According to their example you do not need the new lines after the subject:
http://pear.php.net/manual/en/package.mail.mail.send.php
Check the AT&T response they might decline it if they have you blacklisted or you do not have a valid rDNS or other reasons.
They seem to experience a problem seeing the rDNS records of some IP's.
Use this it will tell you all the info you need.
http://wordpress.org/extend/plugins/xmail-the-right-way
Post the log of it here.

problem in sending mail by gmail smtp in server but works fine in localhost

I have opened a site in a free web hosting company which is not offering mail() facility so I have decided to implement this facility of sending emails using G-mail's SMTP server.
I have used PEAR' Mail package to send mails. It works fine locally but unfortunately its not all working in my website's server.
When the page is opened, it produces a blank white screen even no errors are being mentioned. You can check here to see the script run.
This is my code:
<?php
require_once "Mail.php";
$from = "username#gmail.com";
$to = "username#yahoo.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "username#gmail.com";
$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>");
}
?>
Can anyone help me to understand what's going wrong?
You maybe forgot to upload the Mail.php file or one of the dependencies. A white page often indicates a fatal error.
Gmail blocks some web hosting providers. Contact you your web hosting provider.

sending Emails using PEAR's Mail in PHP is working fine on Windows Dev System but not on Linux Server?

I am using following code to send email using mail.google
<?php
require_once "Mail.php";
$from = "someone#somedomain.com";
$to = "otherone#somedomain.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "mygmailaccount#gmail.com";
$password = "mygmailpassword";
$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 works fine on Windows Development Machine but not on my Amazon Linux Server ?
Please tell me if any additional configuration is required.
Mail.php isn't the only file needed for sending mails via SMTP. You need the following files:
Mail.php
PEAR.php
PEAR5.php
Mail/smtp.php
Net/SMTP.php
Net/Socket.php
Mail/RFC822.php
At least these files were needed when I tried out your example on my own linux server.
I don't know how this Amazon web stuff is working but you probably need to look at some log-file or enable error messages in HTML output somehow or increase the warning level to find out what's really going wrong.
If you want to install the files manually you can download them from here:
http://pear.php.net/package/PEAR/download/
http://pear.php.net/package/Mail/download/
http://pear.php.net/package/Net_Socket/download/
http://pear.php.net/package/Net_SMTP/download/

Categories