Send email using PHPMailer, Mailgun and HTTP api - php

I Am able to send emails off this web server
I am on a shared web server using godaddy, and it's been awesome for me in a number of ways. I host two aspects of my website on here: a public wordpress site and an employee intranet. That's right, it has two aspects, with my custom built intranet being hosted next to a wordpress site. I CAN send emails.
The way I can send emails is via the Mailgun plugin for wordpress. As far as I can tell, this is the ONLY way my web server is sending emails right now. I have tried PhpMailer and php mail() vanilla, neither of those are doing squat.
I am using mailgun as my mail server, and it's working out pretty well so far. I use the wordpress plugin with ninja forms, and all my submissions to the forms get emailed to my email. The MX entries and txt files are all setup, so that is not my issue.
I was able to send emails with the program when it was hosted locally
I know the emails sent from the intranet are configured correctly, because it worked all day long when hosted locally. When it was hosted locally, I was sending through php mail() [I know, I know, hold your jeers] and it worked fine. When I brought the intranet online however, I started getting zero emails.
go daddy tells me "buzz off" essentially. They say no matter what smtp server I'm pointing to, they see no record of an email trying to be sent through them. No matter what host, no matter what domain, no matter what, they can not see the emails trying to be sent.
I have another mx entry, I believe because of our office 365 email server.
This mx entry was brought to my attention because of godaddy. That mx entry points to outlook. From what I can tell, this is clogging my smtp abilities.
How does the mailgun plugin send emails then? Http.
I know, I know, this sounds weird. It sounds weird to me too, I can find a whopping one site on the internet that even talks about it, and it's On our site. What it seems to be saying is that you can send the email info via http, which the mail server decodes and emails via smtp. I think this will be the easiest way to send email on my site, as it is what the Mailgun plugin does.
How do I know the Mailgun plugin uses http instead of smtp?
Mailgun Plugin configuration
So, the plugin asks for my api key and domain name, nothing else. What I'm HOPING to do is to figure out how to do this with my php code. Phpmailer has no documentation on this that I can find, and Mailgun recommends using Phpmailer or the Wordpress plugin.
I cracked open the plugin, and am very confused
I would describe myself as Php attempting. This is way too much for me. The code I THINK is allowing the plugin to do it's thang is this
public function api_call($uri, $params = array(), $method = 'POST')
{
$options = get_option('mailgun');
$apiKey = (defined('MAILGUN_APIKEY') && MAILGUN_APIKEY) ? MAILGUN_APIKEY : $options['apiKey'];
$domain = (defined('MAILGUN_DOMAIN') && MAILGUN_DOMAIN) ? MAILGUN_DOMAIN : $options['domain'];
$time = time();
$url = $this->api_endpoint.$uri;
$headers = array(
'Authorization' => 'Basic '.base64_encode("api:{$apiKey}"),
);
switch ($method) {
case 'GET':
$params['sess'] = '';
$querystring = http_build_query($params);
$url = $url.'?'.$querystring;
$params = '';
break;
case 'POST':
case 'PUT':
case 'DELETE':
$params['sess'] = '';
$params['time'] = $time;
$params['hash'] = sha1(date('U'));
break;
}
// make the request
$args = array(
'method' => $method,
'body' => $params,
'headers' => $headers,
'sslverify' => true,
);
// make the remote request
$result = wp_remote_request($url, $args);
if (!is_wp_error($result)) {
return $result['body'];
} else {
return $result->get_error_message();
}
}
If this is not it, the two main files are in the comments, because of my reputation.
Alternative hopes
I understand this might be straight impossible. If it is, i am thinking there are two alternatives, there might be more. The two alternatives I see are sending my emails the way Ninja Form does to this plugin, but I have no clue as to how to go about that. The second is forcing phpmailer through the outlook mx entry, and sending it via smtp. How I would go about this is also beyond me. Any other suggestions or solutions are welcome.

You should search before posting. GoDaddy blocks outbound SMTP, but they provide a gateway that you can use instead. This blocks many sending scenarios (such as GoDaddy failing SPF checks). MX records have absolutely nothing to do with outbound mail.
You can use PHPMailer with HTTP services like MailGun by using it to construct messages for you. Build your message as you would normally in PHPMailer, then call preSend() and getSentMIMEMessage() instead of send(). You will then have a complete message you can submit to the HTTP API.

