Asked  7 Months ago    Answers:  5   Viewed   28 times

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' . "rn";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
$headers .= 'To: '.$to.'' . "rn";
$headers .= 'From: '.$name. '<'.$email.'>' . "rn";

echo $message='email text here';
@mail($to, $subject, $message, $headers);

 Answers

73

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:wampsendmailsendmail.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);
Wednesday, March 31, 2021
 
HamidR
answered 7 Months ago
36

Azure has no such facility to generate a zip file for a bundle of blobs for you. Azure Storage is just... storage. You'll need to download each of your blobs via php sdk (or directly via api if you so choose).

And if you want the content zip'd, you'll need to zip the content prior to storing in blob storage.

Your code (in your question) won't work as-is, since get_file_contents() expects to work with file I/O, and that's not how to interact with blobs. Rather, you'd do something like this:

$getBlobResult = $blobClient->getBlob("<containername>", "<blobname>");
file_put_contents("<localfilename>", $getBlobResult->getContentStream());
Wednesday, March 31, 2021
 
msg
answered 7 Months ago
msg
87

The only feasible way of doing that with PHP that I see following as possible solution to your issue:

  1. Create a small php file that checks the queue for messages and process them if any. Retrieve only one message at a time. Do not forget to delete the message after processing. When retrieving the message, get it with appropriate timeout to have enough time for processing.
  2. Create a small .bat (or .cmd) file that invokes the php.exe binary providing the path to your php file
  3. In a startup task configure Windows Task Scheduler to execute the batch file (.bat or .cmd) every N seconds/minutes

Hope this helps!

Add comment if you need any additional help, and I'll try to provide code samples. Everything is pretty trivial.

Wednesday, March 31, 2021
 
Extrakun
answered 7 Months ago
94

First download the JavaMail API and make sure the relevant jar files are in your classpath.

Here's a full working example using GMail.

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class Main {

    private static String USER_NAME = "*****";  // GMail user name (just the part before "@gmail.com")
    private static String PASSWORD = "********"; // GMail password
    private static String RECIPIENT = "lizard.bill@myschool.edu";

    public static void main(String[] args) {
        String from = USER_NAME;
        String pass = PASSWORD;
        String[] to = { RECIPIENT }; // list of recipient email addresses
        String subject = "Java send mail example";
        String body = "Welcome to JavaMail!";

        sendFromGMail(from, pass, to, subject, body);
    }

    private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
        Properties props = System.getProperties();
        String host = "smtp.gmail.com";
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);

        try {
            message.setFrom(new InternetAddress(from));
            InternetAddress[] toAddress = new InternetAddress[to.length];

            // To get the array of addresses
            for( int i = 0; i < to.length; i++ ) {
                toAddress[i] = new InternetAddress(to[i]);
            }

            for( int i = 0; i < toAddress.length; i++) {
                message.addRecipient(Message.RecipientType.TO, toAddress[i]);
            }

            message.setSubject(subject);
            message.setText(body);
            Transport transport = session.getTransport("smtp");
            transport.connect(host, from, pass);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        }
        catch (AddressException ae) {
            ae.printStackTrace();
        }
        catch (MessagingException me) {
            me.printStackTrace();
        }
    }
}

Naturally, you'll want to do more in the catch blocks than print the stack trace as I did in the example code above. (Remove the catch blocks to see which method calls from the JavaMail API throw exceptions so you can better see how to properly handle them.)


Thanks to @jodonnel and everyone else who answered. I'm giving him a bounty because his answer led me about 95% of the way to a complete answer.

Tuesday, June 1, 2021
 
HexaGridBrain
answered 5 Months ago
90

For sending out the actual e-mail I would recommend using the PHPMailer library, it makes everything much easier.

Thursday, July 29, 2021
 
Isky
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :