20

How can i send an Email using PHP at windows Azure?

i am using simple mail function:

$to .= 'email-Id';
$subject = " Test Subject";

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: '.$to.'' . "\r\n";
$headers .= 'From: '.$name. '<'.$email.'>' . "\r\n";

echo $message='email text here';
@mail($to, $subject, $message, $headers);
Amir
  • 2,028
  • 6
  • 19
  • 28

5 Answers5

18

To send emails using PHP you have a few options:

Option 1: Use SMTP

You'll need to modify your php.ini configuration file (http://php.net/manual/en/ref.mail.php) and set the SMTP value to an external SMTP server you can use. SMTP servers are not part of the Windows Azure features at the moment.

[mail function]
SMTP = mail.mycompany.com

Option 2: Use sendmail

You'll need to modify your php.ini configuration file (http://php.net/manual/en/ref.mail.php) and set the sendmail_path value to the sendmail executable.

[mail function]
sendmail_path = "C:\wamp\sendmail\sendmail.exe -t"

Since sendmail doesn't exist in Windows, you'll need to use the fake sendmail for windows: http://glob.com.au/sendmail/

Option 3: Use a mail/smtp service

You could use a service like SendGrid to send your emails (they have an offer for Azure users: http://sendgrid.com/azure.html). They'll take care of sending out the email, you'll just need to call the REST api:

$sendgrid = new SendGrid('username', 'password');
$mail = new SendGridMail();
$mail->addTo('foo@bar.com')->
       setFrom('me@bar.com')->
       setSubject('Subject goes here')->
       setText('Hello World!')->
       setHtml('<strong>Hello World!</strong>');
$sendgrid->smtp->send($mail);
Sandrino Di Mattia
  • 24,739
  • 2
  • 60
  • 65
  • 6
    how does this apply to azure websites http://www.windowsazure.com/en-us/home/scenarios/web-sites/ ? – lrkwz Mar 12 '13 at 12:13
  • It applies 100%. You can change php.ini settings on Azure by overriding them. Check out info here https://azure.microsoft.com/en-us/documentation/articles/web-sites-php-configure/ – Mastro Dec 29 '15 at 16:20
7

I had never done PHP, but the following guide was step by step and incredibly easy to get working.

http://www.windowsazure.com/en-us/Documentation/Articles/store-sendgrid-php-how-to-send-email/

Hope it helps someone.

GR7
  • 5,083
  • 8
  • 48
  • 66
  • I *have* done PHP before. It worked, but did involve quite a few steps and I didn't find it "incredibly easy". The main thing though, is that it did work - eventually. It was also worthwhile for setting up SendGrid, which looks like a useful add-on in Azure. – Stephen Hosking Feb 13 '16 at 05:36
0

email-Id ?? what is this? I'm guessing it is the email address of the recipient.

Your headers do not require the To: as the to address is specified in the first parameter. Unless you know the recipient's name and want him to see email was sent to: Some Name not just you do not need that. Also you have an error in it: missing <> before and after the email address.

P.S. Emails sent through PHP's mail() function have one of the highest rates of ending up in SPAM, especially if you do not have Domain Keys and SPF set in your DNS for this. If using Cpanel please refer to Email Authentication section of your Email group in Cpanel.

transilvlad
  • 13,974
  • 13
  • 45
  • 80
  • it is sample code, here will be admin id, $to = 'admin@site.com'; email sent on other servers, but it is not working with windows azure. – Amir May 14 '12 at 11:43
0

I was having the same trouble , but this solution works perfectly for me . just follow these steps :

  1. Just enable 2 step verification on your G-mail account.
  2. Go to app password and then select app = other and then type AzureWebsite and generate a password , and keep the password.
  3. replace the

$mail->Password = 'new password';

4.I hope this will work for you too .

Sachin Sharma
  • 164
  • 1
  • 1
  • 12
0

Updated information @ Nov-2017:

Full Blog post: https://blogs.msdn.microsoft.com/mast/2017/11/15/enhanced-azure-security-for-sending-emails-november-2017-update/

Recommended Method of Sending E-mail

"Microsoft recommends that Azure customers employ authenticated SMTP relay services (typically connected via TCP port 587 or 443, but often support other ports too)....."

Community
  • 1
  • 1
Michael
  • 1
  • 1
  • 1