Related

Why isn't the Codeigniter email class sending mail with sendmail?

I'm attempting to send an email address out to registrants who register for my site however this section of my code is not running. I'm attempting to find out why. I've followed all the necessary documentation. I replaced this section with php's mail function for debugging to see if it'll send an email on my server and it will. Does anybody have any thoughts on what it could be?
// User was successfully created and the user needs to verify their account.
// Send registered an email informing them how to validate their account.
$this->load->library('email');
$this->email->from('owner#owner.com', 'Owner Name');
$this->email->to($post_email_address);
$this->email->subject('Site Name Here Registration');
$this->email->message('Thank you for registering for our site. Here is your registration key to activate your account: '.$registration_key.' Please click on the following link to activate your account.<br />'.anchor('project-manager/login/verify/'.$registration_key, 'Click Here To Activate Your Account', ''));
$this->email->send();
Here is the configurations for the email.com inside of the application/config directory.
<?php
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
I know that it seems unnecessary, but I typically always use SMTP with the mail class. There are just too many server configuration issues that can result in mail not being sent. Sometimes anonymous (e.g. 'nobody' or 'www-data') users are restricted from using sendmail - a very common problem if the server isn't properly configured with suexec.
Additionally, using SMTP relies on known-good DNS / SPF settings of a known-working mail server - might not be the case on individual app servers that you use.
You could probably get to the bottom of the issue with a bit more digging, and possibly even get it corrected - but then you've got the same issue all over again if you need to move your app, or bring up more servers.
Just follow the email class documentation to use SMTP, which is just really a matter of changing the $config['protocol'] accordingly, then add the SMTP host / user / pass variables. Set up an email account, and give it a go. Mail forwarding settings can handle things like noreply a bit more gracefully as well.
To try it, just override the $config array in your controller, e.g.:
$myTempConfig['protocol'] = 'smtp';
$myTempConfig['smtp_host'] = 'mail.somedomain.com';
$myTempConfig['smtp_user'] = 'username';
$myTempConfig['smtp_pass'] = 'swordfish';
$myTempConfig['smtp_port'] = 25;
$myTempConfig['smtp_timeout'] = 30; // adjust as needed for busy mail servers
then call:
$this->email->initialize($myTempConfig)
so it loads the settings. You could just call it $config if you want, I just like making it clear what I'm doing when I do that.
It's really just better to rid yourself of the problem and be done with it once and for all. Once you have SMTP setup, your code just works, no matter where you put it.
Most decent hosting utilizes suexec correctly which means the mail class will 'just work' using sendmail. However, if you tend to deploy a lot of stuff with platform-as-a-service providers, or your own servers, it just makes better sense to use SMTP and save some headache and work.

Swiftmailer - PHP-Mails landing in Spam folder

Im using swiftmailer for sending mails via PHP. Most times it works fine. But sometimes, my mail Mails are landing in Spam-Folder.
Here my code, which sends the mails
function sendMail2($from,$to,$subject,$body,$attachment=NULL) {
require_once 'include_apth/swiftmailer/swift_required.php';
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance($subject);
$message->setFrom($from);
$message->setTo($to);
$message->setBody($body, 'text/html');
if($attachment) {
$message->attach(Swift_Attachment::fromPath($attachment));
}
if(#$mailer->send($message)) {
return true;
}
else {
return false;
}
}
any ideas, why its landing sometimes in spam-folder?
I was having the same issues with deliver-ability of emails. Getting all of the correct DNS settings, headers and the like isn't enough.
Most, if not all cloud-hosting, and home-ISPs IP ranges are on various lists of IPs from where emails are not expected to be sent from - and so they are more likely to be marked as spam.
The easiest way to solve that is to use a dedicated service where emails are well known to come from, and that the company spends a great deal of effort to get email delivery properly configured.
There are a number of well known such companies, many of which offer significant free tiers, as long as you are well behaved and send appropriate emails that aren't being marked as spam, or bounce. If you are hosted on Amazon EC2 for example, you can get over 60,000 emails delivered per month via AWS/SES. My my own systems, I have an account, currently free, with Mailgun, and a 'limit' of 10,000 email sends per month.
For Swiftmailer, there are a number of plugins that can, for example, use a HTTP API to send email to the service, which then is sent over SMTP in the usual way - with greatly improved deliverabilty.
Add the below code and it will work perfectly
$headers =& $message->getHeaders();
$headers->addIdHeader('Message-ID', "b3eb7202-d2f1-11e4-b9d6-1681e6b88ec1#domain.com");
$headers->addTextHeader('MIME-Version', '1.0');
$headers->addTextHeader('X-Mailer', 'PHP v' . phpversion());
$headers->addParameterizedHeader('Content-type', 'text/html', ['charset' => 'utf-8']);
Solution get from the below question
Swiftmailer mails go into SPAM Folder

