PHP - Specifying the SMTP server to send mail through - php

I am developing a simple webmail client for IMAP-based email services using PHP's IMAP library, and I was wondering if there was any way to specify the SMTP server to use when sending an outgoing message, so that the message will be placed in the user's Sent mailbox when they are logged into their IMAP account. I saw that their is an imap_mail() function however it looks from the docs like it is just basically an alias for the normal mail() function, or am I wrong in assumming that?

Why don't you use a full featured class for this function, like PHPMailer?

Saving a copy to the Sent Mail folder is a function of IMAP, and is completely unrelated to queuing the message for delivery via SMTP. I.e., your code will have to do both operations separately -- one does not imply the other.

I have noticed that Gmail adds a copy to the sent folder on its own. Is this common behavior for the big web-mail providers?

Related

mail () SMPT error (5050) sending failed 5.1.1) PHP

I am having a problem that I have never encountered in my life.
In practice we have a website where we receive reservations, the A record is pointed towards our vps for the management of the site instead the part of mail is managed by aruba since the customer has preferred to leave their mail client. (for a matter of convenience because they have been using it for some time)
Going to the problem ..
the php mail () function when trying to send an email to the email: info#dominio.it our server returns as an error:
SMPT error (5050) sending failed 5.1.1)
in practice it is as if the email does not exist and was looking for it on our server but in reality it is located on aruba.
How can I tell the php mail () function to search for this mail not locally but on aruba?
with regard
Fabrizio C.
This only lays partial in php itself. You php.ini configuration file allows you to set a few settings
See:
https://www.php.net/manual/en/mail.configuration.php
The rest of the configuration need to be done in the MTA(Mail Transfer Agent) as in outgoing emails server of your VPS. Hard to say what you are using there..
Also according to your text, it looks like you tink the a record had something to do with your email...
a correctly configurated MTA does the following on an email address.
username#domain.tld
It requests the MX requests of domain.tld, and does not use an A record.
Then tries the MX records in order of their priority to deliver the email.
(This also is outside the scope of PHP, but might be good to know)

check and view reply for sent email - PHP - Codeigniter

