Change email header in php pear mail - php

I'm trying to figure out how send email from php using google's SMTP servers, but change the FROM header to match my domain. The email sends fine, but the recipient sees it sent from myemail#gmail.com, rather than me#mydomain.com. I'm using php pear mail to send.
require_once("Mail.php");
$from = "Me <me#mydomain.com>";
$to = "Zach <myemail#gmail.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "smtp.gmail.com";
$username = "****";
$password = "****";
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}

You must set a ssl encryption. Google can help you http://www.google.com/search?q=pear+mail+ssl

Related

Sending with PHP PEAR Mail fails using SSL+AUTH

Using code similar to below: On my server side I get an error that seems to imply it is trying use a certificate rather than the user/pass I'm providing (also below). Anyone else seen that, and have you gotten it to work? I've tried googling the error but I seem to just get a bunch of irrelevant results. Linux/PHP7.2.5 (also fails on PHP5.4.20) BUT seems to work just fine under Windows/PHP7
SSL_accept error from mail.XXXXXX.com[xxx.xxx.xxx.xxx]: 0
warning: TLS library problem: 20353:error:14094418:SSL routines:SSL3_READ_BYTES:tlsv1 alert unknown ca:s3_pkt.c:1293:SSL alert number 48:
<?php
require_once "Mail.php";
$from = "Sandra Sender <sender#example.com>";
$to = "Ramona Recipient <recipient#example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://mail.example.com";
$username = "smtp_username";
$password = "smtp_password";
$port = 465;
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => 'PLAIN',
'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>");
}
?>
The relevant part of the error message is
unknown ca
which means "certificate is signed by an unknown certificate authority".
Check if the root CA list is up to date on the machine.

Pear mail not working without any error message

I am trying to using Pear mail on my hostgator server. Firstly I've installed the mail package using cpanel and net_smpt was already installed, however when I required the Mail.php class it insisted that the Net folder be within the Mail folder - they were both at the same level before. So I've moved Net inside Mail and it stopped complaining - but maybe I've just messed up everything (maybe not: let me know).
This is my test script, mostly copied from tutorials:
<?php
echo 'Test Pear<br>';
require_once("/home3/myaccountname/php/Mail.php");
$from = "info#mydomain.com";
$to = "myrealemail#gmail.com";
$subject = "Test pear";
$body = "Lorem ipsum dolor sit amet, consectetur adipiscing elit...";
$params['host'] = 'gator3015.hostgator.com';
$params['port'] = '465';
$params['username'] = 'info#mydomain.com';
$params['password'] = 'mypassword';
$params['debug'] = 'true';
$headers = array('From' => $from, 'To' => $to, 'Subject' => $subject);
$smtp = Mail::factory('smtp', $params);
echo 'good so far';
$mail = $smtp->send($to, $headers, $body);
echo 'Still there';
if ( PEAR::isError($mail) ) {
echo("<p>Error sending mail:<br/>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message sent.</p>");
}
?>
The SMTP parameters have been tested using the codeigniter email class, where they succesfully send emails (but as cronjobs and codeigniter refuse to work for me I'm trying to use Pear Mail instead). It requires SSL but apparently it's on by default with Pear Mail.
I've made sure the PHP error messages were displayed for my test. What happens is that I see all my echos except for "Still there" so something goes wrong when I call $smtp->send(). However no error message is displayed, despite setting debug as true. The if(PEAR::isError($mail)) doesn't seem to have any effect either. And no email is sent.
So I'm a bit at a loss to know what's going wrong. Any idea?
UPDATE: the host does not support SMTP for some reason, which explains why I had the weird issue. Using Mandrill works fine.
Try this example,
<?php
require_once "Mail.php";
$from = "Sandra Sender <sender#example.com>";
$to = "Ramona Recipient <recipient#example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "mail.example.com";
$username = "smtp_username";
$password = "smtp_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>");
}
?>
Also this one,
<?php
require_once "Mail.php";
$from = "Sandra Sender <sender#example.com>";
$to = "Ramona Recipient <recipient#example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://mail.example.com";
$port = "465";
$username = "smtp_username";
$password = "smtp_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>");
}
?>

Unable to send mail through google smtp server using Mail.php

My page php code is this
`
require_once "Mail.php";
$from = "<pramodkumar.damam73#gmail.com>";
$to = "<pramodkumar.damam73#gmail.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "pramodkumar.damam73#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>");
}
?>`
Initially it shown many errors saying isError and send functions are not static. So I modified PEAR.php and Mail.php's functions as static. But now I am getting error like
Failed to connect to ssl://smtp.gmail.com:465 [SMTP: Failed to connect socket: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? (code: -1, response: )]
Please fix my problem.

MAMP PRO PHP Mail - Gmail

Hi I´m trying to send mails on localhost to my gmail account. I tried the postfix tutorial on the MAMP Pro website but that did not work for me.
Is there any way to enable sending mails from and to my gmail-account on localhost?
I think you will want to authenticate and send directly with the gmail server. For reasons related to spam prevention, I have seen issues with sending from a local machine to and from my gmail account.
You could check out the PEAR Mail package. Using it is fairly simple (code borrowed from another site):
<?php require_once "Mail.php";
$from = "Sandra Sender <sender#example.com>";
$to = "Ramona Recipient <recipient#example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://mail.example.com";
$port = "465";
$username = "smtp_username";
$password = "smtp_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>");
}
?>

simple way to add smtp to existing mail.php

I have an existing email.PHP that sends emails based on certain functions with in my website, similar to forum notifications.
However its very hit n miss with sending them, worked well for two days then just stopped.
My question is is there a simple way to add smtp instructions to the existing script so that it will use Gmail rather than PHP mail()
I can supply current script if required.
Thanks in advance.
You need to install PEAR Mail. Then you can do it like this.
<?php
require_once "Mail.php";
$from = "<from#gmail.com>";
$to = "<to#yahoo.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "myaccount#gmail.com"; //<> give errors
$password = "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>");
}
?>

Categories