Can't send email via SMTP because "550 - Relay Not permitted"

I'm using CakePHP to send automated emails to clients. It's been working great, but it seems some recipients aren't receiving our emails. So I decided to use the SMTP option for sending emails, and route emails through our email provider at Media Temple.
However, when trying to send email from a Media Temple account, I get the error "550- relay not permitted".
That sounds like the Media Temple server is just plain not allowing me to send mail through it.
That's odd because I've confirmed the username and password I'm using is correct and I can send mail via SMTP through it from my macmail client and iPhone mail client. I've also confirmed my cakephp email settings are correct, because I can send emails via SMTP with a gmail account with the exact same configuration in cakephp.
Any idea why I'm getting this error and how to resolve it?
Thanks
Here's the code that handles sending an email. I use this class just like the regular EmailComponent from within many different controllers.
class CanadafindsEmailerComponent extends EmailComponent
{
...
function send($content = null, $template = null, $layout = null) {
if(!in_array(TECHY_MONITOR_EMAIL,$this->bcc) && is_array($this->bcc))
$this->bcc[]=TECHY_MONITOR_EMAIL;
else if (!in_array(TECHY_MONITOR_EMAIL,$this->bcc) && !is_array($this->bcc))
$this->bcc=array(TECHY_MONITOR_EMAIL);
if(DEVSITE){//commented-out code are settings for smtp with gmail, which works fine
$this->delivery = 'smtp';
$this->smtpOptions = array(
'port'=>'465',//'465',
'timeout'=>'30',//'30',
'auth' => true,
'host' => 'ssl://mail.thenumber.biz',//'ssl://smtp.gmail.com',
'username'=>USERNAME,//'USERNAME#gmail.com',
'password'=>SMTP_PASSWORD//,
);
$this->to=$this->correctFormatOn($this->to);
$this->bcc=$this->correctFormatOn($this->bcc);
$this->cc=$this->correctFormatOn($this->cc);
$this->replyTo=$this->correctFormatOn($this->replyTo);
$this->from=$this->correctFormatOn($this->from);
}
return parent::send($content,$template,$layout);
}
function correctFormatOn(&$email){
if(is_array($email)){
$copiedEmail=array();
foreach($email as $singleEmail){
$copiedEmail[]=$this->correctFormatOnSingle($singleEmail);
}
$email=$copiedEmail;
}else{
$email=$this->correctFormatOnSingle($email);
}
return $email;
}
function correctFormatOnSingle(&$email){
$subEmails=explode(",",$email);
$fixedSubEmails=array();
foreach($subEmails as $subEmail){
$fixedSubEmails[]=preg_replace('/<?([^< ]+)#([^>,]+)[>,]?/i', '<$1#$2>', trim($subEmail));
}
$email=implode(",",$fixedSubEmails);
return $email;
}
}
The main problem I was having was that clients weren't receiving emails from our server, (and so I wanted to use an SMTP server to see if that would fix it, instead of the server's default email server).
But I managed to get those clients to receive emails from the server by making some other changes, thus removing the need to use SMTP and the Media Temple email server.
(As an FYI, I found that we were getting bouncebacks from client email servers stating Diagnostic-Code: smtp; 550 Access denied - Invalid HELO name (See RFC2821
4.1.1.1), but they were being sent directly back to the server, and going into the linux user account "www-data". (I read them in /var/mail/www-data, just using tail and vim). I found that postfix, which was handling the sending of emails, was marking the email sender's hostname (ie, "HELO name") as canadafinds3, the name I gave the server in Rackspace, not the domain name: canadafinds.com. So I changed that in /etc/postfix/main.cf, restarted postfix, et voila! No more bouncebacks from those particular clients, and everyone's happy again.)
I ended up writing my own PHP mail() script based on https://web.archive.org/web/20180401094709/http://www.dreamincode.net/forums/topic/36108-send-emails-using-php-smtp-direct/ in order to circumvent this error.

