SMTP Authentication using PHP Script without using Third Party Software - php

I am writing a website which will automatically generate emails to users on special occasions (e.g. the activation email after user registration). The problem is, SMTP authentication is required, but the website will be hosted on a web hosting service, which does not have Pear Mail installed. I have read many articles on the Internet, saying that either Pear Mail, PHPMailer or SwiftMailer is required for SMTP authentication. As I do not have the authority to install any of them, what can I do in the PHP scripting level if none of the above exists?
A side question is, if Pear Mail, PHPMailer or SwiftMailer is a PHP library, does it mean we can actually perform SMTP authentication by writing our own PHP code?
Added: This link (http://www.webdeveloper.com/forum/showthread.php?233847-A-Way-to-Send-SMTP-Mail-w-o-PEAR) seems to have provided a solution, but when I tried, nothing happened (no email was sent). Does it really work?

Classes like PEAR Mail, SwiftMailer of PHPMailer are all just PHP scripts, no different to your own scripts, so if you can run PHP at all, you can install and run any of these libraries without any special privileges. You don't even need shell access on your host - run the pear installer, or download the package on your dev machine, uncompress it, then upload the PHP files along with your own scripts. You only need to make sure your include paths are working for whatever location you choose to put them.
The installation mechanism described in the documentation is preferable as you can be sure it's putting everything in the right place, handling dependencies, versions etc, but so long as scripts are readable and accessible to PHP it doesn't matter how they get onto the server.
If you're lucky, you may be able to run the pear installer in your shell account, where it would hopefully install pear packages somewhere accessible to you.

Related

How to send an e-mail from a server on which I can't install anything?

I am hosting my website on https://www.gandi.net/en , thing is they don't allow connections over ssh only sftp.
I have a form on my website and want to allow user to contact me through that form. In other words an e-mail needs to be sent from form#mydomain.com to me#mydomain.com.
In order to do that there are a couple of options I tried, but none of them seems to work:
1) PHP's mail function: doesn't support authentification. In order to send an e-mail from form#mydomain.com I need to authenticate. And that doesn't seem to be possible with the mail-function
2) PHPMailer: I saw many people recommending this solution. I downloaded their sources and had a look. It seems like in order for it to work you need to actually install PHPMailer using composer. I can't run composer via sftp...
What other solutions exist? Or what workaround exists?
Thanks!
2) PHPMailer: I saw many people recommending this solution. I
downloaded their sources and had a look. It seems like in order for it
to work you need to actually install PHPMailer using composer. I can't
run composer via sftp...
You don't need to run composer, just download the PHPMailer files manually and upload it via sftp. Composer just makes it easier to install packages, the PHPMailer itself existed long before Composer was a thing.
If you don't know how to install it without Composer, take a look at this answer.
You can give swiftmailer a try: https://swiftmailer.symfony.com/
It is used by Yii2 Framework as well.
If I am not mistaken, for most if not all PHP extensions, you can integrate it into your website by using composer from your own development machine, and then you'd upload the source using FTP. You don't need to use composer on the host server.
I have used swiftmailer with Yii2 websites on a public host and I didn't have to do any special setup on the host.
If all else fails, change host. Get a refund and use your new host's web transfer services to move with minimum effort.
Looks like if you use a SPF TXT record in your DNS setup for their Simple Hosting package, you should be able to use mail() as-is without having to authenticate to your mail host:
# 10800 IN TXT "v=spf1 ip4:217.70.176.0/21 ip6:2001:4b98:c::/48 ip4:217.70.185.10 ip4:173.246.97.150 ip4:217.70.186.165 ptr ?all"
Source:
http://wiki.gandi.net/en/dns/zone/spf-record
Which has this quote:
This Simple Hosting records is only used if you plan to send e-mails through your website (via contact form, etc..)
After you've submitted each of these changes, be sure to activate the new version of your zone file, and keep in mind that it will take about 3 hours for it to take full effect due to the standard DNS propagation delay.
You can confirm the SPF TXT record is deployed using various online SPF testing tools.
Since a lot of users have the ability to edit StackOverflow posts, I recommend going to the source and copying and pasting the appropriate SPF record directly to avoid shenanigans. Also the Gandi Wiki will always have the latest list of IPs whereas this post may go stale after 6 months.
I recommend Composer-less CubicleSoft Ultimate E-mail Toolkit or one of the other already mentioned libraries over directly using the built-in PHP mail() function. The PHP mail() function is very misleading. It simply drops a properly prepared mail message into the mail queue on the local host. However, e-mail is very complex and really needs a library even for the most basic PHP mail() use-cases.
If you want to use one of the other Composer-enabled libraries but don't want to upload thousands of little rinky-dink files that comes with Composer-enabled software, then check out CubicleSoft Decomposer. Decomposer takes an entire Composer project tree and generates standalone builds with 1-2 files as the output.

How to enable mail() function

I am trying to work with a shared host in which the mail() function was disabled (I can see that on disabled functions on phpinfo()) by someone that I don't know.
All I have access is the root folder for the site files in the ftp, so I can't edited the php.ini in /etc/ neither restart Apache (I know is a linux based OS).
Is there any way I can enable it by a secondary php.ini or ini_set()?
EDIT: I am aware of PHPMailer. I was trying to use it but it was being block as well. That's why I started trying to use mail(). Just to see if it would work
You need to get a new host or upgrade to some different plan.
However you can go ahead with mailgun(https://www.mailgun.com/) which is very cheap and no cost to set up.
You just need to link your credit card for a safety but they never charge you unless you pass 10,000 emails per month(https://www.mailgun.com/pricing).
There are other solutions as well like mandrill or sendgrid. However, Mailgun is almost free unless you are using more transactions emails.
You can find the official mailgun php package in here : https://github.com/mailgun/mailgun-php
You need to check if curl is installed in your server though.
Secondly, trying to send emails directly is not a good practice. Most of the times they will end up getting in spam folder. Use SMTP and use PHPMailer Extention.
Steps to do so :
1. So to cpanel and go to emails
2. Create a new email account and add a password
3. Click on configure mail client related to the new email address you have created
4. You will get 2 tabs, SSL configurations and NON SSL configurations
5. You can choose TLS, copy the outgoing host, email and password.
6. Now install phpmailer and enter the above credentials and try sending email.
7. Also as a safety, go to authentication in your cpanel and check if any issues are mentioned for emails just for safety.

Configuration issue when sending email on Mountain Lion using Postfix

I've been stuck on this problem for the whole afternoon and tonight but cannot find an answer. But I believe it's some easy configuration issue on Mountain Lion.
I want to send out email using PHP, and I installed PEAR:Mail package; that all works fine.
But when I try to send email in PHP, it returns me no error; but email not received.
I further dig into the problem, found that I need to properly set up my local postfix mail program. I therefore following these articles to setup:
http://benjaminrojas.net/configuring-postfix-to-send-mail-from-mac-os-x-mountain-lion/#comment-259; when that didn't work, I followed that one instead: http://slashusr.wordpress.com/2012/02/14/enabling-postfix-for-outbound-relay-via-gmail-on-os-x-lion-11/.
The basic steps in the blog post is to teach you how to send out using GMail by doing some settings in /etc/postfix/main.cf
But when I test it in terminal with command:
date | mail -s test <my_email_address>
Nothing happened, except that when I type "mail" again, it prompted me with a new message, telling me this error (part, only what I think to be useful):
: host smtp.gmail.com[209.85.225.108] said: 530-5.5.1
Authentication Required. Learn more at 530 5.5.1
http://support.google.com/mail/bin/answer.py?answer=14257
aa4sm11414435igc.15 (in reply to MAIL FROM command)
--1306515822A0.1347279593/songyy.local
Content-Description: Delivery report
Content-Type: message/delivery-status
I visited the link given in the error message, but it's irrelevant.
I'll very much appreciate if anyone can give me some advice on this.
I want to send out email using PHP, and I installed PEAR:Mail package; that all works fine.
Very well. Then you must have a "mail relay server" address, to whom mails get sent. It is usually supplied by your Internet provider.
You must configure that address into Postfix, as relayhost. No other setting is usually necessary.
The recipe you followed is much more complicated because it entails Postfix connecting to GMail and authenticating as a user, all through TLS/SSL. To do this you must store the username and password to your GMail account (or another account created for that purpose) in the postfix passwd file.
The fact that GMail complains of "Authentication Required" tells us that either you skipped this detail, or you stored the wrong password; so GMail can't go through the email forwarding process.
Note that if you only use plain SMTP, the outbound emails will be sent in the clear (as opposed to GMail use of SSL encryption).
Check out the system logs if you need to see in detail what happened to your mails.
Another thing to watch out, you say:
I specified the file by setting: smtp_sasl_password_maps=hash:/etc/postfix/sasl_passwd
This file expects to be in a specific format (documented in the file itself). After that, though, you have to convert the file into the hashfile, which is the same filename but .db extension. Some distributions do this automatically, otherwise you need to manually run the postfix utility postmap (or in a pinch, makemap). In your case:
postmap -c /etc/postfix /etc/postfix/sasl_passwd
This will take /etc/postfix/sasl_passwd and hash it into /etc/postfix/sasl_passwd.db, which is the actual file Postfix checks. It is possible that this particular detail was not covered in the tutorial you followed, and Postfix is still checking a .db file which was built from the previous (and empty!) sasl_passwd.
You are quite right that by specifying user, host and pass in PEAR, you are effectively overriding Postfix configuration - I think the Mail package performs so-called direct-to-MX exchange, and thus does not even need Postfix at all.
The log files should be specified in main.conf and are usually /var/log/messages or /var/log/mail(.something).

How to send emails with my own email server installed locally

I have installed my mail enabled professional copy on the same server as php is installed (and wordpress) and I want to use the mail server when people enter emails forms etc.
I started with looking on simple email scripts but they all have failed and I have been googling for at least 8-9 hours on different solutions on scripts with sendmail, scripts using gmail, and lots of options that almost all require PEAR or some other solution - and I have thrown in the towel before I start tearing my hair out in frustration..
So Now I want to go with this option as it’s simpler for me and I do already own a license to mail enable.
How can I accomplish this?
I have the newest version of php available... Server is a Windows server 2003 ENT with IIS 6 installed and MySQL server/wordpress.
I am not a programmer - just a "normal" guy.. I know to get around with computers - most of the time - but coding Is like greek to me! (Which in this case might be my Achilles heel)
Try using PHPMailer or SwiftMailer.
These libraries make it trivial to configure your mail server to send emails with.
Mailenable should provide you with a smtp server and credentials.
Here's an example with phpmailer :
http://phpmailer.worxware.com/index.php?pg=examplebsmtp

Has Anyone Here ever tried PEAR

Hi I have been reading alot of articles adoring the PEAR mail package and it seems like PEAR is something I need to try out.
I am interested in setting up a full mail server, similar to a conventional SMTP mail service; which incorporates mail queuing, resending with a backend database etc. My impression is that PEAR can do this but can its service be used with mail clients like outlook to send mail, just as how any any smtp server daemon can where one would enter a portnumber, server name and/or security protocol?
Thanks
No, PEAR isn't going to magically solve these problems for you.
PEAR is a collection of PHP classes that are meant to solve common problems faced by PHP users. The Mail packages offer code for interacting with different parts Email systems. They do not contain code for creating email systems from scratch.
For example, form the Mail_Queue documentation
The Mail_Queue class puts mails in a temporary container, waiting to be fed to the MTA (Mail >Transport Agent), and sends them later (e.g. a certain amount of mails every few minutes) by >crontab or in other way.
The MTA in this case in sendmail, postfix, etc.
Another example, from the Mail_Mbox documentation
It can split messages inside a Mbox, return the number of messages, return,
update or remove an specific message or add a message on the Mbox
Incorrect use of "an" aside, you are using this to read existing MBOX files, and not caring how they got there.
The Mail package is about interacting with existing mail systems, NOT creating replacements. You'll still need to understand how all those email systems work to create a "full mail server, similar to a conventional SMTP mail service". If you're doing this because you want to learn how email systems work, have at it. If you're doing this because you thing this will give your business some leg up in the email game, I laugh and say "good luck with that"
PEAR is a repository for lots of libraries. Some of them deal with mailing.
PEAR's Mail class is designed for sending mail only. It is not designed as an implementation of an SMTP server.
Pear Mail is an SMTP sender aka client, not an SMTP server. Although it's entirely possible to write server (any kind of server) in php that doesn't mean that writing an SMTP server yourself is necessarily a good idea as it requires quite some expertise to do it right (spam anyone?). If you want to see an SMTP server implemented in a scripting language, go have a look at Lamson, written in Python by Zed Shaw.
And while you're there, do read the About page. This quote says it all
However, as great as Lamson is for
processing email intelligently, it
isn’t the best solution for delivering
mail. There is 30+ years of SMTP lore
and myth stored in the code of mail
servers such as Postfix and Exim that
would take years to replicate and make
efficient. Being a practical project,
Lamson defers to much more capable
SMTP servers for the grunt work of
getting the mail to the final
recipient.
It seems to me that PEAR's MailQueue package may address your needs.

Categories