php pear mail not modifying 'from' when using google apps gmail - php

I have google apps setup for one of my site's emails, thus making the site's built-in email routing useless when doing things from the web. What i need to do , is have it use ssl/smtp to connect to the google apps setup.
To accomplish this, I've used PEAR Mail and mime (for the HTML contents).
The messages get sent without an issue... the ONLY problem i'm having, is that the 'From' header isn't being saved across transmission. Instead, the account email is in the 'from' header.
The accounts exist on the webserver's end (which means nothing since its all going through google), and i've added aliases to my gmail apps administration end. But no matter what i do, its not changing the 'from'.
Is this just something i'm going to run into when using a single account with google apps' gmail? (--forced 'from' from the account name?)
Thanks
--for those who were wondering, here is an example function for the mail sending:
function pearMail($from, $fromTitle, $to, $subject, $text, $html)
{
require_once "Mail.php";
require_once('Mail/mime.php');
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "name#domain.com";
$password = "PASSWORD";
$headers = array ('From' => $from,
'Return-Path' => '-do not reply-',
'To' => $to,
'Subject' => $subject);
$crlf = "\n";
// Creating the Mime message
$mime = new Mail_mime($crlf);
// Setting the body of the email
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$body = $mime->get();
$headers = $mime->headers($headers,true);
// Sending the email
$mail =& Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
}

See the google help on changing From::
The custom 'From:' feature works only if you already own the account linked to the alternate address. To send mail with a different Gmail username, you must first sign up for that address.

Related

Sending email to Office 365 using PEAR (PHP)

I am having some trouble figuring out why my emails are not being sent out to my Office 365 account. Before my companies migration to Office 365 we had an internal mail server and getting emails to send out was a piece of cake. However, now that we have moved to Office 365 it has become a real headache getting emails to send out. I thought that maybe I needed to set up an SMTP Relay and point to that server in my php code but it seems that is not the issue. Below is my code used to send out a test email:
$from = "example#example.com";
$to = "test_user#example.com";
$bcc = '';
$subject = "Hi!";
$body = "Hi,\n\nLooks like it worked.";
$host = 'smtp.office365.com';
$port = '587';
$username = 'example#example.com';
$password = '**********';
$headers = array(
'Port' => $port,
'From' => $from,
'To' => $to,
'Subject' => $subject,
'Content-Type' => 'text/html; charset=UTF-8'
);
$recipients = $to.", ".$bcc;
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($recipients, $headers, $body);
echo "test";
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
Once I run this code I am presented with the following error:
authentication failure [SMTP: SMTP server does not support authentication (code: 250, response: SN1PR19CA0003.outlook.office365.com Hello [68.15.136.46] SIZE 157286400 PIPELINING DSN ENHANCEDSTATUSCODES XXXXXXXA 8BITMIME BINARYMIME XXXXXXXB)]
I have researched the heck out of this error and have had no luck in resolving my issue. I am wondering if it has something to do with the fact that the server I am generating the php code on is a development server and not on a live site. I am thinking that since this is not a live site I need to point it to a server that uses SMTP relay in order to get it out onto the internet, however, like I said I have had no luck when doing this.
Any assistance is greatly appreciated.
Thanks in advance.

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.

Using HTML Mime Mail for PHP to send email, but need to authenticate through Exchange server

First off, the server: Exchange 2003 sp2 running on Windows 2003 Server sp2
I have a script that sends email to two email accounts, one called students# and the other being fs# (faculty/staff). We are setting both those email accounts to only accept incoming email by authenticated users on the exchange server, to spare ourselves from spam/junk mail. So right now the emails being sent by the script are not successful. I have the return-path email as a legit user, but it is not authenticated. I noticed that when I tried sending a test via my mail client (Apple's Mail.app) and since I use email through their IMAP server and not through exchange, my email failed as well.
Here is the code for sending the email:
$mail = new htmlMimeMail();
$message = $today.$announcements.$food.$upcoming;
$mail->setHTML($message);
$mail->setSubject($subject);
$mail->setSMTPParams('mail.domain.com', 25, true, 'user', 'pass');
$mail->setFrom("no-reply#domain.com");
$mail->setReturnPath("webmaster#domain.com");
if($message)
$mailresult = $mail->send(array($emailto));
I have never authenticated with an exchange server using the HTML Mime Mail for PHP (http://www.phpguru.org/static/mime.mail.html) class before. Any help would be appreciated.
Maybe there is another PHP class that easily allows authentication with an Exchange server?
EDIT: Are there any php mail classes out there that authenticate properly with an exchange server?
Another EDIT: The Exchange Server uses NTLM authentication and uses Active directory. Hope this helps.
Exchange supports the standard SMTP Auth mechanism, so I would use that. Here is an example using Pear::Mail from here.
<?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>");
}
?>

Categories