Do you know if it is posible to set a DSN communication using the Pear Mail function in PHP?
Explain:
If a write this code in a telnet session:
telnet smtp.example.com 25
[...]
mail from: me#example.com
250 2.1.0 Ok
rcpt to: you#fakemail.com NOTIFY=SUCCESS,FAILURE,DELAY ORCPT=rfc822;you#fakemail.com
250 2.1.5 Ok
[]
i received a notification mail with a status code from the receiver smtp server (relayed, failed, etc.) when my smpt server sends the email. Now i want to do the same using Pear Mail, but I can't find where i have tu put this option.
This is my code:
$messageTEXT= "..." // text_message
$messageHTML="..." // html message
$headers=array();
$headers['From'] = "me#example.com";
$headers['To'] = "you#fakemail.com";
$headers['Subject'] = "Test mail";
$headers['X-Mailer']="My PHP mailer";
$headers['X-Priority']=3;
$headers['Errors-To'] = "me#example.com";
$headers['Return-Path'] = "me#example.com";
$headers['Disposition-Notification-To'] = "me#example.com";
$message = new Mail_mime();
$message->setTXTBody($messageTEXT);
$message->setHTMLBody($messageHTML);
$mimeparams=array();
$mimeparams['charset']= "UTF-8";
$mimeparams['text_encoding']="8bit";
$mimeparams['text_charset']="UTF-8";
$mimeparams['html_charset']="UTF-8";
$body = $message->get($mimeparams);
$headers = $message->headers($headers);
$smtp = Mail::factory(
'smtp',
array(
'host' => "smtp.example.com",
'port' => "25",
'auth' => true,
'username' => "myuser",
'password' => "mypass")
);
$mail=$smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
print ("Error");
return false;
}
http://pear.php.net/manual/en/package.mail.mail.php
You will have to hack PEAR::Mail and Net::SMTP a bit to get this to work.
The standard Mail_smtp::send() method calls into Mail_RFC822::parseAddressList() which will reject the extra data. Commenting out those lines (around line 274) should get you started.
Then you'll need to hack Net_SMTP::rcptTo() to pass the raw data rather than wrapping it in angle brackets.
Make sure your data is being sanitized somewhere else if you're going to circumvent those methods.
Related
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.
I'm trying to send an HTML message while using SMTP authentication to Gmail in PHP. Here is the script that I am using:
require_once "Mail.php";
require_once 'Mail/mime.php';
$from = "Some Name <myemail#gmail.com>";
$to = "Other Name <otheremail#gmail.com>";
$subject = "This is a test";
$crlf = "\n";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "myemail#gmail.com";
$password = "mypass";
$headers = array ('From' => $from,
'Return-Path' => $from,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mime = new Mail_mime($crlf);
$mime->setTXTBody("This is a test email message");
$mime->setHTMLBody($body);
$body = $mime->get();
$headers = $mime->headers($headers);
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
Note: the $body is an HTML table with images and other info.
When I execute the script it fails with the following error:
Failed to set sender: Some name [SMTP: Invalid response code
received from server (code: 555, response: 5.5.2 Syntax error.
c6sm20541406obd.22)]
Here is what I've tried to see what is going wrong:
1. Using the same script using 'Mail' instead of 'smtp' i.e.
$smtp = Mail::factory('Mail');
This works just fine.
2. Using the same script w/o the mime.php, this also works but doesn't allow one to send an HTML email.
Does anybody know how I can combine the two so that I'm still using SMTP authentication and send an HTML message?
EDIT:
Here is the dump of $mime->headers():
[MIME-Version] => 1.0
[From] => Some Name
[Return-Path] => Some Name
[Subject] => This is a test
[Content-Type] => multipart/alternative;
boundary="=_8662996a1f586248545d9f01f48e916d"
See the below URL I think it is very help full to you.
How to send an HTML email using SMTP in PHP
The Gmail's SMTP server address is SMTP.GOOGLEMAIL.COM not SMTP.GMAIL.COM.
Therefore Your settings should be:
// ...
$smtp=array();
$smtp['host']='ssl://smtp.googlemail.com';
See another example:-
Send An HTML & Plain Text Email Using An SMTP Server With PHP & PEAR
http://tonyvirelli.com/slider/php-html-email-using-smtp/
Figured it out myself finally.
Thanks to Abid Hussain for providing the link to the example here: http://tonyvirelli.com/slider/php-html-email-using-smtp/
Solution:
Remove the 'Return-Path' => $from from the headers array.
Add To => $to to the headers array(without this, the to header of the received email is Undisclosed recipients)
Try embracing the names in double quotes, like so:
$from = '"Some Name" <myemail#gmail.com>';
$to = '"Other Name" <otheremail#gmail.com>';
try it and tell us about it.
Update:
Ok, above didn't work, so I just would tell you what it's working for me in my php pages to send emails through smtp gmail.
I use http://phpmailer.sourceforge.net/
smtp server set to smtp.gmail.com
it uses TLS protocol in port 587
so based on that you could try to change your config to:
$host = "tls://smtp.gmail.com";
$port = "587";
you can see here how to send mail through gmail with phpmailer.
I had this issue and via debugging managed to get all kinds of different errors (below). Strangest thing is that it completely stopped working on about 11/2/2017. I didn't do any updates. Previously it would randomly work so I put in a retry and it always succeeded < 5 tries, until the 2nd. Strange.
Failed to connect to ssl://smtp.gmail.com:465 [SMTP: Invalid response code received from server (code: -1, response: )]
fsockopen(): Failed to enable crypto
fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error)
SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed
(and so on)
Another used posted this troubleshooting step that helped me get more errors so I could Google more what could be the cause:
$result = fsockopen('ssl://smtp.gmail.com', 465, $error_no,
$error_message, 5);
if($result === false) {
echo "error no:
$error_no error message: $error_message"; echo print_r($result,true);
}else{
echo 'success\n\n';
}
My fix was to add a CA bundle file, since apparantly PHP couldn't verify the cert of Google:
Download CA bundle https://curl.haxx.se/docs/caextract.html & extract to C:\Program Files\PHP\ssl\cacert.pem
Specify location in php.ini: openssl.cafile = C:\Program Files\PHP\ssl\cacert.pem
Using PHP, I'm attempting to route email through AuthSMTP (a hosted SMTP service). The problem is that the PEAR mail factory automatically tries to negotiation a TLS connection with the server. Rather than simply ignoring the attempt, AuthSMTP throws an error. I need a way to explicitly tell the Mailer class not to try to use TLS. Any suggestions?
$from = "Example <noreply#example.com>";
$to = $email;
$subject = "This is an email";
$body_text = "plain text here";
$body_html = "<h1>HTML here!</h1>";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$mime = new Mail_mime('rn');
$mime->setTXTBody($body_text);
$mime->setHTMLBody($body_html);
$body = $mime->get();
$hdrs = $mime->headers($headers);
$host = "mail.authsmtp.com";
$port = 26;
$username = "my_username";
$password = "whatever_password";
$mailer = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'port' => $port,
'username' => $username,
'password' => $password));
if (PEAR::isError($res)) {
throw new Exception($res->getMessage());
} else {
return true;
}
AuthSMTP is giving me the following error:
SMTP: Invalid response code received from server (code: 428, response: 4.0.0 Your account is using SSL - either disable it in your email client or enable it at http://control.authsmtp.com)
The easiest way is to change $tls=true to $tls=false in the definition of the function auth in PEAR\NET\SMTP.php
This can't be done with a current release of the PEAR Mail package - but is a requested feature. I've uploaded a patch so that this can be done. Hopefully a new release will be distributed soon.
Switched to using PHPMailer instead and had it working in 5 minutes.
FIrst of all I don't intend to spam - I 'm building email facility in my application and would like to be able to include any email address in the from header of the email while be able to send it from any mail server i.e like show a yahoo address in the email from field while send it from my own smtp server? Is it possible? I'm using php and zend framework here
Zend_Mail allows you to specify all that.
Setting up transport (during bootstrap):
$tr = new Zend_Mail_Transport_Smtp($mail_smtp_host, array(
'auth' => 'login',
'username' => $mail_smtp_username,
'password' => $mail_smtp_password,
'port' => $mail_smtp_host_port,
));
Zend_Mail::setDefaultTransport($tr);
Sending a message anywhere in your app:
$mail = new Zend_Mail();
$mail->setFrom($email_from, $email_from_name)
->addTo($email_to)
->addCc($email_cc)
->addBcc($email_bcc)
->setSubject($email_subject)
->setBodyHtml($email_html)
->setBodyText($email_text)
->send()
;
The following works with sendmail and should work with sendmail-like smtp servers. If the from domain does not match the domain of the originating server, the chance of being flagged as spam increases.
$msg = 'my message body';
$subject = 'my message';
$to = 'email#example.com';
$from = "ali#yahoo.com";
$headers .= "From: $from\r\n";
$flags = '-f "$from"';
mail($to, $subject, $msg, $headers, $flags);
You can send through Yahoo's SMTP server.
smtp.mail.yahoo.com, SSL, port 465, username/password for the Yahoo address.
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>");
}
?>