tags:

views:

29

answers:

2

Hi All,

I have to send email from my gmail account using php mail() function. I have read the phpmailer but I don't want to include any external apis. Please suggest.

Thanks in advance.

A: 

Assuming mail() works on your server, just:

<?PHP
mail($to,$subj,$body,"From: [email protected]\r\n");

should do it.

The reason this works is that the "From" address on an email is really very much like the return address on a snail-mail envelope. I can write a letter and mail it from my home, with a return address for my office.

timdev
Thanks for your reply. Actually to extend my question, I have to develop a php application which would work on local host and send emails to outside addresses. I guess this is possible only when I use some mail server like gmail, as i dont have access to the client's local server to configure it.
Ankit
Most web servers will send mail just fine (to any address in the world). Have you even tried? Almost all unix/linux hosts have some variation of sendmail locally, and IIS boxes have some kind of local virtual-smtp service.
timdev
If mail() is *really* broken on your client's server, I'd look at using PHPMailer or SwiftMailer. These libraries will talk SMTP to a remote SMTP server, with all the options available. mail() simply can't do that. But remember: mail() probably works, so try it first, especially if you don't want to rely on a library!
timdev
+1  A: 

You cannot use mail() to send email with your Gmail account, you'll have to use something that talks to Gmail SMTP server directly. So you're stuck with writing your own or using one of the availiable libraries, the most commonly known are:

Alix Axel
PHPMailer works well - I implemented code for this last week and it runs very sweet.
Cruachan