Configure PHP to send all mail to one account only

How can I configure PHP to send all outgoing mail to my own account so that I can test a business application without actually sending mails to unsuspecting businesses, such as "Congratulations, you have a new account. You will be billed for $xxx" ?
Rather than configuring PHP, a generalized solution would be to stand up a dummy SMTP server.
See this question.
So you already wrote the application and it uses live email addresses, and now you want to test it? Did you use a centralized function for mail or are there tons of mail() calls all over the code? Sorry but you're going to have to change every mail() call. Do yourself a favor and replace them all with your own function, and then handle test/live functionality in that one location.
You can redirect all port 25 traffic on the server running PHP to a mailserver/port which delivers all mail to you.
This is the only 100% foolproof method of which I know.
You could create a Google Apps account (or use your dummy server), create a catch-all email account and have it sent to the domain. All you would have to do is look at the catch-all account.
I found this site: http://dummysmtp.com/.
My server is running qmail, so I edited the contents of /var/qmail/control/smtproutes like so:
:smtp.dummysmtp.com username password
It worked when I sent a simple mail with PHP mail(), but later I found that mail is still getting out to other people. I had to crawl into the bowels of the code and found this:
/* Choose the mailer */
switch($this->Mailer) {
case 'sendmail':
$result = $this->SendmailSend($header, $body);
break;
case 'smtp':
$result = $this->SmtpSend($header, $body);
break;
case 'mail':
$result = $this->MailSend($header, $body);
break;
default:
$result = $this->MailSend($header, $body);
break;
//$this->SetError($this->Mailer . $this->Lang('mailer_not_supported'));
//$result = false;
//break;
}
So I had to make sure that each option was configured to send its mail to dummysmtp.com. Once I got that figured out, it all worked.

Why is email not being sent through PHP even though it is being reported as sent?

I'm trying to sends mails in PHP. The code I used for sending a mail in CakePHP is given below. I get the message 'Simple Email Sent' in my web page but the mail is not delivered to my inbox. Am I missing something?
The values in the to, subject and link fields are set with the values entered in the user interface.
$this->set('to',$this->params['form']['to']);
$this->set('subject',$this->params['form']['subject']);
$this->set('link',$this->params['form']['link']);
$this->Email->to = to;
$this->Email->subject = subject;
$this->Email->from = 'someperson#somedomain.com';
$this->Email->delivery= 'mail';
$this->Email->sendAs='text';
$this->Email->template = 'simple_message';
//$this->Email->send(link);
if ( $this->Email->send(link) ) {
$this->Session->setFlash('Simple email sent');
} else {
$this->Session->setFlash('Simple email not sent');
}
On a Linux system, you'll probably have a sendmail script installed already, and PHP will use that. If this is what you have and it's not working, then I'd look for mail configuration problems in your Linux system itself.
On a Windows system, you'll need to configure the SMTP server you want PHP to send mail to. The normal way to do this is in php.ini. The instructions for this are here.
Unless you have set Email->delivery this should be the same for CakePHP - it should default to whatever PHP uses.
Note: If you are using your own Linux install, it could just be that your ISP is blocking port 25, which your mail server is using. In that case you'll need to configure linux to route email to your ISP's email server. Maybe this will help?
Since when is 'to' (line 4) a valid destination email address?
You need to use variable syntax for setting to 'to' line, and the 'subject' line. Those lines should read
$this->Email->to = to;
$this->Email->subject = subject;
Also, I believe there is an attribute in the Email component called error (I cannot find it in the documentation currently) that will help you debug. This may not be totally correct; I use the Email component with SMTP, and there is an attribute that gets set by the Email component called smtpError. I believe there is one called error that you can use to check for an error -- it should contain code that will tell you where your problem lies.
In case that's an incorrect statement, you can always do a var_dump( $this->Email ); after you try to send an email. That will dump the entire contents of the object, so you can see if you have set attributes correctly, and it should help you find out what the error attribute is named.

Categories