I am using php and Codeigniter, I need to implement the following schema
for list of emails I need to send emails for them, and if they reply to this email, I need to know if the reply and what the content of this reply and view it using my php project.
Also I need to send them a multiple choice question and they have to chose one option as answer, and then check this answer from my php project.
I can send email from my gmail, but can't send it from the wamp server itself.
Any help?
I'm not exactly familiar with CodeIgniter, but if you want to send an email, and it doesn't work, especially on Windows the common problem is that there is no SMTP server configured.
Most *nix distros have some form or another of a SMTP server running by default, Windows doesn't. When unconfigured PHP assumes localhost to be an SMTP server.
This SMTP server is what PHP needs to be able to send emails, so you'll either have to configure one or find one you can use that's provided by someone else.
For using PHP's native mail() function and any library that relies on it the configuration entries you're looking for are SMTP and smtp_port. (Have a look at: http://php.net/manual/en/function.mail.php)
It might very well be that your ISP provides a SMTP server. Otherwise you can always set up a gmail/hotmail/whatever account and use their SMTP settings. This is usually far easier than setting up your own server.
If you choose to set one up yourself, you'll want to look into SPF, so your messages don't get marked as spam by default.

codeigniter email class not sending mail with imap [duplicate]

I am developing a lightweight Gmail client for mobile phones, accessing Gmail by IMAP. I want to send a draft from the Drafts folder, but it has some attachments and I cannot download all of them to send it by SMTP.
Moving/copying it to "Sent Mail" does not send it, just moves it to that folder.
How can I send a Draft directly without fetching all the content and attachments from the client? Is there any IMAP command to do it?
IMAP is a mailbox protocol. It does not (natively) support sending mail, only accessing it. In order to send mail you must use SMTP. Its possible that there is an IMAP extension for sending mail, and its possible that Google Mail supports that extension, but I doubt it. Hence, if you want to send an email with attachments, you must actually have the full content of the message available to you to send.
IMAP was designed to receive email messages, not to send it. There is no IMAP command for sending email AFAIK. There is, however, at least one IMAP server which supports a special 'Outbox' folder. When you place the message into this folder it will be sent automatically.
Check Courier-IMAP documentation on Sending mail via an IMAP connection. Note, that this is a non standard method and I'm not aware of any other server which supports this.
There RFC 4468 which extends SMTP so it can fetch the mail content from the IMAP server, but I don't know about any working and widely used implementation.
Talking about gmail: sticking with SMTP is probably the safest way to go.
By the way, now that any modern mail client (including the webbased ones) supports a Sent folder, you typicaly have to use both SMTP and IMAP to send a single mail. And there's a race condition between sending the e-mail over SMTP and successfully saving the e-mail to the IMAP Sent folder. Using IMAP for sending e-mail is a way to avoid this race condition.
Sending email is a special feature of some imap servers. Its nothing in the imap protocol. You just copy your email into a special imap directory on the server and it sends them. I doubt that gmail supports this.
I sent an email to my own email address using IMAP using Python 3 to a gmail account. What is does is append a message to a mailbox. You need to utilize a handful of Python's native libraries. Also study this documentation for imaplib, this code is featured in the section Uploading Messages: To add a new message to a mailbox, construct a Message instance and pass it to the append() method, along with the timestamp for the message.
Then check your gmail inbox and you'll see the new message.
import imaplib
import time
import email.message
import imaplib_connect
new_message = email.message.Message()
new_message.set_unixfrom('name')
new_message['Subject'] = 'Test'
new_message['From'] = 'name#gmail.com'
new_message['To'] = 'name#gmail.com'
new_message.set_payload('This is an example message body.\n')
print(new_message)
with imaplib_connect.open_connection() as c:
c.append('INBOX', '',
imaplib.Time2Internaldate(time.time()),
str(new_message).encode('utf-8'))
# Show the headers for all messages in the mailbox
c.select('INBOX')
typ, [msg_ids] = c.search(None, 'ALL')
for num in msg_ids.split():
typ, msg_data = c.fetch(num, '(BODY.PEEK[HEADER])')
for response_part in msg_data:
if isinstance(response_part, tuple):
print('\n{}:'.format(num))
print(response_part[1])

How can I create a mail server?

I would like to have a server that is able to receive emails. Then I want to use PHP to program the way the emails are shown to the users. Can I do it purely with PHP? I mean, it is not a problem to send emails from PHP but I do not know if I can receive emails by PHP? (In a way PHP receives POST requests).
ADDED
As a response to the first answer, I would like to specify that it looks like I need an SMTP server. I want to be able to communicate with the SMTP server in a programmatic way. For example, I want to have a possibility to "tell" to the SMTP server to create a new e-mail address. I also need to know where incoming emails stored and in what format. For example, how I can extract the "sender", "cc", "bcc" from the file corresponding to the received mail.
would like to have a sever that is able to receive e-mails.
If you are writing it from scratch then you'll need the specification for SMTP. I would advise very strongly against this. SMTP servers are hard to write, and there are several really good open source solutions out there.
My understanding of PHP is that it does very poorly when it comes to multithreading, so it probably isn't a good solution for this problem.
Than I want to use PHP to program the way the mails are shown to the users
Servers that receive mails do not typically show them to users. They usually store them in a standard way (such as Maildir or mbox) which other software (such as a local email client or an IMAP server) accesses.
The job of showing email to a user is belongs to email clients. Web based PHP web mail software includes SquirrelMail and RoundCube. AFAIK they both act as IMAP clients. See the IMAP specification.
As a response to the first answer, I would like to specify that it looks like I need a SMTP servers. I want to be able to communicate with the SMTP server in a programmatic way. For example, I want to have a possibility to "tell" to the SMTP server to create a new e-mail address.
Pick an SMTP server that runs on your OS. Read the instructions to find out how to configure delivery and accepted addresses. It usually comes down to manipulating text files.
I also need to know where incoming mails stored and in what format. For example, how I can extract the "sender", "cc", "bcc" from the file corresponding to the received mail.
Again. See the manual for the mailserver. Most will give you options about where to store the data and in what format.
Then you just need to decide if you are going to get PHP to dig into those directly, or use an IMAP server in between.
No, that is not easily possible. PHP is made for (stateless) http protocol, while a mail is sent in a conversation that is built up from various requests and responses.
It is possible to parse and process mails using PHP, but I would recommend installing a mailbox that you can read from PHP using POP3. Then, your PHP application can show and process mails from that mailbox.

is there something wrong with using php's native mail function?

i tried googling but sadly i get only documentations (or am i using bad keywords)
anyway
i can see that alot of programmers (even those im working with right now) does not seem to approve to using the php native mail function and resorts to using some other framework like sendmail? swift mailer etc...
i'd like to know why? are there really disadvantages to using the native mail function?
if so how does the mailing frameworkds solve that or how are they better??
There's nothing wrong with it for sending simple plain text emails.
However, once you get into multipart mime emails (say, you want an HTML version or to add an attachment) then you have to build the email yourself, and it can be quite tricky to get all the headers and encoding correct. In this case you're better off using a library.
The PHP manual for function mail mentions that there are some restrictions with the mail function and one of these are that the function opens and closes an SMTP socket for each email. The mail function works good when you just want to send a mail or two.
As far as I'm concerned, all of these problems pale in comparison to the major security problem:
Mail header injection: ( http://en.wikipedia.org/wiki/E-mail_injection , and php specific info: http://www.damonkohler.com/2008/12/email-injection.html )
Whereby a spammer bot spiders your site and, finding a vulnerability in your script that is easy to still have when using the very insecure mail() function, IS ABLE TO SEND EMAIL FROM YOUR SERVER TO AN ARBITRARY LIST OF CONTACTS, essentially turning your script & server into a cog in their spam email machine.
I recommend never using mail() with user input, and in general, just making use of PEAR::mail instead. http://pear.php.net/package/Mail/
Using PHP's mail() function requires a properly configured sendmail or equivalent on the host the program is running. However, the Windows implementation is a bit different. If you don't have your MTA configured properly, you won't be able to successfully send emails from your PHP scripts. Like another commenter said on this thread, PHP manual explicitly states that each call to the mail() function opens and closes a socket. This can cause unnecessary delay in script execution.
Additionally, your development and testing environment may not have a public static IP address. Your IP address might be blacklisted by DNSBL, Gmail, Yahoo! and other popular email service providers.
Your best bet in this situation is to use a properly configured external SMTP server. Chances are your employer has already provided an email account with SMTP access. If you don't have one you can use a Gmail account. Gmail provides SMTP access to all email accounts.
You can write scripts to open a socket connection to the external SMTP server. When there are tried and tested open source libraries for this purpose, why write your own?
Incidentally, I wrote a blog post on the very same subject yesterday: Using SMTP With Zend Framework - Solve Email Delivery Problem
Best regards,

Categories